这是我的代码:

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


但是NavigationBar仍然是灰色的,为什么?

在一个新项目中,此代码是可以的。

最佳答案

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

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是绿色的图像,
您也可以更换

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




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


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

关于ios - iOS navigationBar设置透明无效果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34309377/

10-14 16:59