rickytan / RTRootNavigationController

Implicitly make every view controller has its own navigation bar
MIT License
2.15k stars 379 forks source link

iOS15 如果配置了全局的导航器背景,后面针对某一个导航器修改背景没有效果 #302

Closed SpaceBlueFish closed 3 years ago

SpaceBlueFish commented 3 years ago

假如全局设置的背景为红色

if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *app = [UINavigationBarAppearance new];
         [app configureWithDefaultBackground];
         app.backgroundColor = UIColor.redColor;
        [UINavigationBar appearance].scrollEdgeAppearance = app;
        [UINavigationBar appearance].standardAppearance = app;
    }

某一个vc的viewDidLoad方法中,需要修改成白色,没有效果。

    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBackgroundImage:bgImg forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setClipsToBounds:NO];

后来我找到导航器中的背景图片对象,设置bgImgView.image的值,在viewDidAppear中执行可以修改,但滑动返回的时候又被系统重置了,感觉系统在出现和消失的时候都会重置导航器的全局默认属性值。

     for (UIView * view in self.navigationController.navigationBar.subviews) {
            for (UIView * resultView in view.subviews) {
                if ([resultView isKindOfClass:[UIImageView class]]) {
                    UIImageView * bgImgView = (UIImageView*)resultView;
                    bgImgView.image = bgImg;
                    return;
                }
            }
        }

-----------------原来每一个vc也有对应的Appearance,这样设置就行了

if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *app = [UINavigationBarAppearance new];
            [app configureWithDefaultBackground];
        app.backgroundImage = bgImg;
        self.navigationItem.scrollEdgeAppearance = app;
        self.navigationItem.standardAppearance = app;
        return;
    }
my-lsk commented 2 years ago

这样有点麻烦啊

Lee0820 commented 1 year ago

@rickytan 这个原因是框架没在 transferNavigationBarAttributes 判断里适配iOS 15吧

rickytan commented 1 year ago

可能是个问题

rickytan commented 1 year ago

新系统应该使用新方法设置每个 VC 自己的样式(非全局样式)。不过 transer 的问题也处理了,之前新系统的属性没有 transfer

Lee0820 commented 1 year ago

@rickytan transferNavigationBarAttributes 我觉得还是这个好用,因为大部分的需求还是,整体有默认的导航栏样式,少数的是不同的样式,虽然[RTRootNavigationController]旨在 每一个 VC 有自己单独的导航,但是每个VC都写一遍,还是有点麻烦 0.0,能支持配置默认样式,更好

rickytan commented 12 months ago

用 UIAppearance 统一设置就好