vishal dharankar
vishal dharankar

Reputation: 7746

How to make this auto layout arrangement work with scrollview

I have a screen layout where there are two resizable labels , which will contain multiline text. These labels are placed inside their parent views which intern are added to main contentView, main contentView is then added to scrollView ( thats what most of the solutions suggests). For both the labels (below About and Time and location labels in first image attached) I have set height constraints as "greater than or equal to" and setting the numberOfLines to 0 as well as calling SizetoFit, but actual output is not as expected (see second image attached). There are no constraints warnings. All constraints are provided for all the elements.

enter image description here

enter image description here

The code in viewDidLoad is as follows for one of the label.

self.lblAbout.text = @"this is a long two three lines about string which will have two lines this is a long two three lines about string which will have two lines";
self.lblAbout.numberOfLines = 0;
[self.lblAbout sizeToFit];
[self.lblAbout setPreferredMaxLayoutWidth:244.0];

Also

-(void)viewDidLayoutSubviews
{
 [super viewDidLayoutSubviews];
 scrollView.contentSize = contentView.frame.size;
}

Not if any additional constraints are needed, I have added all leading , trailing , top , bottom constrains along with height wherever needed, plus the spacing between all the views is in place. What i want is the labels should get adjusted to number of lines and the contentView (parent view) should scroll inside ScrollView as the total height will be larger than the screen available.

*** problem I think is the outer view of the labels aren't getting resized as per the label because of which all the views below it aren't getting repositioned ****

Upvotes: 0

Views: 133

Answers (2)

vishal dharankar
vishal dharankar

Reputation: 7746

Here is what worked finally.

  1. Added ScrollView in main View
  2. Added View as a contentView ( be sure to rename it in designer to something than just view )
  3. pinned scrollview to main view using leading trailing top and bottom space constraints.
  4. Pinned contentView to scrollview same as above
  5. Added all the components with their respective constraints
  6. Set the height of UILabel which i want to resize using "Greater than or equal to constraint (this is necessary) and set number of lines to 0 in code
  7. The parent view of label shouldn't have any fixed height but enough constraints to calculate it at runtime.
  8. Make sure ScrollView has no ambiguity in calculating contentSize.
  9. imp - Add constraints to width of Main view , scrollview , contentView ( that was in my case , you may not need equal width constraint between contentView and scrollview , but between contentView and main view its necessary) Now scrollview scrolls exactly the way it's needed. To my belief most of the above things i already did but somehow it wasn't working ,deleted everything and did all the things again few times and it worked.

Upvotes: 0

Akash Soni
Akash Soni

Reputation: 535

Please try this Solution,

1. add Height Constraint to superView of your Label.
2. add IBOutlet of that Constraint
3. add this Method to find out Height of your Text you need to give width of super view of your Label so width will be same as your super view
4. Now get Height form returned CGRect and assign it to your Constraints's constant. it should be like

heightConstraint.constant = youObject.size.height;

please Make sure you have added other Constraints accordingly this. if not than you need to also increase Height of other superviews accordingly.

(CGRect)sizeOfDetailLabelFromString:(NSString*)string maxWidth:(CGFloat)maxWidth{
NSDictionary *attributes = @{NSFontAttributeName:FONT_LIGHT};
CGRect rect = [string boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attributes context:nil];
return rect;
}

Upvotes: 1

Related Questions