我想更新popover的布局,我已经尝试使用setNeedsLayout()
和layoutIfNeeded()
方法,但找不到解决方案。
我有一个简单的屏幕,有4个按钮,可以调用弹出框,在弹出框中有2个额外的按钮:
我遇到的问题是我无法更新/刷新屏幕,并且popover始终指向前一个源视图位置。 Picture of the popover
不知何故,我需要一个更新视图的方法,它比layoutIfNeeded()
强,因为如果屏幕从纵向/横向更改,则弹出框会自动更改。 Here I changed portrait/landscape/portrait
项目的GitHub。 GitHub
使用的代码如下:
class ViewController: UIViewController, ButtonDidDisappearDelegate {
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!
@IBOutlet weak var button4: UIButton!
@IBOutlet weak var constrain2: NSLayoutConstraint!
var popVC: PopVC?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func buttonPopover(_ sender: UIButton) {
configureAndPresentPopover(from: sender)
}
func buttonDisappear() {
self.constrain2.constant = 100
self.button3.isHidden = !self.button3.isHidden
if !self.button3.isHidden {
self.constrain2.constant = 10
}
}
func repositionPopover() {
DispatchQueue.main.async {
if let popVC = self.popVC {
self.self.popoverPresentationController(popVC.popoverPresentationController!, willRepositionPopoverTo: self.button4.bounds, in: self.button4)
}
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}
}
}
extension ViewController: UIPopoverPresentationControllerDelegate {
func configureAndPresentPopover(from sender: UIButton) {
popVC = storyboard?.instantiateViewController(withIdentifier: "popVC") as? PopVC
guard let popVC = popVC else {
return
}
popVC.popOverDelegate = self
popVC.modalPresentationStyle = .popover
let popOverVC = popVC.popoverPresentationController
popOverVC?.delegate = self
popOverVC?.sourceView = sender
popOverVC?.sourceRect = sender.bounds
popVC.preferredContentSize = CGSize(width: 300, height: 60)
popOverVC?.permittedArrowDirections = .down
self.present(popVC, animated: true)
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
func popoverPresentationController(_ popoverPresentationController: UIPopoverPresentationController,
willRepositionPopoverTo rect: CGRect,
in view: UIView) {
popoverPresentationController.sourceView = view
popoverPresentationController.sourceRect = rect
}
}
和弹出控制器:
protocol ButtonDidDisappearDelegate: class {
func configureAndPresentPopover(from sender: UIButton)
func buttonDisappear()
func repositionPopover()
}
class PopVC: UIViewController {
weak var popOverDelegate: ButtonDidDisappearDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBOutlet weak var popoverButton: UIButton!
@IBAction func popoverAction(_ sender: Any) {
popOverDelegate?.buttonDisappear()
DispatchQueue.main.async {
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}
}
@IBAction func reposition(_ sender: Any) {
popOverDelegate?.repositionPopover()
}
}
答案 0 :(得分:0)
您需要指定箭头的新位置并询问演示控制器视图的布局,而不仅仅是呈现的视图。
尝试:
func repositionPopover() {
let popOverController = presentedViewController?.presentationController as? UIPopoverPresentationController
popOverController?.sourceView = button4
popOverController?.containerView?.setNeedsLayout()
popOverController?.containerView?.layoutIfNeeded()
}
(无需在主线程上发送,使用UIKit时已经在该线程上)
答案 1 :(得分:0)
您可以尝试再次设置sourceview和sourcerect。当sourcview边界更改时,调用此函数。