最近项目在迭代更新的时候,需要在之前用Autolayout写的界面里添加一个button,添加完这个button后,iPhone5,iPhone4显示不全了.遇到整个问题后很自然就想到了用UIScrollView,很快就创建了一个占满全屏的UIScrollView,把之前所有的控件有[self.view addSubview:xxx]全部改成[self.scrollView addSubview:xxx],信心满满的点击了一个运行按钮,艹,居然滑动不了......

  重新调整了一下思路,解决了此问题,先创建一个UIScrollView没错,然后创建一个contrainerView用于存储之前添加在[self.view]上的控件,把contrainer再添加到scrollview上,最后调整contrainerView的位置即可.

  代码:

// 创建一个scrollview
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
[self.view addSubview:scrollView];
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}]; // 创建一个view,这个view里面放置各种view控件,并添加到scrollview上
UIView *contrainerView = [[UIView alloc] initWithFrame:CGRectZero];
[scrollView addSubview:contrainerView]; UIView *view1 = [[UIView alloc] initWithFrame:CGRectZero];
view1.backgroundColor = [UIColor redColor];
[contrainerView addSubview:view1];
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.equalTo(@);
make.right.equalTo(@(-));
// 宽度不写view显示不出来
make.width.equalTo(@(self.view.frame.size.width - ));
make.height.equalTo(@);
}]; UIView *view2 = [[UIView alloc] initWithFrame:CGRectZero];
view2.backgroundColor = [UIColor greenColor];
[contrainerView addSubview:view2];
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(@);
make.top.equalTo(view1.mas_bottom).offset();
make.right.equalTo(@(-));
// 宽度不写view显示不出来
make.width.equalTo(@(self.view.frame.size.width - ));
make.height.equalTo(@);
}]; UIView *view3 = [[UIView alloc] initWithFrame:CGRectZero];
view3.backgroundColor = [UIColor redColor];
[contrainerView addSubview:view3];
[view3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(@);
make.top.equalTo(view2.mas_bottom).offset();
make.right.equalTo(@(-));
// 宽度不写view显示不出来
make.width.equalTo(@(self.view.frame.size.width - ));
make.height.equalTo(@);
}]; // 容器的顶部位置基于最后一个view控制来确定
[contrainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(scrollView);
make.bottom.equalTo(view3.mas_bottom).offset();
}];

  如果你不是在wb145230博客园看到本文,请点击查看原文.

iOS UIScrollView使用Autolayout的更多相关文章

  1. iOS学习笔记——AutoLayout的约束

    iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...

  2. IOS UIScrollView常用代理方法

    iOS UIScrollView代理方法有很多,从头文件中找出来学习一下 //只要滚动了就会触发 - (void)scrollViewDidScroll:(UIScrollView *)scrollV ...

  3. 【转】iOS学习之Autolayout(代码添加约束) -- 不错不错

    原文网址:http://www.cnblogs.com/HypeCheng/articles/4192154.html DECEMBER 07, 2013 学习资料 文章 Beginning Auto ...

  4. UIScrollview 与 Autolayout 的那点事

    原文  http://www.cocoachina.com/ios/20151221/14757.html 前言 自从写了 介绍Masonry 那篇文章以后 就一直有人对UIScrollView的那个 ...

  5. iOS学习之Autolayout

    转载自:http://vit0.com/blog/2013/12/07/iosxue-xi-zhi-autolayout/ 学习资料 文章 Beginning Auto Layout Tutorial ...

  6. UIScrollView在AutoLayout下的滚动问题

    使用Storyboard编写UI,设置支持AutoLayout. 在其中的一个场景上,添加一个UIScrollView,在对应的代码里增加 - (void)viewDidLoad { [super v ...

  7. 示例详解:UIScrollview 与 Autolayout 的那点事

    前言 自从写了介绍Masonry那篇文章以后 就一直有人对UIScrollView的那个例子不是很理解 UIView *container = [UIView new]; [scrollView ad ...

  8. IOS UIScrollView + UIButton 实现segemet页面和顶部标签页水平滚动效果

    很长一段时间没有写博客了,最近在学习iOS开发,看了不少的代码,自己用UIScrollView和UIButton实现了水平滚动的效果,有点类似于今日头条的主界面框架,效果如下: 代码如下: MyScr ...

  9. iOS - UIScrollView

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIScrollView : UIView <NSCoding> @available(iOS 2.0, ...

随机推荐

  1. [CSS] Re-order the appearance of grid items using the order property

    As with flex items, we can set an order value on grid items. Let’s see how this affects the DOM and ...

  2. 基于 Android NDK 的学习之旅----- C调用Java

    许多成熟的C引擎要移植到Android 平台上使用 , 一般都会 提供 一些接口, 让Android sdk 和 jdk 实现. 下文将会介绍 C 如何 通过 JNI 层调用 Java 的静态和非静态 ...

  3. Objective-C编码规范[译]

    原文链接 : The official raywenderlich.com Objective-C style guide 原文作者 : raywenderlich.com Team 译文出自 : r ...

  4. [React] Use React.cloneElement to Extend Functionality of Children Components

    We can utilize React.cloneElement in order to create new components with extended data or functional ...

  5. tky项目第②个半月总结

    在上一篇半月总结中,介绍了tky项目的整体架构.项目的进展情况.项目的优势与开发中存在的问题等.今天来聊聊这半个月中,项目中发生的事情. 在这半个月中,项目中有了较大的突破:成功通过了国家评測中心的測 ...

  6. oracle中imp导入数据中文乱码问题(转)

    (转自  http://blog.chinaunix.net/uid-186064-id-2823338.html) oracle中imp导入数据中文乱码问题 用imp命令向oracle中导入数据后, ...

  7. java中<T> T和T的区别?

    如果你希望 getMax 方法的返回值类型为 T,就要这样去定义getMax方法: public T getMax() 如果你希望 getMax 方法返回值的类型由调用者决定,那么就这么去定义 get ...

  8. Java、JVM、JRE、JDK等组件的理解

    .java ⇒(javac) .classs ⇒ (类加载器)转换后的 .class 文件 ⇒ (解释器)可执行代码 ⇒ (JIT 编译器)⇒ 机器码 0. 虚拟机 Java 有它的虚拟机:Java ...

  9. android打包SDK具体操作(包含第三方的jar一起打包)

    一.背景 因为最近编写的android项目,需要编写对应的SDK给别人使用,还好以前我都是拆成module写的,所以还不太费工夫,不过因为一些module里面包含第三方的jar,所以打包有点麻烦 二. ...

  10. [Android]使用化名(alias)功能防止相同资源的重复

    在为一个应用匹配不同资源文件的时候,有时可能需要在不同适配类型的资源路径下使用相同的资源文件,这时使用alias方法可以防止相同资源文件的重复,提高效率.以下摘自Android开发文档http://d ...