To make it more clear, here is an image with the constraints that you have to set: If you follow those steps you don’t have to set the UIScrollView’s contentSize property any more to make the UIScrollView scroll. It will wrap into multiple lines instead. Set the width constraint to the width of the UIScrollView and the UILabel will not become wider than the UIScrollView. Instead you should define a left and a width constraint. So if you just set a left and right constraint on the UILabel the UIScrollView will scroll horizontal and the label will be very wide and have only 1 line. That’s because the UIScrollView will give it enough space by allowing horizontal scrolling. If for example you have a UILabel that has a lot of text and should break into several lines, it just won’t, even if you set its numberOfLines property to 0. To ensure that the UIScrollView only scrolls vertically you have to make sure that its subviews don’t become wider than the UIScrollView.ĭo not rely on left and right constraints to define the width of a subview. The bottommost subview must have a bottom constraint with the UIScrollView All other subviews must have a top constraint with the bottom constraint of the subview above themģ. The topmost subview must have a top constraint with the UIScrollViewĢ. There are just a few things you have to take care of:ġ. So here is a short explanation on how to use Auto Layout with a UIScrollView that should scroll vertically: There are a lot of questions on StackOverflow like “Why is my UIScrollView not scrolling when using AutoLayout?” Some people still seem to struggle when it comes to using Apple’s Auto Layout in a UIScrollView. Posted: October 21st, 2015 | Author: Jörn | Filed under: iOS, Swift | Tags: Auto Layout, Masonry, SnapKit | 9 Comments »
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |