Revert navigation bar's background image to default - ios

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;

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?

How to make navigation bar background color to clearColor?

My profile view controller is embedded inside the Navigation controller and it shows up with coloured navigation bar. But I want to avoid that or how to make navigation bar background color to clearColor. So that it can show the images below it eg. Twitter app
Try this code:
[self.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
try this
[self.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;
in swift
self.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.translucent = true
It is very sample,
You can try:
YourNavigation.navigationBar.barTintColor = [UIColor clearColor];
YourNavigation: Enter your NavigationController.
If this can help, you can mark for me!
Thanks
Put this line in AppDelegate's didFinsihLaunchingWithOptions:
[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; // This is for clear navigation bar
[[UINavigationBar appearance] setShadowImage:[UIImage new]]; // this for remvove line under the navigationbar
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
You can't make navigation bar as clear . Just add you custom view then make it clear.

Different UINavigationBar background for different views

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.

Double Navigation Bar when I set translucent to NO

When i set this code in viewDidLoad:
[self.navigationController.navigationBar setTranslucent:NO];
and run the project in xcode, it creates other nav bar above of my main nav bar.
Anyone know what is it?
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
The code above set the nav bar from translucent to solid without hide or create an other nav bar.

Extend app background image to Navigation Bar and Status Bar

I am trying to get an effect like this:
http://dribbble.com/shots/1473643-Fashion-app/attachments/219674
Basically I would like to extend the app background image to the Navigation Bar and the Status Bar.
Any ideas how to do this.
The following lines of code inside viewDidLoad of your vc will help you to have a similar effect:
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
[self.navigationController.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
self.navigationController.navigationBar.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];

Resources