how to center a popoverview in swift

前端 未结 10 1740
终归单人心
终归单人心 2020-12-04 16:42

I have the following code to show a popoverview (dialog) without an arrow, which works fine. The only problem is, that the dialog is shown in the top left (IPad). I would li

10条回答
  •  猫巷女王i
    2020-12-04 17:16

    Basically consist of three steps (iOS 8):

    1.- Present the view:

    Let's say, you want to show a custom view to ask for a Review to the user.. here the function loadNibForRate() returns an instance of RateDialog loaded from its nib, but you can use here any way you desire to locate your UIViewController

    private static func presentCustomDialog(parent: RateDialogParent) -> Bool {
        /// Loads the rateDialog from its xib, handled this way for further customization if desired
          if let rateDialog = loadNibForRate() {
            rateDialog.modalPresentationStyle = UIModalPresentationStyle.Popover
            rateDialog.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
            let x = parent.view.center
    
            let sourceRectX : CGFloat
            //Here we check for the orientation of the device, just to know if we are on an iPad
            let maximumDim = max(UIScreen.mainScreen().bounds.height, UIScreen.mainScreen().bounds.width)
            if maximumDim == 1024 { //iPad
                sourceRectX = x.x
            }else {
                sourceRectX = 0
            }
    
            rateDialog.popoverPresentationController?.sourceView = parent.view
            rateDialog.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.allZeros
            rateDialog.popoverPresentationController?.sourceRect = CGRectMake(sourceRectX, x.y, 0, 0)
            rateDialog.popoverPresentationController?.popoverLayoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
            rateDialog.popoverPresentationController?.delegate = parent
    
            rateDialogParent = parent
    
            callFunctionAsync() {
                parent.presentViewController(rateDialog, animated: true, completion: nil)
            }
            return true
        }
        return false
    }
    

    2.- If we rotate our device, then the popover will not know where to reposition itself, unless we have this on the parent RateDialogParent

    public class RateDialogParent: UIViewController, UIPopoverPresentationControllerDelegate {
    /**
    This function guarantees that the RateDialog is alwas centered at parent, it locates the RateDialog's view by searching for its tag (-555)
     */
     public func popoverPresentationController(popoverPresentationController: UIPopoverPresentationController, willRepositionPopoverToRect rect: UnsafeMutablePointer, inView view: AutoreleasingUnsafeMutablePointer) {
        if popoverPresentationController.presentedViewController.view.tag == RateDialog.thisViewTag {
            let x = popoverPresentationController.presentingViewController.view.center
            let newRect = CGRectMake(x.x, x.y, 0, 0)
            rect.initialize(newRect)
        }
     }
    }
    

    3.- Your RateDialog should have a tag setted, this is just to avoid relocating unwanted popovers if there is more that one presented from your RateDialogParent

    class RateDialog: UIViewController {
     @IBOutlet weak var reviewTitle: UILabel!
     @IBOutlet weak var reviewMessage : UILabel!
     @IBOutlet weak var cancelButtonTitle: UIButton!
     @IBOutlet weak var remindButtonTitle : UIButton!
     @IBOutlet weak var rateButtonTitle : UIButton!
    
        /// For being able to locate this view
     static let thisViewTag = -555
    
     override func viewDidLoad() {
        super.viewDidLoad()
        //sets the tag to identify this view
        self.view.tag = RateDialog.thisViewTag
     }
    }
    

提交回复
热议问题