self.navigationItem.titleView does not work with tabbed application template - ios

I am new to developing for iOS, but I am completely stumped with this.
Steps:
In Xcode, create a new tabbed application for iPhone.
Go into first subview and drag a Navigation Bar to the view.
Go into viewDidLoad and add this (assuming you have dropped logo.png into the project structure):
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
Render the view - it does not work. No custom image replaces the default "Title" text.
I don't understand. Why does this not work? What do I have to do to make it work? Is there something fundamentally different I need to be doing or a concept I am not grasping fully here?
UPDATE
I have figured out that the code above works. You just need to embed your view inside a navigation controller. Click on the first tabbed view, then do Editor > Embed In > Navigation Controller. The code will then work, and you can continue moving forward. Just embed each tab in a navigation controller using the method above and you should be good to go!

The code you have will work if your controller is embedded in a navigation controller, but if you add a navigation bar manually, you need to make an IBOutlet to it (bar in my example), and get its navigation item,
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationItem *item = self.bar.items[0];
item.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]];
}

May be the problem you are facing is because of not setting the frame. I face similar problem sometimes. Try this:
UIImageView *customTitleView = [[UIImageView alloc]initWithFrame:CGRectMake((320-210)/2, 0, 210, 50)];
customTitleView.backgroundColor = [UIColor redColor];
self.navigationItem.titleView = customTitleView;
Hope this helps. :)

Related

UINavigationController's nav bar seems invisible, but title and buttons are still visible

