![](/img/trans.png)
[英]Add a UINavigationBar to a TableViewController presented modally?
[英]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.