2

I want to layout a label in the right corner of its superView with 8pt trailing and 10pt from bottom

This is my code :

  let label = UILabel(frame: CGRectZero)
  label.text = "text goes here"
  label.textColor = UIColor.whiteColor()
  label.translatesAutoresizingMaskIntoConstraints = false
  label.numberOfLines = 0
  label.textAlignment = .Right
  label.backgroundColor = UIColor.blackColor()
  label.font = UIFont(name: "TheSans-Plain", size: 17)
  imageTitleContainer.addSubview(label)


    let label_constraint_H:Array = NSLayoutConstraint.constraintsWithVisualFormat("H:|-(>=8)-[label]-(8)-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["label":label])

    let label_constraint_V:Array = NSLayoutConstraint.constraintsWithVisualFormat("V:[label]-(10)-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["label":label])

    imageTitleContainer.addConstraints(label_constraint_H)
    imageTitleContainer.addConstraints(label_constraint_V)`

but I am getting Unable to simultaneously satisfy constraints in console

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7ff55acf24c0 H:[UILabel:0x7ff55acf1af0'text goes here']-(8)-| (Names: '|':UIView:0x7ff55acbf430 )>

Note : 0x7ff55acbf430 is my imageTitleContainer in the code

Update: other constrains: "<NSLayoutConstraint:0x7ff55accdc80 H:|-(0)-[UIView:0x7ff55acbf430] (Names: '|':UIView:0x7ff55acca080 )>", "<NSLayoutConstraint:0x7ff55accdcd0 H:[UIView:0x7ff55acbf430]-(0)-| (Names: '|':UIView:0x7ff55acca080 )>", "<NSLayoutConstraint:0x7ff55acf2450 H:|-(>=8)-[UILabel:0x7ff55acf1af0'text goes here'] (Names: '|':UIView:0x7ff55acbf430 )>", "<NSLayoutConstraint:0x7ff55acf24c0 H:[UILabel:0x7ff55acf1af0'text goes here']-(8)-| (Names: '|':UIView:0x7ff55acbf430 )>", "<NSLayoutConstraint:0x7ff55adb3830 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7ff55acca080(0)]>" )

Bobj-C
  • 5,276
  • 9
  • 47
  • 83
  • What other constraints are mentioned in the console? There should be a list of all constraint that should be applied. – Jörn Buitink Dec 21 '15 at 06:58
  • @JörnBuitink I updated my question – Bobj-C Dec 21 '15 at 07:01
  • its because of conflict in horizontal constraints one side you are applying `>=8` and another side fix constraint. Remove `>=8` Leading constraint just apply trailing constraint `-8-` and if require set appropriate width. – Dipen Panchasara Dec 21 '15 at 07:19
  • @DipenPanchasara then if the text is long the label width will bypass the superview width – Bobj-C Dec 21 '15 at 07:21
  • @Bobj-C in that case either define specific width and set `linebreakmode` of your label. or apply leading constraint also `-8-` to fixed size. – Dipen Panchasara Dec 21 '15 at 07:23
  • @DipenPanchasara What if I want the label to expand vertically to show all its text ? – Bobj-C Dec 21 '15 at 07:30

1 Answers1

0

Try to lower priority for one of your constraints, e.g.

H:|-(>=8@999)-[label]-(8)-|

This has to be done to allow UICollectionView to do its layout work. Note zero-width constraint named UIView-Encapsulated-Layout-Width which breaks your layout.

Denys Stas
  • 161
  • 7