[英]Popover View positions to wrong place when rotated
我可能會嘗試將彈出框居中並使其在旋轉后仍居中。 這項研究幾乎回答了我的問題。 但是,我注意到一個非常奇怪的問題:當我在設備處於縱向時啟動彈出框時,它首先是居中的,但是,如果我旋轉到橫向,然后再將它旋轉到縱向,它是一個比中心 position 高一點。 我通過屏幕截圖制作了一張照片以表明
左邊的截圖是右邊的position,它是在第一次啟動popover時出現的,右邊的截圖是錯誤的position,你可以通過我畫的線清楚地看到它們之間的區別。
這是我用來使彈出框居中的代碼
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if let controller = self.presentedViewController as? PopoverController {
controller.popoverPresentationController?.sourceRect = CGRect(x: size.width / 2,
y: size.height / 2,
width: 0, height: 0)
let globalPoint = controller.view.superview?.convert(controller.view.frame.origin, to: nil)
controller.label.text = "x: \(globalPoint!.x), y: \(globalPoint!.y)"
self.view.layoutIfNeeded()
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "popoversegue" {
let controller = segue.destination
controller.popoverPresentationController!.delegate = self
controller.popoverPresentationController!.sourceRect = CGRect(x: view.center.x, y: view.center.y,
width: 0, height: 0)
controller.popoverPresentationController?.sourceView = self.view
controller.preferredContentSize = CGSize(width: 500, height: 600)
controller.popoverPresentationController!.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
}
}
此外,我已經意識到這個問題只發生在縱向,彈出框總是在橫向 position 中很好地居中。 這里可能有什么問題?
使您的 class 符合“UIPopoverPresentationControllerDelegate”
然后添加以下代碼。 我已經嘗試過並且工作正常。
func popoverPresentationController(_ popoverPresentationController: UIPopoverPresentationController, willRepositionPopoverTo rect: UnsafeMutablePointer<CGRect>, in view: AutoreleasingUnsafeMutablePointer<UIView>) {
let viewFrame = popoverPresentationController.presentingViewController.view.frame
let deltaX = viewFrame.height - rect.pointee.midX
rect.pointee = CGRect(x: viewFrame.width - deltaX, y: rect.pointee.maxY, width: 0, height: 0)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.