IOS UIScrollView uses Autolayout and iosuiscrollview
During the iteration and update of recent projects, you need to add a button in the previously written interface using Autolayout. After adding the button, the iPhone 5 and iPhone 4 are not displayed completely. after encountering the problem, I naturally thought of using UIScrollView, and soon I created a UIScrollView full screen, which included all the previous controls in [self. change view addSubview: xxx] to [self. scrollView addSubview: xxx], click a run button with full confidence, and fail to slide ......
After re-adjusting the concept, we solved this problem. We created a UIScrollView first, which is correct, and then created a contrainerView for storage before being added to [self. view], add the contrainer to the scrollview, and adjust the position of the contrainerView.
Code:
// Create a scrollview UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame: CGRectZero]; [self. view addSubview: scrollView]; [scrollView mas_makeConstraints: ^ (MASConstraintMaker * make) {make. edges. similar to (self. view) ;}]; // create a view where various view Controls are placed and added to the scrollview. UIView * contrainerView = [[UIView alloc] initWithFrame: CGRectZero]; [scrollView addSubview: contrainerView]; UIView * view1 = [[UIView alloc] initWithFrame: CGRectZero]; Parameters = [UIColor redColor]; [contrainerView addSubview: view1: ^ (MASConstraintMaker * make) {make. left. top. similar to (@ 20); make. right. similar to (@ (-20); // If the width is not specified, the view cannot show make. width. similar to (@ (self. view. frame. size. width-40); make. height. failed to (@ 200);}]; UIView * view2 = [[UIView alloc] initWithFrame: CGRectZero]; view2.backgroundColor = [UIColor greenColor]; [contrainerView addSubview: view2]; [view2 mas_makeConstraints: ^ (MASConstraintMaker * make) {make. left. similar to (@ 100); make. top. pointer to (view1.mas _ bottom ). offset (10); make. right. similar to (@ (-100); // If the width is not specified, the view cannot show make. width. similar to (@ (self. view. frame. size. width-200); make. height. failed to (@ 400);}]; UIView * view3 = [[UIView alloc] initWithFrame: CGRectZero]; view3.backgroundColor = [UIColor redColor]; [contrainerView addSubview: view3]; [view3 mas_makeConstraints: ^ (MASConstraintMaker * make) {make. left. similar to (@ 20); make. top. pointer to (view2.mas _ bottom ). offset (10); make. right. similar to (@ (-20); // If the width is not specified, the view cannot show make. width. similar to (@ (self. view. frame. size. width-40); make. height. pointer to (@ 200);}]; // the container's top position is determined based on the last view control [contrainerView mas_makeConstraints: ^ (MASConstraintMaker * make) {make. edges. rollback to (scrollView); make. bottom. pointer to (view3.mas _ bottom ). offset (20) ;}];
If you did not see this article in wb145230 blog,Click to view Original Text.