5

当我将 a 推送到 a内的子类CNContactViewController堆栈时,顶部导航栏几乎完全隐藏。但是随着亮度一路上升,您可以看到后面的箭头,后面跟着“Detail”这个词,以及系统状态栏。当我点击屏幕的那个角落时, CNContactViewController 确实被解雇了。UITableViewControllerUINavigationController

在此处输入图像描述

当然这不好,因为用户可能甚至看不到导航栏的文本,现在按任何按钮来关闭。

有没有办法让 CNContactViewController 的导航栏色调与显示它的视图控制器(我的应用程序的其余部分)相同?

CNContactViewController *controller = [CNContactViewController viewControllerForUnknownContact:person];

controller.contactStore = [[CNContactStore alloc] init];
controller.delegate = self;
controller.allowsActions = NO;

[self.navigationController pushViewController:controller animated:YES];

我应该注意,我只在 iOS 10 上遇到这个问题,而不是低于 10 的版本。当我点击“添加到现有联系人”时,我也确实得到了正确着色的导航栏,但是当视图控制器被解雇时它再次中断.

在此处输入图像描述

再说一次,我的问题是:有没有办法让 CNContactViewController 的导航栏色调与显示它的视图控制器(我的应用程序的其余部分)相同?

4

2 回答 2

2

您的第二个屏幕截图显示了此问题的原因:您已将条形(或一般的条形按钮项)的色调颜色设置为白色。因此,它们在透明导航栏前面是白色的,在联系人视图控制器中是白色背景。

您无法直接对条形颜色执行任何操作,但您可以通过以下两种方式之一解决此问题:

  • 一种是让你的导航栏不透明。在这种情况下,联系人视图控制器的导航栏将是黑色的,并且您的白色栏按钮项将可见。

  • 另一种方法是在联系人视图控制器按下时更改导航栏的色调颜色(不是栏色调颜色,而是向下传达给其栏按钮项的色调颜色),并在弹出时将其更改回来。

编辑好的,我看到还有一个问题,因为 New Contact 视图控制器是呈现在您面前的另一个视图控制器。如果您拒绝放弃白条按钮项目设置,您将不得不在推送联系人视图控制器时使用外观代理将 UIBarButtonItem 色调颜色设置为其他颜色,然后在导航控制器时将其重置为白色委托告诉您用户正在弹回您的视图控制器。

于 2016-11-15T01:42:57.637 回答
1

我花了几个小时与 CNContactViewController 搏斗,试图强制它适应我的 UINavigation 外观设置,但它不会。它有自己的外观和感觉。我查看了 Mail 和 Notes 等 iOS 应用程序,看看它们如何显示 CNContactViewController 并且它似乎显示为弹出框,所以我也这样做了。

即使这也不是微不足道的。CNContactViewController 必须包装在 UINavigationView 中,以便创建新联系人和其他视图可以推送。如果您覆盖了 UINavigationBar 外观默认值,则需要在前后将它们设置回标准。这是它最终的样子:

@property (strong, nonatomic) CNContactViewController *contactViewController;
@property (assign, nonatomic) UIBarStyle saveAppearanceBarStyle;
@property (strong, nonatomic) UIColor *saveAppearanceBarTintColor;
@property (strong, nonatomic) UIColor *saveAppearanceTintColor;
@property (strong, nonatomic) UIColor *saveAppearanceBackgroundColor;
@property (strong, nonatomic) NSDictionary<NSAttributedStringKey, id> * saveAppearanceTitleTextAttributes;
@property (assign, nonatomic) BOOL saveAppearanceTranslucent;


- (IBAction)onAddToContactsButtonTapped:(id)sender

    self.contactViewController = [CNContactViewController viewControllerForUnknownContact: ... ]; // as before

    [self suspendNavBarAppearanceSettings];

    UINavigationController *popNav = [[UINavigationController alloc] initWithRootViewController:self.contactViewController];
    popNav.modalPresentationStyle = UIModalPresentationPopover;

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onAddToContactsDoneTapped:)];
    self.contactViewController.navigationItem.leftBarButtonItem = doneButton;

    [self presentViewController:popNav animated:YES completion:nil];

    UIPopoverPresentationController *popoverController = newNav.popoverPresentationController;
    popoverController.permittedArrowDirections = UIPopoverArrowDirectionAny;
    popoverController.sourceRect = ...;  // where you want it to point
    popoverController.sourceView = ...;  // where you want it to point
    popoverController.delegate = self;
}

- (void) suspendNavBarAppearanceSettings
{
    self.saveAppearanceBarStyle = [UINavigationBar appearance].barStyle;
    self.saveAppearanceBarTintColor = [UINavigationBar appearance].barTintColor;
    self.saveAppearanceTintColor = [UINavigationBar appearance].tintColor;
    self.saveAppearanceBackgroundColor = [UINavigationBar appearance].backgroundColor;
    self.saveAppearanceTitleTextAttributes = [UINavigationBar appearance].titleTextAttributes;
    self.saveAppearanceTranslucent = [UINavigationBar appearance].translucent;

    [UINavigationBar appearance].barStyle = UIBarStyleDefault;
    [UINavigationBar appearance].barTintColor = nil;
    [UINavigationBar appearance].tintColor = nil;
    [UINavigationBar appearance].backgroundColor = nil;
    [UINavigationBar appearance].titleTextAttributes = nil;
    [UINavigationBar appearance].translucent = YES;
}

- (void) restoreNavBarAppearanceSettings
{
    [UINavigationBar appearance].barStyle = self.saveAppearanceBarStyle;
    [UINavigationBar appearance].barTintColor = self.saveAppearanceBarTintColor;
    [UINavigationBar appearance].tintColor = self.saveAppearanceTintColor;
    [UINavigationBar appearance].backgroundColor = self.saveAppearanceBackgroundColor;
    [UINavigationBar appearance].titleTextAttributes = self.saveAppearanceTitleTextAttributes;
    [UINavigationBar appearance].translucent = self.saveAppearanceTranslucent;
}

- (void)onAddToContactsDoneTapped:(id)sender
{
    [self restoreNavBarAppearanceSettings];

    [[self presentedViewController] dismissViewControllerAnimated:YES completion:nil];
}

 #pragma mark - CNContactViewControllerDelegate

 - (void)contactViewController:(CNContactViewController *)viewController
        didCompleteWithContact:(nullable CNContact *)contact
 {
    [self restoreNavBarAppearanceSettings];

    [[self presentedViewController] dismissViewControllerAnimated:YES completion:nil];
 }

#pragma mark - UIPopoverPresentationControllerDelegate

- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popoverPresentationController
{
    // This method is only called if we are presented in a popover, i.e. on iPad
    // as opposed to full screen like on a phone.

    // on iPad you just tap outside to finish, so remove the Done button
    self.contactViewController.navigationItem.leftBarButtonItem = nil;
}

- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController
{
    [self restoreNavBarAppearanceSettings];
}
于 2017-10-25T15:10:22.090 回答