iOS8 之前,Popover 只能在 iPad 上使用,iPhone 上使用效果类似 present。
iOS8 之后需要实现代理方法,取消设备判断,即可在 iPhone 上实现弹窗效果。
1 2 3 4 5
|
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle { return .none }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| let popover = ViewController()
popover.modalPresentationStyle = .popover
popover.preferredContentSize = CGSize(width: 200, height: 400)
popover.popoverPresentationController?.delegate = self
popover.popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
popover.popoverPresentationController?.sourceView = sender popover.popoverPresentationController?.sourceRect = sender.bounds
popover.popoverPresentationController?.permittedArrowDirections = .up
popover.popoverPresentationController?.backgroundColor = .red
self.present(popover, animated: true, completion: nil)
|