I have two problems with the UINavigationController.
Problem 1
On the initial view I programmatically added a rightBarButtonItem.
When I push the navigationView to the right, everything works right and it appears a back button with the title of the previous view.
When I click the back button, I get back. But then this button is gone. But it's not really gone, it's just invisible, because it can still be clicked.
Problem 2
When I push the viewController on the right, I can't set the title. I tried it before pushing the view with
myViewController.title = #"someTitle";
and I tried it also inside of the viewController with
self.title = #"someTitle";
but nothing works here.
I found the problem:
I was adding a custom background image to the navigationBar. Somehow this image got over the title / button.
So if you ever want to implement a custom background image, don't user this:
UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[backgroundImage setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"headerBg" ofType:#"png"]]];
[navigationController.navigationBar insertSubview:backgroundImage atIndex:0];
[backgroundImage release];
But instead, use this:
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"headerBg.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
Problem 1:
Sounds odd. Don't really have an answer for that :-/
Problem 2:
Can you provide some more code snippets on this, like the whole method bodies where you tried to set the title?
Related
This is more like a design question but does anyone know how they made the Logo in the UINavigationBar look that good in the native iPhone app?
Does anyone know a tutorial?
Thanks in advance!
A UIViewController's -navigationItem property has itself a property called -titleView.
Set that titleView property of the currently displayed UIViewController's navigationItem to an embossed, semi-transparent icon like Twitter has done, and you're set.
You will load an image, and then put it in the navigationItem.titleView property on the view controller that will be inside the navigation controller.
UIImage *image = [UIImage imageNamed: #"header_logo.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
viewController.navigationItem.titleView = imageView;
Basically both answers were correct.
I'm just typing the code I've used. I like it better than #stevenhepting 's code :)
Just put this code in the viewDidLoad: method
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourPicture"]];
The perfect size for your image (in my opinion) is 80x40 pixels (width=80 / height=40).
I have a custom image header ABOVE the uinavigationbar. I do this with this code:
self.navController.view.frame = CGRectMake(0.0, 0.0, 320.0, 480.0);
self.navController.navigationBar.frame = CGRectMake(0.0, 73.0, 320.0, 44.0);
UIView *checkNav = [self.navController.view viewWithTag:9999];
if (checkNav == nil) {
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"header-logo.jpg"]];
imgView.tag = 9999;
[imgView setFrame:CGRectMake(0.0, 23.0, 320.0, 50.0)];
[self.navController.view addSubview:imgView];
}
This works great. But, when the app goes inactive (hit the main iphone home button) and then you go back into the app, the uinavigationbar shifts up to the default place at the top of the screen and is hidden behind my custom header image.
I've tried throwing code into applicationDidBecomeActive, but it doesn't help. Any help would be greatly appreciated.
Thanks!
You do get warned about this in the docs:
With only a few exceptions, you should never modify the navigation bar object directly. It is permissible to modify the barStyle or translucent properties of the navigation bar but you must never change its frame, bounds, or alpha values directly
Having said that, you should be able to get the effect by repeating your above code in applicationDidBecomeActive or applicationWillEnterForeground, assuming you have a pointer to the navigation controller from there. You may find that hiding and re-showing the navigation bar is required (bit of a hack, but it can help - I had a similar problem with hiding / showing the status bar and the navigation bar not going to its correct place)
What about putting the code into viewWillAppear?
Basically I want a custom UINavigationBar. I don't want it to be "translucent" or anything, like the pictures app.
I basically want to completely remove it, but I still want to be able to add back buttons and such when navigation controllers are pushed, and I want the views (EG: UITableViewController) to be pushed down below it.
Like this:
Any ideas how to achieve this at all?
Thanks
#implementation UINavigationBar (background)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:#"navigationbar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
basically, its not completely see through - its a visual lie. The only way to do it realistically is to override UINavigationBar's drawRect: method, as shown above.
To see through the UINavigationBar, if you choose to have one, just:
self.navigationController.navigationBar.translucent=YES;
You'll have to change the tint/color to match the background if you want it to appear like the image you posted.
At the beginning of your AppDelegate subclass UINavigationBar as below:
#interface CustomNavBar : UINavigationBar
#end
#implementation CustomNavBar
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:#"bar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
and then in the AppDelegate do this magic:
//Set custom NavigationBar
[self.navController setValue:[[CustomNavBar alloc]init] forKeyPath:#"navigationBar"];
//Set tint to match bar.png
self.navController.navigationBar.tintColor = [UIColor colorWithRed:0.93 green:0.43 blue:0 alpha:1];
//Set font for NavigationBar
[self.navController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"Comfortaa-Bold" size:20], UITextAttributeFont, nil]];
That should give you a lot more control over UINavigationController look & feel.
Hard to tell, could be the UINavigationBar is there and color matches the UIView background or, there is no UINavigationBar, just a view with custom buttons and UILabel on top. Pick an approach and code it, or ask the question again with more specifics.
I'm overwriting UINavigationController to replace the default navigationBar property with an instance of my own subclass of UINavigationBar. So I tried something like
_navigationBar = [[SBNavigationBar alloc] init];
in my -initWithRootViewController:. But that didn't work out as I expected it. There's still the default navigationBar being displayed.
So what's the best point to overwrite the navigationBar?
Thanks in advance
–f
If you check the documentation http://developer.apple.com/iphone/library/documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html, you'll see that the navigationBar property is read-only.
To have a custom navigationBar you can use Categories for example. You will find many questions answering this here on stackoverflow.
One simple version is putting an image in drawRect: like so...
#implementation UINavigationBar (Custom)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"bg_toolbar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
UIImageView *navBarImageView = [[UIImageView alloc] initWithImage:navBarImage];
[navBarImageView setFrame:CGRectMake(0, 0, 320, 44)];
self.navigationItem.titleView = navBarImageView;
[navBarImageView release];
I am trying to add an image to my navigationBar, but when I used the code as listed above, it places the image into the navigationBar but leaves a grey gap to the left and right of the image. The image was created at 320x44, I'm not sure why it is resizing it despite the fact that I am setting the frame.
That's... probably not how you want to do that—it's not what the titleView is for. The usual way to do a custom navigation-bar background is to create a category on UINavigationBar, like so:
#implementation UINavigationBar(MyCustomBackground)
- (void)drawRect:(CGRect)r
{
[[UIImage imageNamed:#"my-navigation-background.png"] drawInRect:self.bounds];
}
#end
Throw that in a .m file in your project and you should see all of your navigation bars use "my-navigation-background.png" as their background.