This is my code:
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
}
but navigationBar is still gray, Why?
In a new project this code is ok.
I have changed color using following code.
self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"greenPatter.png"]];
CGRect bgFrame = self.navigationController.navigationBar.bounds;
bgFrame.origin.y -= 20.0;
bgFrame.size.height += 20.0;
UIView *backgroundView = [[UIView alloc] initWithFrame:bgFrame];
backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"greenPatter.png"]];
backgroundView.alpha = 0.6;
[self.navigationController.navigationBar addSubview:backgroundView];
[self.navigationController.navigationBar sendSubviewToBack:backgroundView];
[self.navigationController.navigationBar setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
greenPatter.png is image of greencolor, You can also replace
self.navigationController.navigationBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"greenPatter.png"]];
to
self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
It's working for me. Try and Let me know.
我可能会说清楚,但您尝试过吗?
[self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
To set transparent I use this code
- (void)viewWillAppear:(BOOL)animated {
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
[super viewWillAppear:animated];
}
Then to get color back
- (void)viewWillDisappear:(BOOL)animated {
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = nil;
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
[super viewWillDisappear:animated];
}
A fully transparent navigation bar:
- (void)transparentNavigationBar {
UINavigationBar *navBarAppearance = [UINavigationBar appearance];
[navBarAppearance setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
[navBarAppearance setShadowImage:[UIImage new]];
[navBarAppearance setTranslucent:YES];
}
If you want to make it semi-transparent set translucent to NO and set desired batTintColor with alpha < 1.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.