Cris
Cris

Reputation: 12194

Using autolayout from code with UIScrollView

I'm trying to use autolayout FROM CODE to correctly handle rotation in my UIScrollView that contains many UILabel generated at runtime for every page of a UIPageControl.

I've not found so many examples, so i ask if anyone has examples about UIScrollView with autolayout with NSLayoutConstraints.

EDITED To be clearer: i create at runtime different UILabels and add them to UIScrollView. In portrait it's:


(source: disclafani.org)


While in landscape is wrong:

(source: disclafani.org)



How can it be solved using autolayout? I've tried using:

NSLayoutConstraint *cons1=[NSLayoutConstraint
constraintWithItem:label 
attribute:NSLayoutAttributeLeft          
relatedBy:NSLayoutRelationEqual 
toItem:label2 attribute:NSLayoutAttributeLeft 
multiplier:1.0 
constant:[UIScreen mainScreen].bounds.size.width*-1
                          ];

but it does not work and i don't think this is the right approach

Upvotes: 1

Views: 2027

Answers (1)

matt
matt

Reputation: 535140

There are actually two quite different strategies, which Apple describes in great detail in this article, and which I illustrate in these two examples:

Keep in mind, though, that UILabel is a very special case, because of its use of instrinsic size rules. It is hard to say more without knowing more details of your case.

Upvotes: 5

Related Questions