Settings the title from inside a UITableView - ios

I'm working on a simple application with 3 tabs at the moment, the first tab is a UITableViewController, the other 2 are UIViewControllers.
Now from inside both UIViewControllers, I can set self.title and the title of the navbar will change.
But if I try this from inside the UITableViewController, this doesn't work, why is this?
I'm also trying to set a custom background for the navigation bar, after checking some tutorials, I found that this should work, but it really doesn't
UIImage *image = [UIImage imageNamed:#"bgNavigationBar.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

self.navigationItem.title=#"Your Title";
should work, just put it in
- (void)viewDidLoad {
}
of the UITableViewController

Related

Can't set my navigation bar background image

I want to change my table view navigation bar background image to some png, i created the nav bar png with the exact sizes of a default nav bar (I checked in interface builder).
This is my code (in my table view controller class, viewDidLoad method):
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:#"nav.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
Shouldn't this work..?
this is my table view settings in the interface builder if this help:
the top one is what i suppose to get and the bottom one is what im getting:
thanks!
UIImageView *backImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"nav.png"]];
[self.navigationController.navigationBar addSubview:backImgView];
Try to add Image in custom view
Why not use the existing nav bar? Don t think you need to create a new one... This code is working for me :
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"nav.png"] forBarMetrics:UIBarMetricsDefault];
Your TableViewController must be "embadded" in a navigation controller a.k.a connected to other ViewControllers as push segue... Then you get your nav bar automatically instead of creating one... I mean you create a nav bar and never add it to the view isnt it?

UINavigation bar with custom image

I want Custom navigation bar with size =60 .And In this navigation bar i add my custom image to this.
I write this code..
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"1.png"] forBarMetrics:UIBarMetricsDefault];
help me thanks in advance
hope this will help you
ok , you want to customize the height of navigation bar(according apple designing guildelines you should ignored it but there are many beautiful apps in app store with customize navigation bar height).
the one way i got to know to customize the navigation bar by subclassing UINavigationBar and and overvide the following method
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize customizeSize = CGSizeMake(customizeWidth , customizeWidthHeight);
return customizeSize;
}
UIImageView *imgView = [[UIImageView alloc] initWithFrame:SOMEFRAME];
imgView.image = SOMEIMAGE;
self.navigationItem.titleView = imgView;
navigation bar has the property title view. U can add any Elements u want on it.

Custom UINavigationBar Background Working on all Navigation Controllers Except One

I have a series of view controllers connected by a root navigation controller. All of the view controllers are table views, except one which is a view controller.
I have a custom navigationBar image which I've applied in the App Delegate and it works great everywhere, except this one view controller. With this, when the segue happens (Push), it almost super imposes the custom background with another image and the result is a darker uinavigationBar.
My code in the App Delegate is:
UIImage *nav = [UIImage imageNamed:#"Purple.png"];
[UINavigationBar appearance] setBackgroundImage:nav forBarMetrics:UIBarMetricsDefault];
Does anyone have any ideas for what I can do to ensure this view controller maintains the correct custom UINavigationBar?
In the viewDidLoad of this view, I've even tried to set this:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"Purple.png"] forBarMetrics:UIBarMetricsDefault];
I also tried this in the viewWillAppear but nothing - still that darker image.
Any thoughts would be greatly appreciated!
you wrote this wrong
UIImage *nav = [UIImage imageNamed:"#Purple.png#];
replace that with this
UIImage *nav = [UIImage imageNamed:#"Purple.png"];

iOS 5 - 1 Custom UINavigationBar different than all others

I'm using UIAppearance in iOS 5 to create a custom UINavigationBar. I use the following code (inside AppDelegate.m) to give all of the navigation bars the same background
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"NavBarNoText.png"] forBarMetrics:UIBarMetricsDefault];
I'd like to be able to use 1 navbar "NavBarWithText.png" for only 1 TableViewController, while I use the NavBarNoText.png as the background for all of the other navigation bars except for said TableViewController. Does anyone know if this is possible? I know Instagram does something like it, with the home tab using a different navigation bar than all of the other tabs.
I've tried using this separately in the ViewDidLoad method in each separate class, but it doesn't seem to work. I get the single view to have a different background at first, but it changes back to the common background after I switch a view.
-(void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"NavBarWithText.png"] forBarMetrics:UIBarMetricsDefault];
}
For the single view
-(void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"NavBarNoText.png"] forBarMetrics:UIBarMetricsDefault];
}
For all of the other views.
You can use this appearanceWhenContainedIn: to show different navigationBar when displayed in UITableViewController for more information see here

Remove Background Image on UINavigationBar

My app uses a navigation controller with an image on in the navigation bar of the home page. The background image is done by putting
[navigationController.navigationBar setBackgroundImage:navImage forBarMetrics:UIBarMetricsDefault];
[_window addSubview:navigationController.view];
in the app delegate. The nav bar displays this image fine.
Every other view will not have an image in the navigation bar. Therefore, I need to remove this background image when any other view controller is pushed. Does anyone know how this can be done?
There are many answers online about adding or changing a nav bar image, but this question is a little bit different. Thank you in advance.
To remove UINavigationBar background in iOS 5 or later, you should do this:
[navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
Hope it helps
I believe this to be the first actual answer to this for iOS5, the main problem being the "removal" of the background image once you are done. Well, just retain the existing image and put it back when you are done.
#implementation MyViewController {
UIImage *_defaultImage;
}
- (void)viewWillAppear:(BOOL)animated {
_defaultImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"bar.png"] forBarMetrics:UIBarMetricsDefault];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.navigationController.navigationBar setBackgroundImage:_defaultImage forBarMetrics:UIBarMetricsDefault];
}
You can set background image with blank image created in runtime.
UIGraphicsBeginImageContextWithOptions(CGSizeMake(36, 36), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[UINavigationBar appearance] setBackgroundImage:blank forBarMetrics:UIBarMetricsDefault];
Creating blank image in runtime:
How to create a blank transparent png in Objective-C?
Nothing here have worked for me. After searching for hours, the only thing that worked was following a source code example provided by Apple.
I placed the following code and it worked and removed the navigation bar image from other view controllers finally!!
// Reset everything about navigation bar. (Other flows' background image used to show up on another view controller)
self.navigationController!.navigationBar.setBackgroundImage(nil, for: .default)
self.navigationController!.navigationBar.setBackgroundImage(nil, for: .compact)
self.navigationController!.navigationBar.barTintColor = nil
self.navigationController!.toolbar.barTintColor = nil
self.navigationController!.toolbar.isTranslucent = true
Hope this helps someone.
Cheers
It looks like the easiest way to do this is to:
1) set the alpha of the navigation bar on the home page to 0.000001
2) put the background image in a UIImageView "behind" the navigation bar (which is now clear)
3) use viewWillAppear / viewWillDisappear to set the navigation bar's opacity to either 1 or 0.00001 when views are pushed / popped.

Resources