0

I am not using auto-layout, and I want to know if I can specify constraints between a UILabel and a UITextView. I tried what is mentioned here, but I got an error View hierarchy unprepared for constraint, so I looked up for that and found this, the accepted answer says that the views I am trying to add constraints to must be the subviews, if that is correct how can I add constraints so that the X position of my UITextView is set according to the height of UILabel ? I will post a picture so that it will give you all a clear idea:

enter image description here

Now I don't know if the UILabel is a single line or a couple of lines, and I want my UITextView to adjust it's X-position accordingly. This is the code I added, since I just learned about it please let me know if it is correct:

NSLayoutConstraint *xConstraint=[NSLayoutConstraint constraintWithItem:self.tvNewsDetails attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.lblNewsTitle attribute:NSLayoutAttributeBottom multiplier:0.45 constant:0];

[self.tvNewsDetails addConstraints:@[xConstraint]];
  • You have to try visual formatting method.This is recommended by apple and you cannot make it done without using autolayout. – Arun Apr 26 '16 at 10:49
  • check this http://stackoverflow.com/questions/36606205/uiview-with-dynamic-height-multiple-uilabel/36607158#36607158 – Badal Shah Apr 26 '16 at 10:50
  • Constraints are the things that drive autolayout. You cannot have one without the other. Do you mean you want to use constraints without interface builder? – Paulw11 Apr 26 '16 at 11:08
  • Its not about IB, i just want to set the constraints (no auto-layout though). –  Apr 26 '16 at 11:09
  • @Paulw11 - So i can't add constraints without auto-layout ? –  Apr 26 '16 at 11:09
  • What you are asking doesn't make sense. Constraints=autolayout. – Paulw11 Apr 26 '16 at 11:14
  • @Paulw11 - Sorry, i am new to obj C, so i can't even use NSLayoutConstraint in my code ? –  Apr 26 '16 at 11:17
  • Yes, you can, but if you are creating your UI elements programmatically then you need to set the item's `translatesAutoResizingMaskIntoConstraints` property to `false` and then you will need to add all of the constraints required to layout your items appropriately. – Paulw11 Apr 26 '16 at 11:19
  • @Paulw11 - Thanks a lot.. –  Apr 26 '16 at 11:21

1 Answers1

1

First you set leading, trailing, top, bottom for UILabel. In right side corner attribute inspector Line = 0 and line breaks = word wrap

then set constraints leading, trailing, top, bottom for UITextview. if UITextview height is fixed size means set height for UITextview.

must place both the component in UIScrollview.

it will flexible, while label height will increase.

parthiban
  • 95
  • 9