gpt4 book ai didi

ios - 当 iPad 旋转时,ActionSheet 的 PopoverController 不会停留在窗口的中心

转载 作者:行者123 更新时间:2023-11-28 12:00:38 24 4
gpt4 key购买 nike

我有一个注销按钮,它显示一个操作表,该操作表使用以下代码将 iPad 上的弹出窗口直接置于窗口中间:

popoverController.sourceRect = CGRect(x: window.bounds.midX, y: window.bounds.midY, width: 0, height: 0)
popoverController.permittedArrowDirections = []

此处显示在窗口中间:

Center

但是当我旋转 iPad 时,它会将自己定位到左下角:

Lower left hand corner

为什么我旋转 iPad 时它不会停留在窗口的中央?我该如何修复它以使其保持在旋转中心?

操作表:

fileprivate func logOut(){

let actionSheet = UIAlertController(title: nil, message: "Are you sure you want to log out?", preferredStyle: .actionSheet)

let cancelActionButton = UIAlertAction(title: "Cancel", style: .destructive){ (action) in
// ...
}

let logoutActionButton = UIAlertAction(title: "Log Out", style: .default){ (action) in
// ...
}

actionSheet.addAction(logoutActionButton)
actionSheet.addAction(cancelActionButton)

if let popoverController = actionSheet.popoverPresentationController{
popoverController.popoverBackgroundViewClass = PopoverBackgroundView.self

popoverController.sourceView = view
guard let window = UIApplication.shared.keyWindow else {
popoverController.sourceView = view
popoverController.sourceRect = CGRect(x: view.bounds.midX, y: view.bounds.midY, width: 0, height: 0)
popoverController.permittedArrowDirections = []
return
}
window.backgroundColor = .clear
popoverController.sourceRect = CGRect(x: window.bounds.midX, y: window.bounds.midY, width: 0, height: 0)
popoverController.permittedArrowDirections = []
}
tabBarController?.present(actionSheet, animated: true, completion: nil)
}

UIPopoverBackgroundView 的子类:

class PopoverBackgroundView: UIPopoverBackgroundView {

override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.white
}

convenience required init(coder aDecoder: NSCoder) {
self.init(frame: CGRect.zero)
}

override func layoutSubviews() {
super.layoutSubviews()
}

override var arrowOffset: CGFloat {
get {
return 0.0
}
set {
}

}

override var arrowDirection: UIPopoverArrowDirection {
get {
return UIPopoverArrowDirection.up
}
set {
}
}

override class func contentViewInsets() -> UIEdgeInsets {
return UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)
}

override class func arrowBase() -> CGFloat {
return 0.0
}

override class func arrowHeight() -> CGFloat {
return 0.0
}

override class var wantsDefaultContentAppearance: Bool {
get {
return false
}
}
}

最佳答案

您应该在设备旋转时更新框架:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
let bounds = UIApplication.shared.keyWindow?.bounds ?? view.bounds
currentActionSheet?.popoverPresentationController?.sourceRect = CGRect(x: bounds.midX, y: bounds.midY, width: 0, height: 0)
}

您需要将 UIAlertController 或 popoverController 保存到一个变量

weak var currentActionSheet : UIAlertController?

关于ios - 当 iPad 旋转时,ActionSheet 的 PopoverController 不会停留在窗口的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50180485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com