iOS: How To Make AutoLayout Work On A ScrollView
iOS: How To Make AutoLayout Work On A ScrollView
Posted on June 11th, 2014
Ok, I’ll admit. I’ve been seriously struggling with AutoLayout ever since it’s been introduced. I understand the concept, and I LOVE the idea of it, but when I actually do it, it almost never behaves as it does in my head.
So when I had a chance to go talk to an actual Apple Engineer about AutoLayout last week at WWDC, I made sure to go. I thought of my most painful experience using AutoLayout recently – when I was making a login screen with username and password fields on a ScrollView (so it scrolls up when the keyboard comes up) – and had the Apple engineer walk me through the example.
Here is what we made:
This is just two input fields centered on a ScrollView. You can see the AutoLayout at work here – the two input fields are centered correctly both on a 4s and a 5s device.
This “simple” solution took the Apple Engineer 40 minutes to solve! However, several senior engineers I know said that they’ve never been able to get AutoLayout working quite right on a ScrollView, so 40 minutes is actually not bad!
Here are the key tricks to getting AutoLayout to work on a ScrollView:
One View
The ScrollView should have only ONE child view. This is forced in Android, so I should have made the connection, but I just didn’t think of it – it’s too easy to put the two input text fields right onto the ScrollView, especially in Interface Builder.
Here is what the View Hierarchy should actually look like:
Again, make sure to put all your fields and custom views inside the one child view of the ScrollView!
Equal Widths
I’m going to start with the constraints from the outside (on the main view) in (to the stuff inside the ContentView).
The obvious starting point is to bind the ScrollView to the View – just select the ScrollView from the view hierarchy, and add the following constraints:
The key to getting the constraints to work properly however, is adding anEqual Width constraint between the main View and the ContentView. The ScrollView adjusts to the size of the content inside of it, so setting the ContentView to the Width of the ScrollView leaves the width of the ContentView ambiguous.
To create the Equal Width Constraint between the ContentView and the View, select the ContentView on the view hierarchy and Control + Drag to the View – you should get a pop-up that gives you the “Equal Widths” option:
Your constraints between the main View, ScrollView, and ContentView should look like this:
Content Insets
The constraints between the ScrollView and the ContentView are surprisingly straight forward – just bind the ContentView to the ScrollView (make sure the constant to the bottom layout guide is 0):
The constraints between the ContentView and ScrollView are now as follows with all constants set at 0:
If your storyboard is like mine, you might notice that the actual ContentView is not the full height of the main view or the ScrollView:
However, we do want to make sure the ContentView is centered when it’s rendered on a device. To do that we need to write some code to property set the Content Insets in the ViewController:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
// ViewController.swift import UIKit class ViewController: UIViewController { @IBOutlet var scrollView : UIScrollView @IBOutlet var contentView : UIView override func viewDidLoad() { super .viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func viewDidLayoutSubviews() { let scrollViewBounds = scrollView.bounds let containerViewBounds = contentView.bounds var scrollViewInsets = UIEdgeInsetsZero scrollViewInsets.top = scrollViewBounds.size.height/2.0; scrollViewInsets.top -= contentView.bounds.size.height/2.0; scrollViewInsets.bottom = scrollViewBounds.size.height/2.0 scrollViewInsets.bottom -= contentView.bounds.size.height/2.0; scrollViewInsets.bottom += 1 scrollView.contentInset = scrollViewInsets } } |
Once you add the proper constraints into the ContentView (see next step), your final result will look like this:
The ugly colors are meant to differentiate the ScrollView (green) from the ContentView (red). Again, in the storyboard, the ContentView is at the top of the ScrollView, but with our content insets set in code, it now becomes centered.
Centering Multiple Views
The final step is to add AutoLayout to the ContentView. This is the same as adding layout normally to any view, so I won’t go into much detail here.
The one thing I did learn that I’d like to share (although now it seems obvious) is how to center the two text fields in the view. Previously, I put the two text fields into a container view, and centered the container view in the parent view. However, that is not necessary.
Instead, you can center each text field horizontally in container (so they’re now centered and on top of each other), and then add a constant of 25 to one (so it’s moved up 25 pixels from the center), and add a constant of -25 to the other (so it’s moved down 25 pixels from the center).
This will leave you with a space of 50 pixels between the two text fields, but the space exactly in between them will be the center of the view.
Do you have any other AutoLayout tips? I’m always looking to learn more and improve, so please let me know in the comments!
You can view the source code on Github here.
Enjoy the article? Join over 8,500+ Swift developers and enthusiasts who get my weekly updates.
Subscribe
iOS: How To Make AutoLayout Work On A ScrollView的更多相关文章
- iOS开发——使用基于Autolayout的ScrollView
问题描述: 在使用Autolayout布局方式对ScrollView进行布局时,如果你想做一个可以垂直方向滚动的九宫格类似这样: 拿一行来说,一定不要想当然的尝试去给一行图标进行均匀排列的操作(指 ...
- iOS 屏幕适配:autoResizing autoLayout和sizeClass
1. autoResizing autoresizing是苹果早期的ui布局适配的解决办法,iOS6之前完全可以胜任了,因为苹果手机只有3.5寸的屏幕,在加上手机app很少支持横屏,所以iOS开发者基 ...
- 一篇文章详解iOS之AutoResizing、AutoLayout、sizeClass来龙去脉
前言 iPhone自诞生以来,随着其屏幕尺寸不断的多样化,屏幕适配的技术一直在发展更新.目前,iOS系统版本已经更新到9.3,XCode的最新版本已经是7.3,仅iPhone历史产品的尺寸就已经有4种 ...
- iOS开发——modifying the autolayout engine from a background thread
很多时候,我们需要用到多线程的东西,比如红外线检测是否有人经过.这种情况需要搞个子线程在后台不断的检测,这个线程可能是第三方提供的,你调用它给的方法,然后显示提示框的时候,问题就来了. 提示信息:Th ...
- iOS 8 UI布局 AutoLayout及SizeClass(二)
一.新特性Size Class介绍 随着iOS8系统的公布,一个全新的页面UI布局概念出现,这个新特性将颠覆包含iOS7及之前版本号的UI布局方式,这个新特性就是Size Class. Size Cl ...
- iOS 小 Tip:优化侧滑返回与 ScrollView 的兼容性
http://www.cocoachina.com/ios/20150909/13369.html 作者:@周楷雯Kevin 授权本站转载. 倘若在 ViewController 中添加了一个 Tab ...
- iOS学习笔记——AutoLayout的约束
iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...
- iOS屏幕适配
## iOS屏幕适配 ### iOS屏幕适配发展史 1> iPhone4以前(没有iPad) * 不需要屏幕适配 2> iPad.iPhone5等设备出现 * 需要做横竖屏适配 * aut ...
- iOS开发UI篇—UIScrollView控件介绍
iOS开发UI篇—UIScrollView控件介绍 一.知识点简单介绍 1.UIScrollView控件是什么? (1)移动设备的屏幕⼤大⼩小是极其有限的,因此直接展⽰示在⽤用户眼前的内容也相当有限 ...
随机推荐
- thinkjs中自定义sql语句
一直以为在使用thinkjs时,只能是它自带的sql语句查询,当遇到类似于这样的sql语句时,却不知道这该怎样来写程序,殊不知原来thinkjs可以执行自定义sql语句 SELECT * from a ...
- sq楼盘信息
Sq楼盘信息 1.1按关注度来分 前10个 序号 楼盘 网址 价格 关注人数 地址 附件社区 1 上海公馆 http://shangqiu.jiwu.com/loupan/236459.html 38 ...
- 不同json如何使用jsonArray以及ajax如何取,实现读取新闻
jsp界面 <%@ page contentType="text/html;charset=gb2312"%><%@page import="org.j ...
- Yii2中request的使用
1.普通的get和pst请求 $request = Yii::$app->request; $get = $request->get(); // equivalent to: $get = ...
- 《JavaScript高级程序设计》读书笔记--前言
起因 web编程过程使用javascript时感觉很吃力,效率很低.根本原因在于对javascript整个知识体系不熟,看来需要找些书脑补一下,同时欢迎众网友监督. 大神推荐书籍 看了博客大神们推荐的 ...
- TRACERT命令
这半个月 服务器从联通线路换到移动线路 导致基层用联通和电信线路的用户 上不去收费软件 tracert 120.194.42.142:8090 发现路由器 解析地址绕过很多条街后 出现丢包现象 联系 ...
- Lombok简化Java代码
导包:import lombok.Data; Lombok简化Java代码: 在Lombok中,生成构造方法的annotation一共有三个:@NoArgsConstructor, @Required ...
- MindManager使用说明
MindManager是一款很好实现思维导图的软件,唯一有些遗憾的是它并不是免费的,而且价格还不菲. 初识MindManager 正确安装好MindManager之后,打开软件,会发现MindMana ...
- [C++中级进阶]001_C++0x里的完美转发到底是神马?
[C++中级进阶]001_C++0x里的完美转发到底是神马? 转载至:http://www.cnblogs.com/alephsoul-alephsoul/archive/2013/01/10/285 ...
- 调试 zeromq 发现 accept 死循环
起因:在群里一个同学说使用 zeromq 的时候出了点儿问题,问题描述如下“router连接十几万客户端后,然后把router杀死,重启,这时候zeromq的某个线程99%的cpu,卡死了,再也接受不 ...