鍍金池/ 問答/C  iOS  網(wǎng)絡(luò)安全/ 切換應(yīng)用時,設(shè)置iOS的導(dǎo)航欄的背景顏色失效

切換應(yīng)用時,設(shè)置iOS的導(dǎo)航欄的背景顏色失效

我想要改變指定控制器的導(dǎo)航欄的背景顏色。
切換應(yīng)用時,我設(shè)置的導(dǎo)航欄的背景顏色失效了,感覺被初始化了。
進(jìn)入App,設(shè)置沒問題。請看導(dǎo)航欄。
圖片0

切換應(yīng)用時,設(shè)置的導(dǎo)航欄的背景顏色失效
圖片1

搞不定。我列舉一下,試過的代碼:

1. 設(shè)置 statusBar.backgroundColor,

UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
     statusBar.backgroundColor = [UIColor redColor ];
}

2. 在 statusBar上插入子視圖.

 UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
 UIView * backgroundColorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 20) ];
 backgroundColorView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
 backgroundColorView.backgroundColor = [UIColor redColor ];
 [statusBar.subviews.firstObject insertSubview: backgroundColorView atIndex:0];

3. 插入 layer,也一樣, CALayer

我在Github上面,找到了實(shí)現(xiàn)的源碼,通過Runtime 實(shí)現(xiàn)的。 但是我的領(lǐng)導(dǎo),不準(zhǔn)許我使用Runtime實(shí)現(xiàn)。
問下 不使用Runtime 的實(shí)現(xiàn)方式?
感覺不難,就是我沒什么頭緒。
望解決。
:-)

回答
編輯回答
荒城

直接在 navigationBar 上添加 view,我這邊可以,LZ試下
UINavigationBar *navigationBar = self.navigationController.navigationBar;

    [navigationBar.superview insertSubview:self.navigationBarBGView belowSubview:navigationBar];
2018年1月8日 16:45
編輯回答
嫑吢丕
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    self.navigationBarBGView = [[UIView alloc ]initWithFrame: CGRectMake(0, 0, KScreenWidth, 20) ];
    self.navigationBarBGView.backgroundColor = [UIColor redColor ];
    [navigationBar.superview insertSubview: self.navigationBarBGView aboveSubview: navigationBar];

}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationBarBGView removeFromSuperview ];
    [super viewWillDisappear:animated];
}

系統(tǒng)的狀態(tài)欄,是 UIWindow層。

UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];

切換應(yīng)用時,設(shè)置iOS的導(dǎo)航欄的背景顏色,只需要改View 層,就好了。

Mr_Liu 的答案,適用于 導(dǎo)航欄隱藏的界面。

[self.navigationController setNavigationBarHidden:YES animated: animated ];
2017年4月23日 22:26