如何从标签栏项目呈现popover?

时间:2018-02-04 11:21:53

标签: ios swift uitabbarcontroller swift4 uitabbar

我想在Swift 4中提供一个ViewController作为popover,但它通常会显示Viewcontroller,这是代码:

class CustomTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let favViewController = TrialViewController()
        let exhibtionViewController = TrialViewController()
        let menuViewController = ttttViewController()
        let notificationViewController = TrialViewController()
        let profileViewController = ttttViewController()

        favViewController.tabBarItem.title = "first"
        exhibtionViewController.tabBarItem.title = "second"
        menuViewController.tabBarItem.title = "third"
        notificationViewController.tabBarItem.title = "forth"
        profileViewController.tabBarItem.title = "fifth"

        favViewController.tabBarItem.image = UIImage(named:"home25")
        exhibtionViewController.tabBarItem.image = UIImage(named: "bag25")
        menuViewController.tabBarItem.image = UIImage(named: "main_add_25")
        notificationViewController.tabBarItem.image = UIImage(named: "notification25")
        profileViewController.tabBarItem.image = UIImage(named: "man_man25")


        let tabBarItemWidth = Int(self.tabBar.frame.size.width) / (self.tabBar.items?.count)!
        let x = tabBarItemWidth * 3;
        let newRect = CGRect(x: x, y: 0, width: tabBarItemWidth, height: Int(self.tabBar.frame.size.height))
        print(newRect)

        menuViewController.modalPresentationStyle = .popover
        menuViewController.view.frame = newRect
        menuViewController.preferredContentSize = CGSize(width: 150,height: 150)

        if let popoverMenuViewController = menuViewController.popoverPresentationController {
            popoverMenuViewController.permittedArrowDirections = .down
            popoverMenuViewController.delegate = menuViewController as? UIPopoverPresentationControllerDelegate
            popoverMenuViewController.sourceRect = newRect
            popoverMenuViewController.sourceView = self.tabBar

            present(menuViewController, animated: true, completion: nil)

        }

        viewControllers = [favViewController, exhibtionViewController, menuViewController, notificationViewController, profileViewController]
    }

}

我的代码有什么问题?

1 个答案:

答案 0 :(得分:4)

override func viewDidAppear(_ animated: Bool) {

    let vc = self.storyboard?.instantiateViewController(withIdentifier: "bbb") as! ttttViewController
    vc.modalPresentationStyle = .popover //presentation style

    vc.preferredContentSize = CGSize(width: 150,height: 150)
    vc.popoverPresentationController?.delegate = self as! UIPopoverPresentationControllerDelegate
    vc.popoverPresentationController?.sourceView = view
    vc.popoverPresentationController?.sourceRect = self.tabBar.frame
    self.present(vc, animated: true, completion: nil)


}


func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return  .none
}

演示在这里popover

enter image description here