UIPopoverPresentationController 弹出窗口显示在整个视图上
UIPopoverPresentationController popover displays over the entire view
我正在尝试创建一个出现在我的应用程序中的 rightBarButtonItem。单击此 barItem 时,我想使用 UIPopoverPresentationController 显示模式弹出窗口。我已经能够让按钮显示在所有视图的 barItem 上。但是,当我单击按钮时,xib 会占据整个视图(包括导航栏,请参见下图)。请参阅下面的class:
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
}
}
点击按钮时的结果:
单击时弹出窗口占据整个视图:
你有没有使用正确的委托方法?我觉得这样更好看:
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
此外,在这种情况下,不需要 sourceView 和 sourceRect:为 popover 显示控制器指定一个 barButtonItem 就足够了。
https://developer.apple.com/documentation/uikit/uipopoverpresentationcontroller/1622314-barbuttonitem
我正在尝试创建一个出现在我的应用程序中的 rightBarButtonItem。单击此 barItem 时,我想使用 UIPopoverPresentationController 显示模式弹出窗口。我已经能够让按钮显示在所有视图的 barItem 上。但是,当我单击按钮时,xib 会占据整个视图(包括导航栏,请参见下图)。请参阅下面的class:
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
}
}
点击按钮时的结果:
单击时弹出窗口占据整个视图:
你有没有使用正确的委托方法?我觉得这样更好看:
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
此外,在这种情况下,不需要 sourceView 和 sourceRect:为 popover 显示控制器指定一个 barButtonItem 就足够了。 https://developer.apple.com/documentation/uikit/uipopoverpresentationcontroller/1622314-barbuttonitem