I want to fix the header for a UITableView such that the header sticks to the top when the table view is scrolled.
I did some googling and it seems that cleanest way is to use subclass a UINavigationController and make it follow the UITableDataSource and UITableViewDelegate protocols, and the in the view of the UINavigationController add the header view and the tableView and implement all the protocol methods.
basically I am doing exactly as the chosen answer for this stackoverflow question
I have in the didSelectRowAtIndexPath method the following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:nil bundle:nil] autorelease];
viewController2.title = #"test";
[self pushViewController:viewController2 animated:YES];
}
If I have in the last line "self.navigationController" (as one normally would if this is a UITableViewController) instead of "self", nothing happens (of course, since self is the UINavigationController) but when I have "self" as it currently is, the view of the viewController# is not pushed, however something is happening because the navigation bar has changed with "test" appearing, but the fixed header and UITableView remains. More interestingly, when I tap the table view for the first time, only "test" appear in the navbar but not the "Back" button. I have to tap the rows another time (since the UITableView remains after I tap the first time) for the "Back" button to show in the navbar...
so in short
1) what should I do to make the view of viewController2 shows when i tap on the table view row?
2) how do I get the "Back" button to show at the first tap?
my init method of the UINavigationController is as follow:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self.navigationController setNavigationBarHidden:NO];
UIView *mainView = [[[UIView alloc] initWithFrame:CGRectMake(0, statusBarHeight+navBarHeight, 320, 480-statusBarHeight-navBarHeight-tabBarHeight)] autorelease];
UIImage *stripedBackground = [UIImage imageNamed:#"bg.png"];
UIImageView *background = [[[UIImageView alloc] initWithImage:stripedBackground] autorelease];
background.frame = CGRectMake(0, 0, 320, mainView.frame.size.height);
[mainView addSubview:background];
UIImage *header = [UIImage imageNamed:#"header.png"];
UIImageView *headerView = [[[UIImageView alloc] initWithImage:header] autorelease];
headerView.frame = CGRectMake(0, 0, header.size.width, header.size.height);
[mainView addSubview:headerView];
UITableView *streamTableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, header.size.height, 320, mainView.frame.size.height-header.size.height) style:UITableViewStylePlain] autorelease];
[streamTableView setDelegate:self];
[streamTableView setDataSource:self];
[mainView addSubview:streamTableView];
[header release];
[self.view addSubview:mainView];
}
return self;
}
Related
I've a sample project, where I have created a custom UIWebView named WebView. They are added to the view in a UIViewController. Two WebView's are initialized in the viewDidLoad and added to an array list. The first one is added as subview på self.view. The Storyboard contains a UIBarButton, and when this is tapped, the second WebView is added as subview to the self.view.
- (void)viewDidLoad
{
[super viewDidLoad];
WebView *webView1 = [[WebView alloc] initWithFrame:self.view.bounds];
WebView *webView2 = [[WebView alloc] initWithFrame:self.view.bounds];
self.webViews = [NSArray arrayWithObjects: webView1, webView2, nil];
UIWebView *webView = [self.webViews objectAtIndex:0];
[self.view addSubview:webView];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://m.google.com/"]]];
}
Here is the implementation of the WebView where a toolbar is added to the bottom (-50px) of the view:
#implementation WebView
- (void) initToolbar {
UIToolbar *toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlackTranslucent;
toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[toolbar sizeToFit];
toolbar.frame = CGRectMake(0, self.frame.size.height-80, 320, 30);
[self addSubview:toolbar];
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self initToolbar];
}
return self;
}
#end
The problem is, that the toolbar does not have the same position in the two views (they could be positioned like the first one). Why are they not positioned equally?
You can download the small sample project here:
http://uploads.demaweb.dk/UIWebView.zip
My notes: As mentioned, the two WebView's are currently initialized in the viewDidLoad. If I instead wait and first initialize then when I need them, it seems to work as expected.
The thing here is that self.frame.size.height on viewDidLoad is not the same when you initialize it via the button click. If you add the UIWebView directly in viewDidLoad, the UINavigationBar of your UINavigationController is not loaded yet and the frame of self.view is 'perceived' to be (not really, because it is) larger.
This explains the difference of 40.0 (?) points.
One possible solution to this problematic behaviour is to initialize and add (at least the first) instance of UIWebView in the viewWillAppear:animated method. (UIViewController does already implement this method)
I'm trying to create a popover with the following code:
//.h
#interface LoginViewController : UIViewController <UIPopoverControllerDelegate>
....
#end
//.m
- (IBAction)popoverTest:(id)sender
{
UIViewController *popoverContent = [[UIViewController alloc] init];
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
UILabel *nameLabel = [[UILabel alloc] init]; //edited, fixed UILabel allocation
nameLabel.text = #"Person's name";
[nameLabel sizeToFit];
nameLabel.frame = CGRectMake(10, 10, nameLabel.frame.size.width, nameLabel.frame.size.height);
[myView addSubview:nameLabel];
popoverContent.view = myView;
popoverContent.contentSizeForViewInPopover = CGSizeMake(300, 300);
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverController.delegate = self;
[popoverController presentPopoverFromRect:((UIButton *)sender).frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
NSLog(#"ran all code");
}
I created a UIView, put a label as a subview and then assigned my view to the UIViewController.view. Then I created a popover controller, sized the popover controller, set the delegate and presented it from the button's frame.
I receive a SIGABRT and the app crashes.
Is there something I'm missing?
EDIT: I fixed the UILabel allocation. The problem is always there.
Your code is quite strange.
For example why do you create a view and do you add it to the content view of your popover?
Then, you have to make attention to memory leaks. There a lot of them in your code.
That said, here a simple example for display a UIViewcontroller within a UIPopoverController.
- (IBAction)yourAction:(id)sender
UIButton* senderButton = (UIButton*)sender;
UIViewController* yourController = [[UIViewController alloc] init];
UIPopoverController* pop = [[UIPopoverController alloc] initWithContentViewController:yourController];
pop.delegate = self;
pop.popoverContentSize = CGSizeMake(300, 300);
self.popover = pop;
[pop presentPopoverFromRect:senderButton.bounds inView:senderButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
pop.passthroughViews = nil;
[yourController release];
[pop release];
}
where self.popover is a #property with a retain policy. In this manner in UIPopoverControllerDelegate methods (or wherever you want), you can release your popover and/or dismiss it.
Hope it helps.
P.S. Check the code because I've written by hand.
Edit
Usually when you create a popover, its content view controller is or a custom UIViewController or a UINavigationController (in this case you want to take advantage of its navigation bar).
For example, instead of the simple UIViewController, you could create a custom one
//.h
#interface CustomUIViewController : UIViewController
#end
//.m
#implementation CustomUIViewController
// other code here...
- (void)viewDidLoad
{
// here you are sure that the view has been loaded in memory (alternatively override loadView method), so
UIView* greenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
green.backgroundColor = [UIColor greenColor];
[self.view addSubview:greenView];
[greenView release];
}
#end
and use it within a a popover
- (IBAction)yourAction:(id)sender
UIButton* senderButton = (UIButton*)sender;
CustomUIViewController* yourController = [[CustomUIViewController alloc] init];
// same as before...
}
solved my issue from this question.
short answer: ARC doesn't retain the UIPopoverController.
i made it an instance variable and works just fine.
The problem seems to be the following:
UILabel *nameLabel;
nameLabel.text = #"Person's name";
nameLabel is uninitialized. It points to random memory, and when calling -setText: through the text-property, your application crashes.
Apart from this, you have a few memory leaks. Remember: If you call alloc, you have to release it somewhere.
(I'm not sure how this works with the new automatic alloc/release).
having difficulty getting UIButton Items to show.. I want to place several UIButtons within my UIPopUpController. I'm doing everything in a storyboard and I need to do this section programmatically.
- (void)showPopover:(id)sender
{
if(![popoverController isPopoverVisible])
{
myPopOver = [[PopUp alloc] init];
popoverController = [[UIPopoverController alloc] initWithContentViewController:myPopOver] ;
[popoverController setPopoverContentSize:CGSizeMake(300, 200.0f)];
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
CGRect rect = (CGRect)[sender frame];
rect.origin.y = self.view.frame.size.height - rect.size.height;
[popoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else
{
[popoverController dismissPopoverAnimated:YES];
}
}
You could use a UINavigationController (it has already a navigation bar where you can attach buttons items) and add it as the content of your UIPopoverController. The UINavigationController can be istantiated using the initWithRootViewController method. In this case the controller would be your Popup class. For example, inside your showPopover method, you could do the following:
PopUp* myPopOver = [[PopUp alloc] init];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:myPopOver];
popoverController = [[UIPopoverController alloc] initWithContentViewController:navController];
// do other stuff here to present you UIPopoverController
[myPopOver release];
[navController release];
Now, inside your PopUp class, within viewDidLoad method, you can customize the UINavigationController navigationBar. For example:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *aButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(somethingSelector:)];
self.navigationItem.rightBarButtonItem = aButtonItem;
[aButtonItem release];
}
where somethingSelector is like the following:
- (void)somethingSelector:(id)sender
{
// your custom actions
}
EDIT
Alternatively you can avoid to use the UINavigationController and create UIToolbar inside your PopUp class. Inside the UIToolbar you can attach UIBarButtonItems. See UIToolbar Class Reference
I create a UIViewController (a custom calendar and its size is 550x440) and when i push a button it must be appear; the problem is that if I use setModalPresentationStyle and setModalTransitionStyle they change size of my view; can I set the size for these presentation?
I find a solution:
[self presentModalViewController:calendar animated:YES];
calendar.view.superview.frame = CGRectMake(0, 0, 200, 200);
Maybe you want to change your design to implement your calendar as a popover:
// Define the size of the calendar view controller for the popover
UIViewController *viewController = [[UIViewController alloc] init];
viewController.contentSizeForViewInPopover = CGSizeMake(550.0f, 440.0f);
viewController.delegate = self;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
// Create the popover
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:navigationController];
// Present the popover from one button
[popoverController presentPopoverFromBarButtonItem:button permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
//release the popover content
[viewController release];
[navigationController release];
I have a very tricky problem and after long searching (google, stackoverflow, ...) i didn't get the solution that works for me.
Let me introduce you in my current choosen architecture:
I have a a AppDelegate, which has a UIView that contains a UINavigationController and the application didFinishLaunchingWithOptions: contains:
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 400)];
UIViewController *myController = [[UIViewController alloc] init];
myController.view = myView;
FSCScrumRootView * myRootView = [[FSCScrumRootView alloc] initWithNibName:#"FSCScrumRootView" bundle:[NSBundle mainBundle]];
[myController.view addSubview:myRootView.navigation.view];
[self.window addSubview:myController.view];
[self.window makeKeyAndVisible];
return YES;
}
In my FSCScrumRootView (inherits from UIViewController) i init the view like this:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.navigation = [[[UINavigationController alloc] init] autorelease];
self.scrumProjectsList = [[[FSCScrumProjectListView alloc] init] initWithNibName:#"FSCScrumProjectListView" bundle:nil];
[navigation pushViewController:scrumProjectsList animated:YES];
[navigation view];
}
return self;
}
In my FSCScrumProjectListView (it is inherited from UITableViewController) i have implemented the viewDidLoad as following:
- (void)viewDidLoad
{
[super viewDidLoad];
//Set the title
self.navigationItem.title = #"Scrum Projects";
UIBarButtonItem *myRefreshButton = [[[UIBarButtonItem alloc] initWithTitle:#"Refresh" style:UIBarButtonSystemItemRefresh target:self action:#selector(refreshList)] autorelease];
self.navigationItem.leftBarButtonItem = myRefreshButton;
UIBarButtonItem *myLogoutButton = [[UIBarButtonItem alloc] initWithTitle:#"Logout" style:UIBarButtonSystemItemCancel target:self action:#selector(logout)];
self.navigationItem.rightBarButtonItem = myLogoutButton;
//Initialize the toolbar
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
//Set the toolbar to fit the width of the app.
[toolbar sizeToFit];
//Caclulate the height of the toolbar
CGFloat toolbarHeight = [toolbar frame].size.height;
//Get the bounds of the parent view
CGRect rootViewBounds = self.parentViewController.view.bounds;
//Get the height of the parent view.
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
//Get the width of the parent view,
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
//Create a rectangle for the toolbar
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
//Reposition and resize the receiver
[toolbar setFrame:rectArea];
//Create a button
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]
initWithTitle:#"Info" style:UIBarButtonItemStyleBordered target:self action:#selector(info_clicked:)];
[toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
//Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];
//Reload the table view
[self.tableView reloadData];
}
This results finally in the following screen (as i'd like to have it):
View iOS Mockup of current result
The Problem:
My Problem now is, that i can click ONLY on the Refresh Button. The other two buttons (Info and Logout) cannot be clicked. And i don't understand why? What I am doing wrong here?
Your help is kindly appreceated!
Try autoreleasing the second two buttons like your first one (the refresh).
UIBarButtonItem *myLogoutButton = [[[UIBarButtonItem alloc] initWithTitle:#"Logout" style:UIBarButtonSystemItemCancel target:self action:#selector(logout)]autorelease];
UIBarButtonItem *infoButton = [[[UIBarButtonItem alloc]
initWithTitle:#"Info" style:UIBarButtonItemStyleBordered target:self action:#selector(info_clicked:)]autorelease];
People, i have found the reason for my problem and i have to say, it was a very very silly one.
The first line of this project was responsible for all the troubels:
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 400)];
Since the View i created there is only sized 200x400, it is to small to recognize any events that appear in the outside of this view (although everything is visible).
If i change the size of this view, all works as expected:
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 500)];
But the more dynamic solution would probably be:
CGRect cgRect =[[UIScreen mainScreen] bounds];
CGSize cgSize = cgRect.size;
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cgSize.width, cgSize.height)];
Maybe there is an even better solution for getting the dynamic screen size?