Swift Default Alertviewcontroller Breaking Constraints

Swift default AlertViewController breaking constraints

This error is not critical, seems to be unfixed bug form Apple. This constraint appears in animation style just after presenting. Sample Image I tried to catch and change it (change values, relations, priority) before presenting – no success because of this dynamically added constraints.

When you turn off animation in self.present(alert, animated: false) and using alert.view.addSubview(UIView()) – the error disappears. I can't explain it, but it works!

let alert = UIAlertController(title: "Change your profile image", message: nil, preferredStyle: .actionSheet)

alert.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Online Stock Library", style: .default, handler: nil))
let cancel = UIAlertAction(title: "Cancel", style: .destructive, handler: nil)

alert.addAction(cancel)
alert.view.addSubview(UIView()) // I can't explain it, but it works!

self.present(alert, animated: false)

Presenting UIAlertController (in actionsheet style) caused mysterious autolayout warning

This is a bug which is not fixed by Apple team this bug is related to alerts and action sheets related to animation. You may follow these links to verify this:-

UIAlertController's actionSheet gives constraint error on iOS 12.2 / 12.3

Swift default AlertViewController breaking constraints

The solution for this is to pass animation value as false like:-

controller.present(alertController, animated: false, completion: nil)

I have present the UIAlertController without animation and warning got vanished.

or you may try this solution mentioned in the one of the links of stackoverflow:-

@IBAction func buttonTapped(_ sender: UIButton) {
...
self.present(alertController,animated: true,completion: nil)



alertController.view.subviews.flatMap({$0.constraints}).filter{ (one: NSLayoutConstraint)-> (Bool) in
return (one.constant < 0) && (one.secondItem == nil) && (one.firstAttribute == .width)


}.first?.isActive = false

}

Swift - Unable to simultaneously satisfy constraints

All action sheets do that. It’s an Apple bug. It has no effect on your app’s functionality. Ignore it and move on.

How to fix ActionSheet of UIAlertController has the conflict constraint error in Swift

You may have to manually silent the constraint alert.

   @IBAction func newToDoBarButtonTapped(_ sender: UIBarButtonItem) {
...
self.present(alertController,animated: true,completion: nil)

alertController.view.subviews.flatMap({$0.constraints}).filter{ (one: NSLayoutConstraint)-> (Bool) in
return (one.constant < 0) && (one.secondItem == nil) && (one.firstAttribute == .width)
}.first?.isActive = false

}


Related Topics



Leave a reply



Submit