CNContactViewController navigation bar different between versions

廉价感情. 提交于 2019-12-01 05:13:56

问题


Our tint color is white. Our app uses 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. 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.

Has anyone else experienced changes between Xcode versions/SDK versions that the navigation area of CNContactViewController has changed? 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)

回答1:


I was having the exact same issue. It definitely seems like an iOS 10 bug. Anyways, I found a work around by setting the navigation bar's translucency to 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



回答2:


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];



回答3:


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.

The following code helps me solve the problem (It works well in 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;
            }
        }
    });



回答4:


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. On exit (in the delegate method) set it back to white.




回答5:


In Swift 5 and Xcode 10.2

In iOS 9.0 CNContactViewController navigation bar working properly, but not higher versions.

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


来源:https://stackoverflow.com/questions/39793418/cncontactviewcontroller-navigation-bar-different-between-versions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!