iOS navigationBar设置透明无效

时间:2015-12-16 10:21:18

标签: ios objective-c uinavigationcontroller

这是我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

但导航栏仍然是灰色的,为什么?

在新项目中,此代码没问题。

6 个答案:

答案 0 :(得分:2)

我使用以下代码更改了颜色。

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是greencolor的图像, 您也可以替换

self.navigationController.navigationBar.barTintColor =  [UIColor colorWithPatternImage:[UIImage imageNamed:@"greenPatter.png"]];

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

它为我工作。 试试让我知道。

答案 1 :(得分:0)

我可能会说明显的,但你试过这个吗?

@Context
private HttpServletRequest servletRequest;

final URI contextUri
     = URI.create(servletRequest.getRequestURL().toString())
    .resolve(servletRequest.getContextPath());

答案 2 :(得分:0)

以下代码表现良好。

UINavigationBar *bar =  [UINavigationBar appearance];
[bar setBackgroundImage:[UIImage imageNamed:@"navbg"] forBarMetrics:UIBarMetricsDefault];

enter image description here

答案 3 :(得分:0)

设置透明我使用此代码

- (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];
}

然后回颜色

- (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];
}

答案 4 :(得分:0)

AppDelegate.m class

中使用此代码
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"topbar-bg"] forBarMetrics:UIBarMetricsDefault];

enter image description here

答案 5 :(得分:0)

完全透明的导航栏:

- (void)transparentNavigationBar {

    UINavigationBar *navBarAppearance = [UINavigationBar appearance];

    [navBarAppearance setBackgroundImage:[UIImage new]
                                                  forBarMetrics:UIBarMetricsDefault];
    [navBarAppearance setShadowImage:[UIImage new]];
    [navBarAppearance setTranslucent:YES];

}

如果你想让它半透明设置半透明到NO并设置所需的batTintColor与alpha< 1。