[英]iOS navigationBar Setting transparent without effect
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? 但是NavigationBar仍然是灰色的,为什么?
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 greenPatter.png是绿色的图像,您也可以替换
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. 如果要使其半透明,请将半透明设置为NO,然后将所需的batTintColor设置为alpha <1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.