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

问题描述 投票:1回答:1
当我单击

创建新联系人选项时,CNContactViewController导航栏颜色未显示。第一次可以看到我的屏幕,但是,当我单击创建新联系人时,我没有得到导航栏颜色,也没有看到后退按钮。

第一屏幕

enter image description here

第二画面

enter image description here

在旧版本中

enter image description here

我的代码是

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

默认情况下还有一个电话号码:((913)351-5518

ios swift uinavigationcontroller contacts cncontactviewcontroller
1个回答
0
投票

我建议以Popover Modal Presentation样式在UINavigationController中显示CNConctactViewController,并添加一些按钮以返回到主应用程序。正如网上谣言所报道的那样,这种实现方式并非无关紧要。

让我分享一些代码(速览5)。

主要班级:

class MyViewController: UITableViewController, UIPopoverPresentationControllerDelegate {

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

扩展名:

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

结果enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.