CNContactViewController navigation bar different between versions

后端 未结 5 699
[愿得一人]
[愿得一人] 2021-01-14 00:00

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

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-14 00:26

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

提交回复
热议问题