0

I create one uilable and uibutton

@IBOutlet weak var label_one: UILabel!
@IBOutlet weak var btn_one: UIButton!

and set following trailing and leading constraints

        self.btn_one.translatesAutoresizingMaskIntoConstraints=false
    let contariants=NSLayoutConstraint(item:self.view,attribute:NSLayoutAttribute.LeadingMargin , relatedBy: NSLayoutRelation.Equal, toItem: btn_one, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1.0, constant: 20)
    let trailingConstraints=NSLayoutConstraint(item: self.view, attribute:NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: btn_one, attribute:NSLayoutAttribute.Trailing , multiplier: 1.0, constant:20 )

    self.view.addConstraint(contariants)
    self.view.addConstraint(trailingConstraints)

// but it not working at all and giving following warnings 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. ( "", "UIButton:0x7ff8c84b0b60'Button' (Names: '|':UIView:0x7ff8c85226d0 )>" )

Will attempt to recover by breaking constraint

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful. 2015-12-16 10:39:01.918 ConstraintsExample[2635:38168] 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. ( "", "UIButton:0x7ff8c84b0b60'Button' (Names: '|':UIView:0x7ff8c85226d0 )>", "", "" )

Will attempt to recover by breaking constraint

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

Haris
  • 12,120
  • 6
  • 43
  • 70
khaskheli
  • 13
  • 4

2 Answers2

0

This error is cause you are trying to make the view do multiple constraints at the same time. The instant you drag n drop an element on the view from IB, it makes a basic constraint of the given location. When you attempt the following "addConstraint" method the compilier is unable to understand which constraint to follow.

The above will work if u programmatically make a button and label. PFB a post that talk about this:

Programmatically Add CenterX/CenterY Constraints

In case you want to modify your existing constraints on the storyboard: Add a constraint on the element: adding an element contraint

No use that contraint as a connection to your code: make the constraint part of the code

Community
  • 1
  • 1
Karthik
  • 99
  • 2
  • 14
  • it works fine now and thanks, can i change constraints which were already set from xcode?????? – khaskheli Dec 16 '15 at 07:42
  • Have a look at this post: http://stackoverflow.com/questions/29991246/set-constraints-through-code-to-an-element-from-storyboard-with-swift-ios8 What they say is to simply control+drag a NSLayout constraint from the storyboard onto your view controller and then modify it in your code as below: constraint.constant = 50 – Karthik Dec 16 '15 at 07:45
  • i drag and drop button from storyboard and apply constraints from a code none of constraints worked.. and when i create button from code and apply same constraints it will apply all those and works fine, i could not understand this, can u help me.. i will appreciate – khaskheli Dec 16 '15 at 10:02
  • @khaskheli I've updated my answer. please have a look. – Karthik Dec 17 '15 at 01:44
0

You do what the error message says. You look at the constraints that it displays, think hard, and figure out where you have contradicting constraints.

You probably have some constraints in the .xib file or storyboard.

BTW. "contariants" is beyond horrible. The first constraint looks wrong (view.leading = button.leading + 20, which means the button starts 20 pixels left of the screen????). NSLayoutAttribute.LeadingMargin should be just .LeadingMargin.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • thanks @gnasher729. one question i set constraints on view from xcode and want change from code is it possible???? – khaskheli Dec 16 '15 at 07:33