gpt4 book ai didi

ios - 自定义 UINavigationBar 外观以在 CNContactPickerViewController 中显示

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:04:53 24 4
gpt4 key购买 nike

我使用下面的代码来自定义我的 UINavigationBar 在显示 ContactPickerViewController 时的外观:

 id specialNavBarAppearance = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[CNContactPickerViewController class]]];

[specialNavBarAppearance setBarTintColor:[UIColor colorWithRed:213/255.0f green:38/255.0f blue:46/255.0f alpha:1.0]];
[specialNavBarAppearance setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,nil]];
[specialNavBarAppearance setTintColor:[UIColor whiteColor]];

但没有出现任何变化。我哪里错了?

最佳答案

我为 UIViewController 创建了一个类别,仅当您使用 Method swizzling 呈现 CNContactPickerViewController 时才自定义 UINavigationBar 的外观。在呈现之前,我检查呈现的是否是 CNContactPickerViewController,然后我们更改外观。然后在解雇时,我将外观重置为默认值。这是一个疯狂的解决方案,但它完成了工作。

#import "UIViewController+CustomAppearance.h"
#import <objc/runtime.h>
@import ContactsUI;

@implementation UIViewController (CustomAppearance)

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

SEL originalSelector = @selector(presentViewController:animated:completion:);
SEL swizzledSelector = @selector(presentViewController2:animated:completion:);

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);
}



SEL originalSelector2 = @selector(dismissViewControllerAnimated:completion:);
SEL swizzledSelector2 = @selector(dismissViewControllerAnimated2:completion:);

Method originalMethod2 = class_getInstanceMethod(class, originalSelector2);
Method swizzledMethod2 = class_getInstanceMethod(class, swizzledSelector2);

BOOL didAddMethod2 =
class_addMethod(class,
originalSelector2,
method_getImplementation(swizzledMethod2),
method_getTypeEncoding(swizzledMethod2));

if (didAddMethod2) {
class_replaceMethod(class,
swizzledSelector2,
method_getImplementation(originalMethod2),
method_getTypeEncoding(originalMethod2));
} else {
method_exchangeImplementations(originalMethod2, swizzledMethod2);
}

});
}

#pragma mark - Method Swizzling

- (void)dismissViewControllerAnimated2:(BOOL)flag completion:(void (^)(void))completion
{
[self setupDefualtAppearance];

[self dismissViewControllerAnimated2:flag completion:completion];
}



- (void)presentViewController2:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion {

if ([viewControllerToPresent isKindOfClass:[CNContactPickerViewController class]]) {
[self setupContactsPickerAppearance];

}
[self presentViewController2:viewControllerToPresent animated:flag completion:completion];
}


- (void)setupDefualtAppearance{

id specialNavBarAppearance = [UINavigationBar appearance];

[specialNavBarAppearance setBarTintColor:nil];
[specialNavBarAppearance setTitleTextAttributes: nil];
[specialNavBarAppearance setTintColor:nil];
}


- (void)setupContactsPickerAppearance{
id specialNavBarAppearance = [UINavigationBar appearance];

[specialNavBarAppearance setBarTintColor:[UIColor colorWithRed:213/255.0f green:38/255.0f blue:46/255.0f alpha:1.0]];
[specialNavBarAppearance setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,nil]];
[specialNavBarAppearance setTintColor:[UIColor whiteColor]];
}

@end

关于ios - 自定义 UINavigationBar 外观以在 CNContactPickerViewController 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38291683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com