繁体   English   中英

模态显示时不同的UINavigationBar BarTint颜色

[英]Different UINavigationBar BarTint Color when presented Modally

现在,我正在像这样全局更改AppDelegate中的bartint颜色。

[[UINavigationBar appearance] setBarTintColor:[UIColor greenColor]];

有没有办法保持这种状态,但是当这些视图以模态显示时,会全局更改BarTintColor?

因此,我决定创建一个使viewWillAppear混乱的类别来解决此问题。

@implementation UIViewController (IsModal)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];

        SEL originalSelector = @selector(viewWillAppear:);
        SEL swizzledSelector = @selector(extended_viewWillAppear:);

        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

        BOOL didAddMethod =
        class_addMethod(class,
                        originalSelector,
                        method_getImplementation(swizzledMethod),
                        method_getTypeEncoding(swizzledMethod));

        if (didAddMethod) {
            class_replaceMethod(class,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    });
}

- (void)extended_viewWillAppear:(BOOL)animated {
    [self extended_viewWillAppear:animated];
    [self styleIfModal];
}

- (void)styleIfModal {
    if([self isModal] && self.navigationController != nil) {
        [self.navigationController.navigationBar setBarTintColor:[UIColor grayColor]];
    }
}

- (BOOL)isModal
{
    return self.presentingViewController.presentedViewController == self || (self.navigationController != nil && self.navigationController.presentingViewController.presentedViewController == self.navigationController) || [self.tabBarController.presentingViewController isKindOfClass:[UITabBarController class]];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM