简体   繁体   English

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

[英]Different UINavigationBar BarTint Color when presented Modally

Right now I am globally changing the bartint color in the AppDelegate like this. 现在,我正在像这样全局更改AppDelegate中的bartint颜色。

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

Is there a way to keep this, but globally alter the BarTintColor when those views are presented modally? 有没有办法保持这种状态,但是当这些视图以模态显示时,会全局更改BarTintColor?

So I decided to create a category that swizzled viewWillAppear to solve this. 因此,我决定创建一个使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.

相关问题 将UINavigationBar添加到以模态方式呈现的TableViewController中? - Add a UINavigationBar to a TableViewController presented modally? 将数据传递给视图控制器时,以模态方式呈现该控制器时是否有所不同? - Passing data to a view controller different when the controller is presented modally? 以模态呈现的UIViewController的背景颜色随机变化 - Background Color Randomly Changes of UIViewController presented Modally 模态显示viewcontroller时,用户可以滚动 - User can scroll when viewcontroller is presented modally 模态显示/关闭视图控制器时通知? - Notification when view controller is presented modally/dismissed? 更改选项卡时以模态显示的ViewController消失 - Dismiss ViewController presented modally when tab is changed 将以模态形式呈现的视图控制器转移给其他基础视图控制器 - dismiss a modally presented view controller to a different underlying view controller Tableview以模态和自动布局呈现 - Tableview presented modally and autolayout 半透明的UINavigationController,以模态显示 - Translucent UINavigationController, presented modally 在iOS7上模态显示时,以前的UIViewController会流血 - Previous UIViewController bleeds through when presented modally on iOS7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM