没有backgroundColor的UINavigationBar

时间:2015-01-20 00:19:40

标签: ios objective-c uinavigationbar

我想创建一个没有彩色背景的UINavigationBar

我试过

 self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];

 self.navigationController.navigationBar.backgroundColor = nil;

但无法解决问题。我还尝试为self.view.backgroundColor使用的导航栏设置相同的颜色,但我无法覆盖原始的颜色。 (我使用Storyboard可能导致问题,但不确定,因为我可以用代码操纵tintColor。)

2 个答案:

答案 0 :(得分:1)

self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                             forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.translucent = YES;

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
//[backButton setImage:[UIImage imageNamed:@"you_back_image"] forState:UIControlStateNormal]; this is the image you would use as a back image
[backButton setTitle:@"back" forState:UIControlStateNormal];
[backButton sizeToFit];
[backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButton;

然后编写方法goBack,这取决于你呈现或推送当前ViewController的事实。

- (void)goBack {
//[self dismissViewControllerAnimated:YES] 
//[self.navigationController popViewControllerAnimated:YES];
}

答案 1 :(得分:0)

在appDelegates中尝试这一个didFinishLaunchingWithOptions方法,它清除导航栏的背景颜色

[[UINavigationBar外观] setBarTintColor:[UIColor clearColor]];