Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
584 views
in Technique[技术] by (71.8m points)

user interface - Xamarin iOS: Auto Layout within scroll view?

I have a scrollView on my storyboard on which I have added top, left, right, bottom constraints in order for it to resize automatically in accordance with different screen sizes. The problem is that if I add image view or any other stuff within this scroll view and place constraints on this image view to maintain some distance from top, left, right and bottom of the scrollview boundaries. It just doesn't work. I Would like to have everything resized according to the screen size of the target device. How could I do so?

Light Grey area is my scrollview, grey area is plain view, innermost element is imageview.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is a little complicated to explain.

As we know , before the autolayout get working, we should set constraints on them , according to those constraints the compiler can determine the Frame (position and size) on controls.

However, UIScrollView is a bit of different from UIView. In general, we just need to set left,right,top,bottom , but in UIScrollView, it has a definition called contentSize which determines the actual size of scrollview. so the constraints we set above is not enough.

So, as a workaround ,we add a container view inside the scrollview to determine contentSize.

1. Constraint on scrollview (green area in the following gif)

enter image description here

2. Constraint on container view.(red area in the following gif)

pay attention: we set additional width (equal to scrollview) in order to indicate the the width is fixed , means we can't scroll horizontally but vertical orientation is allowed.

enter image description here

3. Constraint on inner view(blue area in the following gif)

enter image description here

Test result :

enter image description here

iPhone5S

enter image description here

iPad Air2

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...