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

标签 ios swift ipad uiactionsheet

我有一个注销按钮,它显示一个操作表,该操作表使用以下代码将 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/

相关文章:

ios - NSCoding 和 Bools Swift 3

c - GLKit 的 GLKMatrix "Column Major"怎么样?

ios - 维护 HKHealthStore 对象的最佳方法......单例?

ios - 如何在不编写自己的应用程序的情况下向 iOS 发送推送通知?

swift - 带有 ImageView 自动布局单元拉伸(stretch)的 iOS8 UITableViewCell

ios - 警告 : Attempt to present * on * which is already presenting (null)

ios - "enable"- 我的 UIButton 成员不存在,为什么?

ios - 键盘存在时向上移动对话框有效,除非 ipad 被转动

ios - 延迟 UITableViewCell 触摸响应

iphone - 如何更新ios?