I've run into a weird situation. This is all done in code - no Interface Builder.
I'm creating a UIViewController and adding some content to it:
UIViewController* popoverViewController = [[[UIViewController alloc] init] autorelease];
UIView* popoverContentView = [[[UIView alloc] init] autorelease];
popoverContentView.backgroundColor = [UIColor blackColor];
// Add some stuff to popoverContentView
popoverViewController.view = popoverContentView;
I then create a UINavigationController, set its root view controller to the UIViewController from above, and add a title and a button to the navigationItem:
UINavigationController* popoverNav = [[[UINavigationController alloc] initWithRootViewController:popoverViewController] autorelease];
popoverViewController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissPopover)] autorelease];
[popoverViewController.navigationItem setTitle:#"MY TITLE"];
Then I set up a UIPopoverController with the UINavigationController in it and present it:
self.popoverController = [[[UIPopoverController alloc] initWithContentViewController:popoverNav] autorelease];
self.popoverController.delegate = self;
[self.popoverController setPopoverContentSize:CGSizeMake(320, 216) animated:NO];
[self.popoverController presentPopoverFromRect:self.cell.frame inView:self.popoverParentView permittedArrowDirections:UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown animated:YES];
The problem is that everything appears correctly with one exception: the navigation bar is invisible, but the title text and the bar button still show up and work correctly. I've tried messing with the bar's hidden and tintColor properties and changed the size of the popover, but nothing changes.
I'm sure I'm missing something obvious, but I can't see it.
I have other examples of similar things in my project's codebase, but those appear correctly. Any ideas as to why this is happening or how I could fix it?
EDIT
Not sure if the following will help, but I'm hoping it will provide some clues as to what's really going on here to someone who's seen something like this before.
I pushed a new (blank) view controller onto popoverNav just to see what would happen. It pushes and animates perfectly. Everything looks right except that the nav bar is still transparent and the bar button items are pushed to the top of the view.
Perhaps not the main cause of the problem, but it looks like the UIDatePicker is clipped by the CGSize you have setup. As far as I can remember, the default height for the picker is 216 (which you have) but you haven't taken into consideration the required height for the UINavigationBar.
I'm not sure why the background of the view looks to be some kind of textured grey unless this is set in your UIViewController but it doesn't appear that way from the code you pasted. This certainly isn't a default texture for iOS though.
Perhaps try and make your CGSize a little larger to accommodate the contents of the view as it looks as though the "Done" button is hugging the top right corner where there should be a fair few points worth of spacing around this by default.
Also, is there a reason why you're creating a new view and assigning it as the view property of your view controller?
UIView* popoverContentView = [[[UIView alloc] init] autorelease];
popoverContentView.backgroundColor = [UIColor blackColor];
// Add some stuff to popoverContentView
popoverViewController.view = popoverContentView;
Why not add the content as subviews of the view instead?

placing a view right behind my tab bar

I have a tab bar with 3 buttons, each of which loads a different controller and hence a different view.
I would like to place a UIView right behind my tab bar so that it is visible on all 3 different sub-controllers.
How can I achieve that?
It's pretty easy (this is using a storyboard) :
• Create a subclass of UITabBarController (I'll call it "TabViewController").
• In your storyboard, select your UITabBarViewController, and give it the class `TabViewController (on the right bar, 3rd section, custom class).
• In your TabViewController.m file, use this code :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIView *theView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
theView.backgroundColor = [UIColor redColor];
[self.view addSubview:theView];
[self.view bringSubviewToFront:self.tabBar];
}
You can do whatever you want with theView before you add it to self.view, here I just create a 50x50 red square at the position (50, 50). The view will stay on top of everything else !
Run & have fun !
add that green banner on Window in appDelegate.
[self.window addSubview:greenBannerView];

UIView appearing before navigation transition is over

I'm writing an iPhone app with a table inside a navigation controller. When the user clicks one of the cells in the main screen a UIView on top of the incoming view controller is created (it's like a toolbar).
self.toolbar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
toolbar.backgroundColor = [UIColor colorWithRed:0.8823 green:0.8823 blue:0.8823 alpha:1.0];
[self.navigationController.view addSubview:toolbar];
The problem is that the view appears before the transition to the new view controller is complete and the effect is pretty weird. I suppose this is due to the fact I add the view to the navigationController,but I need to do this otherwise the bar would scroll together with the table and instead I want it to be fixed.
Any suggestion?
I've found a possible solution: add the toolbar as TableHeaderView and follow iOS: Add UIView to UITableView
Any other better solution is more than welcome

UINavigationController nav bar overlaps contained TableView

I have a third party control that wants me to put a view inside of it. I'm trying to get a UINavigationController containing a series of table views inside of it, but when adding the controls the navigation bar overlaps the tableview by about half a row, which looks dumb.
Here's the code. I'm using the ArcGIS Server iOS SDK to put the navigation controller in a callout box on the map:
IdentifyResultsViewController *idWindow = [[IdentifyResultsViewController alloc] init];
idWindow.results = results;
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:idWindow];
map.callout.customView = nvc.view;
nvc.view.frame = CGRectMake(0, 0, 275, 400);
[map showCalloutAtPoint:self.mapPoint];
Is this a common problem using the UINavigationViewController, or should I look to the third party control?
I actually just had a similar problem with a third party control obstructing my navigation bar. I tried to look into the control but I'm not versed enough to unhide the navigation bar.
What I did may be something you can do too: instead of using the built in UINavigationBar, I kind of built one myself by just putting in a UIView at the top of the page and adding custom buttons to it that performed the functions that I wanted in the bar. This gives you a little more wiggle room around that third party control if you can't find what's causing the issue.
Hope it helps!
I solved this using some simple reordering of code - instead of using initWithRootViewController, I created the navigation view controller, set it's frame manually, and then pushed the view controller on to it:
IdentifyResultsViewController *idWindow = [[IdentifyResultsViewController alloc] init];
idWindow.results = [self filterResults:results];
UINavigationController *nvc = [[UINavigationController alloc] init];
nvc.view.frame = CGRectMake(0, 0, 275, 400);
[nvc pushViewController:idWindow animated:NO];
map.callout.customView = nvc.view;
[map showCalloutAtPoint:self.mapPoint];

Custom NavigationBar

I have a Navigation Controller as the root of my app and I am using the appearance proxy to customise the look on iOS 5, but for iOS 4 I was hoping to use a category to override drawRect:, this was fine, except that all the Navigation bars were affected as you would expect from a category.
I don't want to tamper with the "system" popups, such as the Mail composer, or the SMS composer, I want their bars to stay blue and system-like.
I Tried to create my own UINavigationController with it's xib and change the class of the NavigationBar to my custom sub lass of UINavigationBar. But the results are not taking affect at all on screen.
I am aware of the following post but couldn't get any solutions to run as expected.
How to subclass UINavigationBar for a UINavigationController programmatically?
My first attempt which does work but uses an undocumented setNavigationBar: method:
_myNavigationController = [[UINavigationController alloc] initWithRootViewController:someVC];
if([[BT_NavigationController class] respondsToSelector:#selector(appearance)]){
// some iOS 5 magic in here !
[_myNavigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"topbar.png"];
}else{
// Probably looking at app store refusal
CustomBar * bar = [[CustomBar alloc] init];
// [_myNavigationController setNavigationBar: bar];
[bar release];
}
[parentView presentModalViewController:_myNavigationController animated:YES];
To avoid that I created a UINavigationController, which I also had a xib for, reassigning the class of the navigation bar to my custom class, but placing breakpoints in drawRect: method, I can see that this isn't being called.
Why would that be, it seems that my code is not loading the nib, and therefore not realising the nab bar should be my custom class and not the UINavigationBar.
Any tips would be helpful, thanks.
If your modification is just about adding an image, you can simply insert it from the view controllers you want to customize:
UIImageView *bgImageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"topbar.png"]] autorelease];
[self.navigationController.navigationBar addSubview:bgImageView];
You have to tweak a little based on what other elements you have in your UINavigationBar for your UIImageView to be at the lowest index of the subviews but still above the background. Worked for us.
Good luck.

Resources