简体   繁体   English

CNContactViewController 导航栏颜色无法正常工作

[英]CNContactViewController navigation bar colour not working properly

CNContactViewController navigation bar colour not appearing when i click Create New Contact option.当我单击“创建新联系人”选项时,CNContactViewController 导航栏颜色未出现。 See my screens for 1st time it's ok, but when i click Create New Contact i'm not getting navigation bar colour and not visible back button.第一次看到我的屏幕没问题,但是当我单击“创建新联系人”时,我没有获得导航栏颜色并且看不到后退按钮。

1st screen第一屏

在此处输入图像描述

2nd screen第二屏

在此处输入图像描述

In older versions在旧版本中

在此处输入图像描述

My code is我的代码是

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

        if #available(iOS 10.0, *) {
            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)
    }

And one more is by default phone number: (913) 351-5518还有一个是默认电话号码: (913) 351-5518

I would suggest to display a CNConctactViewController within a UINavigationController in Popover Modal Presentation style, and add a few button to return to the main application.我建议在 UINavigationController 中以 Popover Modal Presentation 样式显示 CNConctactViewController,并添加一些按钮以返回到主应用程序。 This implementation seams not trivial as reported by rumours over the net.这个实现并不像网上谣言所报道的那样微不足道。

Let me share a few pieces of my code (swift 5).让我分享几段我的代码(swift 5)。

The major class:大类:

class MyViewController: UITableViewController, UIPopoverPresentationControllerDelegate {

   var contactViewController = CNContactViewController()
   ...
   @objc func dismissContactViewController() {
      contactViewController.dismiss(animated: true, completion: nil)
   }
}

The extension:扩展名:

extension MyViewController: CNContactViewControllerDelegate {

func openCNContactViewController(willAppearWith: CNContact, type: ContactType) {
    switch type {
    case .forContact:
        contactViewController = CNContactViewController(for: willAppearWith)
        contactViewController.allowsEditing = false
        break
    case .forNewContact:
        contactViewController = CNContactViewController(forNewContact: willAppearWith)
        contactViewController.allowsEditing = true
        break
    case .forUnknowContact:
        contactViewController = CNContactViewController(forUnknownContact: willAppearWith)
        contactViewController.allowsEditing = true
        break
    }
    contactViewController.allowsActions = true
    contactViewController.contactStore = globalContactStore
    contactViewController.hidesBottomBarWhenPushed = true
    contactViewController.delegate = self
    // define the button (or select a default one)
    let button = UIButton(type: .custom)
    button.setTitleColor(self.view.tintColor, for: .normal)
    button.setTitle("My app name", for: .normal)
    button.addTarget(self, action: #selector(dismissContactViewController), for: .touchUpInside)
    let closeButton = UIBarButtonItem(customView: button)
    closeButton.style = .plain

    // add flexible space
    let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)

    // add to toolbar
    contactViewController.setToolbarItems([flexibleSpace, closeButton, flexibleSpace], animated: false)

    let navigationVC = UINavigationController(rootViewController: contactViewController)

    // show toolbar
    navigationVC.setToolbarHidden(false, animated: false)

    // set navigation presentation style
    navigationVC.modalPresentationStyle = .popover

    // present view controller
    self.present(navigationVC, animated: true, completion: nil)
 }

The result结果在此处输入图像描述

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

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