简体   繁体   English

CNContactViewController 导航栏因版本而异

[英]CNContactViewController navigation bar different between versions

Our tint color is white.我们的色调是白色。 Our app uses CNContactViewController.我们的应用程序使用 CNContactViewController。 In our version of the app in the store built with Xcode 7 targeting iOS 8 and 9, if you were iOS 9 we called CNContactViewController.在我们的商店中使用 Xcode 7 构建的针对 iOS 8 和 9 的应用程序版本中,如果您使用的是 iOS 9,我们将调用 CNContactViewController。 The back button is white but has a gray navigation bar behind it.后退按钮是白色的,但后面有一个灰色的导航栏。 In our development build using Xcode 8 targeting iOS 9 and 10, there is no gray bar, so the back button is white on top of white and very hard to see the shadow.在我们针对 iOS 9 和 10 使用 Xcode 8 的开发版本中,没有灰色条,因此后退按钮是白色的,很难看到阴影。

Has anyone else experienced changes between Xcode versions/SDK versions that the navigation area of CNContactViewController has changed?有没有其他人经历过 Xcode 版本/SDK 版本之间的变化,CNContactViewController 的导航区域发生了变化? Might there be some other change in our app that would have affected this bar?我们的应用程序中可能会有其他一些影响这个栏的变化吗?

Edit: here is an image what it looks like in our latest build.编辑:这是我们最新版本中的图像。 I did delete some personal information so that's the boxes in the middle, but you can see at the top left its very hard to see the back button.我确实删除了一些个人信息,所以这是中间的框,但是您可以在左上方看到很难看到后退按钮。

在此处输入图片说明

Edit: this is how we set the colors throughout the app.编辑:这就是我们在整个应用程序中设置颜色的方式。 The white back button wouldn't be an issue if it also used the bar tint color of Red instead of nothing如果白色的后退按钮也使用红色的条形色调颜色而不是什么都不是问题

    UINavigationBar.appearance().barTintColor = UIColor.red
    UINavigationBar.appearance().tintColor = UIColor.white
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

The code we use to push this onto our existing navigation controller that has red bar and white buttons:我们用来将其推送到我们现有的具有红色栏和白色按钮的导航控制器上的代码:

let ucvc = CNContactViewController(forUnknownContact: contact)
ucvc.delegate = self
ucvc.allowsEditing = true
ucvc.allowsActions = true
ucvc.alternateName = name()
ucvc.contactStore = CNContactStore()
self.navigationController?.pushViewController(ucvc, animated: true)

I was having the exact same issue.我遇到了完全相同的问题。 It definitely seems like an iOS 10 bug.这绝对看起来像是 iOS 10 的错误。 Anyways, I found a work around by setting the navigation bar's translucency to false.无论如何,我通过将导航栏的半透明设置为 false 找到了解决方法。 Then set the background color of the application's main window to whatever color you want the navigation bar to be.然后将应用程序主窗口的背景颜色设置为您希望导航栏的任何颜色。

Some code snippets:一些代码片段:

UINavigationBar.appearance().isTranslucent = false
UIApplication.shared.delegate?.window??.backgroundColor = UIColor.red

I've solved it like this:我是这样解决的:

CNContactViewController *vc = [CNContactViewController viewControllerForContact:contact];
vc.delegate = self;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    for (UIView *view in [vc.navigationController.navigationBar subviews]) {
        view.tintColor = [UIColor darkTextColor];

        view.backgroundColor = [UIColor redColor];
    }
});

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

By using Debug View Hierarchy of XCode, I find the alpha of the subview named "_UIBarBackground" of UINavigationBar turns to be 0 after CNContactViewController has been pushed.通过使用 XCode 的调试视图层次结构,我发现 UINavigationBar 的名为“_UIBarBackground”的子视图的 alpha 在 CNContactViewController 被推送后变为 0。

The following code helps me solve the problem (It works well in iOS 11):以下代码帮助我解决问题(在 iOS 11 中运行良好):

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        for (UIView *view in self.navigationController.navigationBar.subviews) {
            if ([view isKindOfClass:NSClassFromString(@"_UIBarBackground")]) {
                view.alpha = 1;
                break;
            }
        }
    });

In Swift 5 and Xcode 10.2在 Swift 5 和 Xcode 10.2 中

In iOS 9.0 CNContactViewController navigation bar working properly, but not higher versions.在 iOS 9.0 CNContactViewController 导航栏工作正常,但不是更高版本。

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(0.1 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: {

   //Set status bar background colour
   let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
   statusBar?.backgroundColor = UIColor.red
   //Set navigation bar subView background colour
       for view in controller.navigationController?.navigationBar.subviews ?? [] {
          view.tintColor = UIColor.white
          view.backgroundColor = UIColor.red
       }
})

navigationController?.pushViewController(controller, animated: true)

Here i fixed status bar background colour and navigation bar background colour.在这里我修复了状态栏背景颜色和导航栏背景颜色。 If you don't want status bar colour comment it.如果你不想状态栏颜色评论它。

The complete code is完整的代码是

func addPhoneNumber(phNo:String) {
    if #available(iOS 9.0, *) {
        let store = CNContactStore()
        let contact = CNMutableContact()
        let homePhone = CNLabeledValue(label: CNLabelHome, value: CNPhoneNumber(stringValue : phNo))
        contact.phoneNumbers = [homePhone]
        let controller = CNContactViewController(forUnknownContact : contact)
        controller.contactStore = store
        controller.delegate = self

        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(0.1 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: {

            //Set status bar background colour
            let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
            statusBar?.backgroundColor = UIColor.red
            //Set navigation bar subView background colour
            for view in controller.navigationController?.navigationBar.subviews ?? [] {
                view.tintColor = UIColor.white
                view.backgroundColor = UIColor.red
            }
        })

        navigationController?.pushViewController(controller, animated: true)
    }
}

Your question has solved my problem: I now know why I have the same issue.你的问题解决了我的问题:我现在知道为什么我有同样的问题。

I have resolved it by setting navigationController.navigationBar.tintColor to a shade of blue just before pushing the CNContactViewController.我已经通过在推送 CNContactViewController 之前将 navigationController.navigationBar.tintColor 设置为蓝色阴影来解决它。 On exit (in the delegate method) set it back to white.在退出时(在委托方法中)将其设置回白色。

Just subclass the CNContactViewController只需继承 CNContactViewController

    class MyAwesomeViewController: CNContactViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationController?.navigationBar.subviews.forEach({ (aView) in
            aView.backgroundColor = UIColor.red
        })
    }
    
}

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

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