代码之家  ›  专栏  ›  技术社区  ›  user7503015

UIPopoverPresentationController popover在整个视图中显示

  •  0
  • user7503015  · 技术社区  · 8 年前

    class MyAppsNavigationController: UINavigationController, UINavigationControllerDelegate, UIPopoverPresentationControllerDelegate {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.delegate = self
            self.navigationBar.barTintColor = Colors.Red01.color()
            self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
        }
    
        func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
            viewController.navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "Ellipsis"), style: .plain, target: self, action: #selector(displayMenu(sender:)))
        }
    
        func displayMenu(sender: UIBarButtonItem)
        {
            let filterVC =  DropdownMenuController(nibName: "DropdownMenuController", bundle: nil)
            let nav = UINavigationController(rootViewController: filterVC)
            nav.modalPresentationStyle = UIModalPresentationStyle.popover
            //nav.isNavigationBarHidden = true
            nav.preferredContentSize = CGSize(width: 200, height: 300)
    
            let popover = nav.popoverPresentationController! as UIPopoverPresentationController
            popover.permittedArrowDirections = .up
            popover.delegate = self
            popover.barButtonItem = self.navigationItem.rightBarButtonItem
            popover.sourceView = self.view;
            var frame:CGRect = (sender.value(forKey: "view")! as AnyObject).frame
            frame.origin.y = frame.origin.y+20
            popover.sourceRect = frame
    
            popover.delegate = self
            self.present(nav, animated: true, completion: nil)
        }
    
        func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
            return .none
        }
    }
    

    单击按钮时的结果:

    First view with rightBarButtonImage

    单击后,弹出窗口将接管整个视图:

    When clicked the popup takes over entire view

    1 回复  |  直到 8 年前
        1
  •  0
  •   Adam    8 年前

    你有没有可能没有使用正确的委托方法?我认为这看起来更好:

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

    此外,在这种情况下不需要sourceView和sourceRect:为popover表示控制器指定一个barButtonItem就足够了。 https://developer.apple.com/documentation/uikit/uipopoverpresentationcontroller/1622314-barbuttonitem