136

On iOS 12.2, while using UIAlertController's actionSheet, Xcode gives constraint error. Anyone having this problem?

This same code runs on iOS 12.1 with no error.

I have tested this code on Xcode 10.2 and 10.1.

class ViewController: UIViewController {
    
    let Click : UIButton = {
        let button = UIButton(type: UIButton.ButtonType.system)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle("OK", for: .normal)
        button.tintColor = UIColor.blue
        button.addTarget(self, action: #selector(click(_:)), for: UIControl.Event.touchUpInside)
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(Click)
        Click.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        Click.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    }

    
    @objc func click(_ sender: UIButton) {
        let optionMenu = UIAlertController(title: nil, message: "Choose Option", preferredStyle: .actionSheet)
        
        let deleteAction = UIAlertAction(title: "Delete", style: .default)
        let saveAction = UIAlertAction(title: "Save", style: .default)
    
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
        
        optionMenu.addAction(deleteAction)
        optionMenu.addAction(saveAction)
        optionMenu.addAction(cancelAction)
        
        self.present(optionMenu, animated: true, completion: nil)
    }

}
[LayoutConstraints] 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. 
(
    "<NSLayoutConstraint:0x6000001b6ee0 UIView:0x7fe3b6513020.width == - 16   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000001b6ee0 UIView:0x7fe3b6513020.width == - 16   (active)>

PS:

Just to make sure that the problem is on UIAlertController, I removed everything and updated the code as below, but I received the same error:

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        let optionMenu = UIAlertController(title: "Test", message: "Choose Option", preferredStyle: .actionSheet)
        
        let deleteAction = UIAlertAction(title: "Delete", style: .default)
        let saveAction = UIAlertAction(title: "Save", style: .default)
        
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
        
        optionMenu.addAction(deleteAction)
        optionMenu.addAction(saveAction)
        optionMenu.addAction(cancelAction)
        
        self.present(optionMenu, animated: true, completion: nil)
    }
    
}
Rex Nihilo
  • 604
  • 2
  • 7
  • 16
L03n
  • 1,421
  • 2
  • 10
  • 12
  • i've tested your code, it runs perfectly. can you provide an actual screen shot – Sanad Barjawi Mar 27 '19 at 08:05
  • @SanadBarjawi https://imgur.com/Hek2eQT here is the error. this problem occur only on iOS 12.2 – L03n Mar 27 '19 at 08:16
  • what's your xCode Version? – Sanad Barjawi Mar 27 '19 at 08:19
  • @SanadBarjawi tested on Xcode 10.2 and Xcode 10.1 on iOS 12.1 and 12.2 problem shows up on iOS 12.2 only – L03n Mar 27 '19 at 08:24
  • try to give the Click button a fixed width – Sanad Barjawi Mar 27 '19 at 08:28
  • @SanadBarjawi I did. button is not the problem. – L03n Mar 27 '19 at 08:31
  • does it occur if you commented the actionSheet code? – Sanad Barjawi Mar 27 '19 at 08:35
  • @SanadBarjawi without actionSheet no error – L03n Mar 27 '19 at 08:58
  • i think that this issue comes from the parent View, so try to added another parent view with zeroes as leading trailing top and bottom, then add your current elements to it (this is just for testing) – Sanad Barjawi Mar 27 '19 at 09:27
  • @SanadBarjawi I add tempView with zeroes as leading trailing top and bottom. then added a button in that view. but same problem I don't think button is the problem . Something is wrong with UIAlertController in iOS 12.2. just to make sure its not the button I completely remove another code and just put the UIAlertController code in ViewDidAppear. same problem – L03n Mar 27 '19 at 09:54
  • in viewDidLoad, try to view.layoutIfNeeded() – Sanad Barjawi Mar 27 '19 at 09:55
  • @SanadBarjawi nope not working. – L03n Mar 27 '19 at 10:10
  • 3
    suppose that its the 12.2 update issue then – Sanad Barjawi Mar 27 '19 at 10:27
  • 5
    I'm experiencing this same problem in Xcode 10.2 running iOS 12.2--guessing it's a bug! I'm planning to just ignore it. – Becky Hansmeyer Mar 29 '19 at 19:44
  • 1
    Same issue with 12.2. 12.1 doesn't have this problem. – Alexey Chekanov Apr 07 '19 at 23:59
  • I am also experiencing the same issue with iOS 12.2 – FE_Tech Apr 13 '19 at 05:16
  • Duplicate of [Swift default AlertViewController breaking constraints](https://stackoverflow.com/questions/55653187/swift-default-alertviewcontroller-breaking-constraints), we may as well consolidate these. And please file a bug report. – matt Apr 14 '19 at 19:16
  • I have exactly the same issue: iOS 12.2 with iPhone action sheet , I am using objective-C code and the same code has been working for years, it logs this error only for iOS 12.2 – sean3033 Apr 17 '19 at 17:38
  • @matt I inform apple about the problem. waiting for respond, and [Swift default AlertViewController breaking constraints](https://stackoverflow.com/questions/55653187/swift-default-alertviewcontroller-breaking-constraints) is the Duplicate one. this problem was raise way before the one you mention in the link. correct me if I am wrong. – L03n Apr 19 '19 at 06:06
  • 1
    Even with 12.3 as well, XCode 10.2.1 – Rajan Maheshwari May 27 '19 at 11:03
  • Same issue here. iOS 12.3.1 – unixb0y Jul 05 '19 at 15:16
  • The workaround is to find the constraint with constant < 0 and decrease its priority. if let constraints = alertViewController.view?.subviews.first?.constraints { for constraint in constraints { if constraint.constant < 0 { constraint.priority = UILayoutPriority(rawValue: constraint.priority.rawValue - 1) } } } – Denis Bystruev Sep 30 '20 at 02:42
  • Another way is to negate the constant to make it positive: if let constraints = alertViewController.view?.subviews.first?.constraints { for constraint in constraints { if constraint.constant < 0 { constraint.constant = -constraint.constant } } } – Denis Bystruev Sep 30 '20 at 02:52

1 Answers1

165

It's a bug in iOS versions:

  • 12.2
  • 12.3
  • 12.4
  • 13.0
  • 13.1
  • 13.2
  • 13.2.3
  • 13.3
  • 13.4
  • 13.4.1
  • 13.5
  • 13.5.1
  • 13.6
  • 14.0
  • 14.1
  • 14.2
  • 14.4

The only thing we can do is to file a bug report to Apple (I just did that and you should too).

I'll try to update answer for a new versions of iOS if the bug still will be present. Help appreciated.

Andrei Konstantinov
  • 6,971
  • 4
  • 41
  • 57