0

I`m using the AutoLayout with code on my application written in Swift 2.0 and that uses Cartograph to help with constrains. But something are blocking my app, otherwise, I use this to add a view programmatically.

func addButtonOne() -> Void {
    buttonOne = UIButton()
    buttonOne.backgroundColor = UIColor.greenColor()
    self.view.addSubview(buttonOne)

    constrain(buttonOne, view) { buttonOne, view in
        buttonOne.left == view.right - 12
        buttonOne.right == view.left + 12
        buttonOne.bottom == view.bottom - 12
    }

}

But after the compile, this returns:

2015-09-23 00:54:24.489 Fun With Swift[24809:2358352] 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) 

(
"<NSLayoutConstraint:0x7afe2160 H:[UIView:0x7afdfe90]-(-12)-[UIButton:0x7afe13b0](LTR)>",
"<NSLayoutConstraint:0x7afe2730 UIButton:0x7afe13b0.right == UIView:0x7afdfe90.left + 12>",
"<NSLayoutConstraint:0x7b0866e0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7afdfe90(320)]>"
)

Anyone know what I can do? This is a different issue because I use a library.

Pedro Paulo Amorim
  • 1,838
  • 2
  • 27
  • 50
  • possible duplicate of [Auto-layout: What creates constraints named UIView-Encapsulated-Layout-Width & Height?](http://stackoverflow.com/questions/23308400/auto-layout-what-creates-constraints-named-uiview-encapsulated-layout-width-h) – jtbandes Sep 23 '15 at 04:25
  • No. I'm using Swift and a library to do this. – Pedro Paulo Amorim Sep 23 '15 at 04:31
  • I don't think the library is the problem here. – jtbandes Sep 23 '15 at 04:35
  • The problem is with these two relations buttonOne.left == view.right - 12 buttonOne.right == view.left + 12. How can buttonOnce left view.right and buttonOne.right and view.left be related ? – Sandeep Sep 23 '15 at 06:01

1 Answers1

0

The add constrain code should be like following

 constrain(buttonOne, view) { buttonOne, view in
            buttonOne.left == view.left + 12
            buttonOne.right == view.right - 12
            buttonOne.bottom == view.bottom - 12
        }

If you need a button in the bottom with 12 pt margin from left right and bottom

Johnykutty
  • 12,091
  • 13
  • 59
  • 100