iOS: How To Make AutoLayout Work On A ScrollView

轉載自:?http://natashatherobot.com/ios-autolayout-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:

ScrollView TextFields Autolayout?ScrollView AutoLayout 5

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:

scrollview hierarchy

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:

constraints

The key to getting the constraints to work properly however, is adding an?Equal 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:

equal width option

Your constraints between the main View, ScrollView, and ContentView should look like this:

Equal Widths

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):

constraints

The constraints between the ContentView and ScrollView are now as follows with all constants set at 0:

scrollview to contentview

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:

storyboard views

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:

centered container view

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).

top 25?bottom -25

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.

轉載于:https://www.cnblogs.com/zsw-1993/p/4879192.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/273574.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/273574.shtml
英文地址,請注明出處:http://en.pswp.cn/news/273574.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

windows 中搭建Zookeeper的搭建

個人博客 :https://www.siyuan.run CSDN:https://blog.csdn.net/siyuan 微信小程序:思遠Y 下載 下載地址: https://mirrors.cnnic.cn/apache/zookeeper/ PS:zookeeper 從3.5.5以后的版本帶有bin標識的包,否…

strcmp函數和strcpy函數

(一)strcmp函數 strcmp函數是比較兩個字符串的大小,返回比較的結果。一般形式是: istrcmp(字符串,字符串); 當中,字符串1、字符串2均可為字符串常量或變量;i 是用于存放比較結果的整型變量。比較結果…

Vs Code:Remote SSH

Remote SSH 簡介 Remote - SSH 擴展允許您使用任何帶有 SSH 服務器的遠程計算機作為開發環境。由于幾乎每個桌面和服務器操作系統都有可配置的 SSH 服務器,因此該擴展可以在各種情況下大大簡化開發。 您可以: 在部署的同一操作系統上進行開發&#xff…

樣條之貝塞爾(Bezier)

我曾經發過兩篇關于貝塞爾的文章:數學圖形(1.47)貝塞爾(Bzier)曲線,數學圖形之貝塞爾(Bzier)曲面。那是使用我自己定義的腳本語言生成貝塞爾圖形。由于我自己定義的腳本語法功能有限,所以最多只能支持5次貝塞爾函數,而這里將實現N…

TCollector

TCollector tcollector is a client-side process that gathers data from local collectors and pushes the data to OpenTSDB. You run it on all your hosts, and it does the work of sending each hosts data to the TSD. tcollector是client-side(客戶端&…

設計模式 之 工廠模式

項目源碼:https://gitee.com/Jacob-gitee/DesignMode 個人博客:https://jacob.org.cn 女媧造人的故事 東漢《風俗通》記錄了一則神話故事:“開天辟地,未有人民,女媧搏黃土做人”,講述的內容就是大家非常熟…

設計模式 之 單例模式

項目源碼:https://gitee.com/Jacob-gitee/DesignMode 個人博客:https://jacob.org.cn 宗旨 Ensure a class has only one instance,and provide a global point of access to it.(確保某一個類只有一個實例,而且自行實例化并向整個…

如何實現滑動scrollview上下隱藏

問題描述現在有一個需求,就是一個界面如下ABCA固定在頂部,C固定在底部其中B是一個scrollview(也可能是listview),要實現,在向上滑動B的時候,A平滑的往上滑,同時C平滑的往下滑,直到消失&#xff…

設計模式 之 抽象工廠模式

項目源碼:https://gitee.com/Jacob-gitee/DesignMode 個人博客 :https://jacob.org.cn 女媧的失誤 工廠模式中講了女媧造人的故事。人是造出來了,世界也熱鬧了,可是低頭一看,都是清一色的類型,缺少關愛、仇…

strip 命令的使用方法

用途 通過除去綁定程序和符號調試程序使用的信息,降低擴展公共對象文件格式(XCOFF)的對象文件的大小。 語法 strip [ -V ] [ -r [ -l ] | -x [ -l ] | -t | -H | -e | -E ] [ -X {32 |64 |32_64 }] [ -- ] File ... 描…

設計模式 之 模板模式

項目源碼:https://gitee.com/Jacob-gitee/DesignMode 個人博客 :http://jacob.org.cn 女媧的失誤 工廠模式中講了女媧造人的故事。人是造出來了,世界也熱鬧了,可是低頭一看,都是清一色的類型,缺少關愛、仇…

使用Java高速實現進度條

基于有人問到如何做進度條,以下給個簡單的做法: 主要是使用JProgressBar(Swing內置javax.swing.JProgressBar)和SwingWorker(Swing內置javax.swing.SwingWorker) 有人肯定會說,不是用線程做的嗎…

Linux 安裝JDK

個人博客 :https://www.siyuan.run CSDN:https://blog.csdn.net/siyuan 微信小程序:思遠Y 安裝時使用到的命令: cd:切換目錄。 eg:cd / mkdir:創建目錄。 eg:mkdir jacob 創建單極目…

Css導航

<div> <ul> <li><a></a></li> <li><a></a></li> <li><a></a></li> .. </ul> </div> <li>中也可包含 <ul> <a></a> <li><a></a>&…

關于js的function.來自百度知道的回答,學習了.

在js中&#xff0c;創建一個函數對象的語法是var myFunction new Function(arg1,…,agrN, body);其中&#xff0c;該函數對象的N個參數放在 函數主體參數body的前面&#xff0c;即函數主體參數必須放在參數列表的最后&#xff0c;也可以無參數new Function(body)。你添加第三個…

Ribbon 支持的9大負載均衡策略

個人博客 &#xff1a;https://www.siyuan.run CSDN&#xff1a;https://blog.csdn.net/siyuan 微信小程序&#xff1a;思遠Y 線性輪詢策略&#xff1a; RoundRibbonRule BaseLoadBalancer 負載均衡器默認采用線性負載輪詢負載均衡策略。 工作流程&#xff1a; RoundRibbonRule…

fedora20開機啟動配置:systemctl

老版fedora中使用chkconfig配置開機啟動&#xff0c;fedora20中&#xff0c;使用chkconfig會出現各種問題。使用systemctl配置。 具體表格如下 轉載于:https://www.cnblogs.com/hh6plus/p/5548083.html

Mysql 字符操作函數相關

常用的字符串函數&#xff1a; 函數說明CONCAT(s1,s2&#xff0c;...)返回一個或多個待拼接的內容&#xff0c;任意一個為NULL則返回值為NULL。CONCAT_WS(x,s1,s2,...)返回多個字符串拼接之后的字符串&#xff0c;每個字符串之間有一個x。SUBSTRING(s,n,len)、MID(s,n,len)兩個…

“cvSnakeImage”: 找不到標識符

1>g:\project\opencv\helloopencv\helloopencv\helloopencv.cpp(74) : error C2065: “CV_VALUE”: 未聲明的標識符1>g:\project\opencv\helloopencv\helloopencv\helloopencv.cpp(74) : error C3861: “cvSnakeImage”: 找不到標識符 增加頭文件 #include <opencv2/l…

Shell 快速入門

個人博客 &#xff1a;https://www.siyuan.run CSDN&#xff1a;https://blog.csdn.net/siyuan 微信小程序&#xff1a;思遠Y 概述 Shell 是一個用 C 語言編寫的程序&#xff0c;它是用戶使用 Linux 的橋梁。Shell 既是一種命令語言&#xff0c;又是一種程序設計語言。 Shell…