Different UINavigationBar background for different views - ios

I am creating my UINavigationBar in code like so:
// init and add root navigation controller to view
_rootNavigationController = [[UINavigationController alloc] init];
[_rootView.contentContainer addSubview:_rootNavigationController.view];
_rootNavigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
Then inside a certain UIViewController I want the UINavigationBar to have a clear background, so i use this code which works:
UINavigationBar *navBar = self.navigationController.navigationBar;
navBar.topItem.title = #"";
[navBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[navBar setShadowImage:[UIImage new]];
[navBar setTranslucent:YES];
[navBar setTintColor:[[AVOThemeManager sharedTheme] contentBackButtonColor]];
Once the user leaves this page by pressing back, I want to return my UINavigationBar back to how it origonally looked, so I am trying this:
- (void)viewDidDisappear:(BOOL)animated {
UINavigationBar *navBar = self.navigationController.navigationBar;
[navBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[navBar setShadowImage:nil];
[navBar setBarStyle:UIBarStyleBlackTranslucent];
[navBar setTranslucent:YES];
}
But this does not seem to work. I also do not want to set the UINavigationBar to be a new instance, and then set the properties, because it has a global button which is added in the root navigation controller, which the child view controllers don't know about.
Is there anything else I can try?

After posting, I immediately found the answer. Basically instead of using viewDidDisappear, I should use viewWillDisappear and it worked.

Related

iOS Make Navigation Bar transparent, while at the same time, preventing view to slide under Navigation Bar

My problem is, when I set the navBar to transparent with this code:
[[self navigationController] setNavigationBarHidden:NO animated:YES];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
[self.navigationController.navigationBar setTranslucent:YES];
.. the navigationbar hides the top of the viewcontroller's view:
I tried to prevent this with:
self.edgesForExtendedLayout = UIRectEdgeNone;
... but then, the navbar is not transparent anymore.
How can I achieve both?

Customise UI Navigation Bar and button

Hello I am new to ios development and want to know how to customise navigation bar buttons
This is what i used to change navigation bar color
[[UINavigationBar appearance] setBarTintColor:[UIColor]]
I want to change button colour
Here is a exmaple
First you need to set navigation bar, assuming your first screen is SpalshViewController you need to set splash screen as a root view controller for navigation bar and then set navigation bar as root view controller like :
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
SplashScreenVC *obj =[CustomUtility getViewController:#"SplashScreenVC"];
self.navigationController =[[UINavigationController alloc] initWithRootViewController:obj];
self.window.rootViewController =self.navigationController;
in didFinishLaunchingWithOptions
Now coming to customize navigation bar setting and appearance you need to set :
self.navigationController.navigationBar.translucent = NO;
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"top_bg2.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor],NSFontAttributeName:[UIFont fontWithName:#"Lato-Regular" size:18]}];
Now setting the custom bar buttons for navigation bar you need to create barbutton items like :
UIBarButtonItem *leftbtn;
UIBarButtonItem *rightbtn;
[self.navigationController.navigationItem setLeftBarButtonItem:leftbtn];
[self.navigationController.navigationItem setRightBarButtonItem:rightbtn];
customizebarbuttonItems According to your need
if you want to set more than one buttons to the navigation bar you can add like:
UIBarButtonItem *leftbtn1;
UIBarButtonItem *leftbtn2;
UIBarButtonItem *rightbtn1;
UIBarButtonItem *rightbtn2;
[self.navigationController.navigationItem setLeftBarButtonItems:#[leftbtn1,leftbtn2]];
[self.navigationController.navigationItem setRightBarButtonItems:#[rightbtn1,rightbtn2]];
Hope it will helpful for you
You can use below navigation bar to customize. In Storyboard you have option to change the colour.
Otherwise you can put UIView to customize. Like below,
Create a 1x1 pixel image with the color you want. Then add following code to your AppDelegate class.
UIImage *btnBgColor = [[UIImage imageNamed:#"backColor"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UIBarButtonItem appearance] setBackgroundImage: btnBgColor
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
OR
Write the below code on your base viewController class, then make this class as superclass of all your other view controllers.
UIButton* yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
yourButton.layer.backgroundColor = [UIColor redColor].CGColor;
UIBarButtonItem* buttonItem = [[UIBarButtonItem alloc] initWithCustomView: yourButton];
self.navigationBar.leftBArButtonItems = #[buttonItem];
You can add your custom button in navigation bar like:
UIButton *btnObj = [UIButton buttonWithType:UIButtonTypeCustom];
[btnObj setFrame:CGRectMake(0, 0, 60, 44)];
[btnObj setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[btnObj setImage:[UIImage imageNamed:#"<button background image>"] forState:UIControlStateNormal];
[btnObj addTarget:self action:#selector(btnObjClick:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *itemBtnObj = [[UIBarButtonItem alloc] initWithCustomView:btnObj];
self.navigationItem.rightBarButtonItems = [[NSArray alloc]initWithObjects:itemBtnObj, nil];
You can do it from story board itself.
Drag UINavigation Bar
Customise your navigation using images or background colors giving them shadows.
Add navigation item
you can customize those too using images or colors and images and position as per requirements.
To add constraints to the same refer my ans. Adding constraints to a UIBarButtonItem
Hope it helps.. Happy Coding.. :)

Do I need to embed a UINavigationController & Programmatically add UINavigationBar?

I don't see all of my navigation bar.
There is only a outline of where a nav bar would be.
I set the navigationBar Color and tint colors so as to define this nav bar even if it had no items.
I used Storyboards to make the currently functional UI.
My Story Board UI includes Delegates,Custom Collection View Cells,Custom Table View Cells,And Animations.
By the end of my app I wanted to add nav bars programmatically to each of my ViewControllers. (see code)
I have used this code in 6 other classes. And I Have 6 Perfectly Flawless navigation bars in those ViewControllers.
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizeralloc]
initWithTarget:self
action:#selector(titleGestureLabelMethod)];
UILabel * titleView = [UILabel new];
titleView.text = #"Title";
titleView.textColor = [UIColor greenColor];
[titleView sizeToFit];
titleView.userInteractionEnabled = YES;
[titleView addGestureRecognizer:tapGesture];
self.navigationItem.titleView = titleView;
[self.view addSubview:titleView];
[self.navigationController.navigationBar setTranslucent:NO];
UIBarButtonItem *lButton = [[UIBarButtonItem alloc] initWithTitle:#"L"
style:UIBarButtonItemStylePlain
target:sel
action:#selector(lButtonMethod)];
[self.navigationItem setLeftBarButtonItem:lButton animated:YES];
UIBarButtonItem *rButton = [[UIBarButtonItem alloc]
initWithTitle:#"R"
style:UIBarButtonItemStylePlain
target:self
action:#selector(rButtonMethod)];
[self.navigationItem setRightBarButtonItem:rButton animated:YES];
self.navigationController.navigationBar.tintColor = [UIColor redColor];
[self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]];
[self.view addSubview:testTwoNavBar];
}
You need to embed your view controller into a navigation controller. If you're using Interface Builder, from your storyboard select the view controller. In the menu select editor and choose embed in navigation controller. It's hard to understand what you really need though from this question.

Revert navigation bar's background image to default

At first, I set navigation bar's background to translucent.
barImage=[[self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault] copy];//barImage is a UIImage point
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
It works well.
Now I need to revert navigation bar's background image to default.
- (void)dealloc
{
[self.navigationController.navigationBar setBackgroundImage:barImage
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = nil;
}
This doesn't work. What should I do?
Not sure if you're still stuck on this, but here's the code to reverse the effect. I borrowed a navigation controller, and copied everything over to the original navigation controller.
UINavigationController *tempNavigationController = [[UINavigationController alloc]initWithRootViewController:[[UIViewController alloc]init]];
[self.navigationController.navigationBar setBackgroundImage:[tempNavigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [tempNavigationController.navigationBar shadowImage];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = tempNavigationController.view.backgroundColor;
tempNavigationController = nil;

Presented UINavigationController tint color not changing

MyVC *vc = [[MyVC alloc] init];
vc.delegate = self;
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
[nc.navigationBar setTintColor:[UIColor redColor]];
[self presentViewController:nc animated:true completion:^{}];
The navigation bar is black. I present this from a navigation controller inside a tab bar controller which is created in my App Delegate. In my ApplicationDidFinishLaunchingWithOptions, I can control the color of the navigation controllers there.
Why is this navigation controller tint bar black?
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
I would suggest you to use this if all of your NavigationBars are the same color. Appearance property of an object sets uniformity on the function you apply on it.
You can try to make custom class for UINavigationBar
.h
#interface CustomUINavigationBar : UINavigationBar {
}
#end
.m
#implementation CustomUINavigationBar
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor colorWithRed:0.023 green:0.14 blue:0.478 alpha:1];// for example
self.tintColor = color;
}
#end
Also you can try to insert the below code in the didFinishLaunchingWithOptions: of AppDelegate.m
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
In iOS7 you have to use barTintColor property
[nc.navigationBar setBarTintColor:[UIColor redColor]];
instead of
[nc.navigationBar setTintColor:[UIColor redColor]];

Resources