如果源视图发生变化,如何正确处理`UIAlertController`?

时间:2016-05-17 10:52:55

标签: swift uialertcontroller

我创建了一个UIAlertController,如下所示:

let menu = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)

menu.popoverPresentationController?.sourceView = someView!
menu.popoverPresentationController?.sourceRect = someView!.bounds

在我旋转设备之前一切正常。 (仅限iPad,因为对于iPad,UIAlertController根据sourceViewsourceRect在某个位置显示弹出窗口)

当我旋转设备时,someView的位置/大小会被其他模块更改。因此,有一些关于约束的警告:

Unable to simultaneously satisfy constraints.

虽然这只是警告,但我仍然希望知道在源视图发生变化时处理UIAlertController的正确方法是什么?

编辑: 记录约束警告:

Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
"<NSAutoresizingMaskLayoutConstraint:0x7fac14c672f0 UIView:0x7fac14c994c0.width == 19>",
"<NSLayoutConstraint:0x7fac14c95260 UIView:0x7fac1483b430.width == 300>",
"<NSLayoutConstraint:0x7fac124f8520 _UIAlertControllerView:0x7fac14c41cc0.width == UIView:0x7fac14c994c0.width>",
"<NSLayoutConstraint:0x7fac14c09ac0 _UIAlertControllerView:0x7fac14c41cc0.width >= UIView:0x7fac1483b430.width>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fac14c95260 UIView:0x7fac1483b430.width == 300>

1 个答案:

答案 0 :(得分:-1)

try using following It works

 @IBAction func actionContinueToPay(sender: UIButton) {
        let optionMenu = UIAlertController(title: "Select payment method", message: "Select the payment method first", preferredStyle: .ActionSheet)

        let cashOnDeliveryAction = UIAlertAction(title: "Cash On delivery", style: .Default, handler: {
            (alert: UIAlertAction!) -> Void in
            // self.serviceCallSaveOrder()
            self.serviceCallSaveOrder("No Token",STATUS: self.STATUS_COD)

        })



        let walletAction = UIAlertAction(title: "Pay by Card", style: .Default, handler: {
            (alert: UIAlertAction!) -> Void in
            self.serviceCall_GetcardInfo()

        })

        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {
            (alert: UIAlertAction!) -> Void in
            print("Canrcelled")
        })
        optionMenu.addAction(cashOnDeliveryAction)
        optionMenu.addAction(walletAction)

        optionMenu.addAction(cancelAction)

        self.presentViewController(optionMenu, animated: true, completion: nil)

    }