iosswiftipaduiactionsheet

ActionSheet's PopoverController doesn't stay in window's center when iPad is rotated


I have a logOut button that presents an action sheet that centers the popover on an iPad directly in the middle of the window using this code:

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

Here it's presented in the middle of the window:

Center

But when I rotate the iPad it positions itself towards the lower left hand corner:

Lower left hand corner

Why is it not staying in the center of the window whenever I rotate the iPad? How do i fix it so that it stays center on rotation?

ActionSheet:

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)
}

Subclass of 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
        }
    }
}

Solution

  • You should update the frame when the device is rotated:

    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)
    }
    

    You will need to save the UIAlertController or the popoverController to a variable

    weak var currentActionSheet : UIAlertController?