Swift - 使用Auto Layout和Size Classes实现页面自适应弹性布局
在过去只有iphone4的时候,可以在代码里将一个可视单元的位置写死,这样是没问题的,但随着iPhone5,6的发布,屏幕尺寸有了越来越多种可能。这就要求App的UI控件具有在不同屏幕尺寸的设备上具有一定动态的可调性,实现较好的UI展示效果。
|
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
import UIKitclass ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //创建组件 var textField = UITextField(frame: CGRectZero) //这里不再需要刻意制定x,y坐标 textField.borderStyle = UITextBorderStyle.RoundedRect self.view.addSubview(textField) var button = UIButton.buttonWithType(UIButtonType.System) as UIButton; button.setTitle("按钮", forState:UIControlState.Normal) button.backgroundColor = UIColor(red: 55/255, green: 186/255, blue: 89/255, alpha: 0.5) self.view.addSubview(button) var textView = UITextView(frame: CGRectZero) textView.text="hangge.com" textView.backgroundColor = UIColor(red: 55/255, green: 186/255, blue: 89/255, alpha: 0.5) self.view.addSubview(textView) //使用Auto Layout的方式来布局 textField.setTranslatesAutoresizingMaskIntoConstraints(false) button.setTranslatesAutoresizingMaskIntoConstraints(false) textView.setTranslatesAutoresizingMaskIntoConstraints(false) //创建一个控件数组 var views:NSMutableDictionary = NSMutableDictionary() views.setValue(textField, forKey: "textField") views.setValue(button, forKey: "button") views.setValue(textView, forKey: "textView") //创建一个水平居中约束(按钮) var constraint:NSLayoutConstraint = NSLayoutConstraint(item: button, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1.0, constant: 0.0) self.view.addConstraint(constraint) //创建水平方向约束 self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat( "H:|-5-[textField]-5-|", options: nil, metrics: nil, views: views)) self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat( "H:|-5-[textView]-5-|", options: nil, metrics: nil, views: views)) //创建垂直方向约束 self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat( "V:|-20-[textField]-20-[textView]-20-[button]-20-|", options:nil, metrics: nil, views: views)) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }} |
二,Size Classes(适配各类型的屏幕)
Classes特性,是对老式UI思路的全新抽象,把所有设备(iPhone4、5、6、iPad、Apple
Watch)屏幕抽象成屏幕Size的变化,把屏幕的宽和高分成三种情况:紧凑(Compact)、任意(Any)和正常(Regular),这样宽和高
3*3共9种布局。


Swift - 使用Auto Layout和Size Classes实现页面自适应弹性布局的更多相关文章
- 关于IOS的屏幕适配(iPhone)——Auto Layout和Size Classes
Auto Layout和Size Classes搭配使用极大的方便了开发者,具体如何使用Auto Layout和Size Classes大家可以参考其他文章或者书籍,这里只提一点,在我们设置Size ...
- Swift语言Auto Layout入门教程:上篇
原文:Beginning Auto Layout Tutorial in Swift: Part 1/2,译者:@TurtleFromMars 开始用自动布局约束的方式思考吧! 更新记录:该教程由Br ...
- 从 Auto Layout 的布局算法谈性能
这是使用 ASDK 性能调优系列的第二篇文章,前一篇文章中讲到了如何提升 iOS 应用的渲染性能,你可以点击 这里 了解这部分的内容. http://t.cn/Rc4KbUC 在上一篇文章中,我们提到 ...
- 深入理解Auto Layout 第一弹
本文转载至 http://zhangbuhuai.com/2015/07/16/beginning-auto-layout-part-1/ By 张不坏 2015-07-16 更新日期:2015-07 ...
- 有了Auto Layout,为什么你还是害怕写UITabelView的自适应布局?
本文转载至 http://www.cnblogs.com/ios122/p/4832859.html Apple 算是最重视应用开发体验的公司了.从Xib到StoryBoard,从Auto Layou ...
- 【转】有了Auto Layout,为什么你还是害怕写UITabelView的自适应布局?
Apple 算是最重视应用开发体验的公司了.从Xib到StoryBoard,从Auto Layout到Size Class,每一次的更新,都会给iOS应用的开发带来不小的便利.但是,对于绝对多数i ...
- swift 约束 - SnapKit 适配iPhoneX 安全区 和苹果自带的VFL ,auto layout 安全区适配
这里tableview 是从最顶上的安全区适配的, nextBtn是最下边从安全区设置的,如果是在中间的view还是原来的写法,看2 1.安全区适配适用于Vc里面, 如果是自定义的view或封装的vi ...
- iOS8 Size Classes的理解与使用
在iOS8中,新增了Size Classes特性,它是对当前所有iOS设备尺寸的一个抽象,也是该抽象了,想想现在多少种iOS尺寸的设备吧:iPhone4-5-6-6plus.iPad.iPad min ...
- Auto Layout 使用心得
此系列文章代码仓库在 https://github.com/johnlui/AutoLayout ,有不明白的地方可以参考我的 Auto Layout 设置哦,下载到本地打开就可以了. 简介 Auto ...
随机推荐
- ThinkPHP - 前置操作+后置操作
前置操作和后置操作 系统会检测当前操作(不仅仅是index操作,其他操作一样可以使用)是否具有前置和后置操作,如果存在就会按照顺序执行,前置和后置操作的方法名是在要执行的方法前面加 _before ...
- c.Tom and paper
Tom and paper Description There is a piece of paper in front of Tom, its length and width are intege ...
- 在vmware里面免费安装纯净的xp虚拟机
1. 安装vmware, 略 2. 下载xp http://msdn.itellyou.cn/ 用迅雷下载Windows XP Professional with Service Pack 3 (x8 ...
- 【centos6 , 7】 网络原理、网络配置
第一部分:网络原理: 一.网络编址 (主要使用IP编址) 1.ip编址是一个双层编址方案,一个ip地址标识一个主机(或一个网卡接口) 2.现在应用最为广泛的是IPv4编址,已经开始逐渐向IPv6编址转 ...
- 深入理解-HashMap
一.HashMap概述 HashMap 在家族中位置:实现了Map接口,继承AbstractMap类.HashMap 允许key/value 都为null. 二.HashMap存储结构 HashMap ...
- 7.2.1 生成1~n的排列(全排列)【STL__next_permutation()_的应用】
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> ...
- Ajax 实现无刷新分页
Ajax 实现无刷新分页
- 使用jquery获取ul的li的值赋值
jquery:$('#dropdownMenu1').val(str);不jquery:document.getElementById('dropdownMenu1').value = str;
- Android 电话自己主动接听和挂断具体解释
1.通过aidl及反射实现挂断电话 详细分三步: (1)ITelephony.aidl ,必须新建com.android.internal.telephony包并放入ITelephony.aidl文件 ...
- 你跟大牛之间仅仅差一个google
google在中国被墙的厉害,http://209.116.186.231/ 这个地址能够訪问google.另外.有VPN或者某个奇妙的浏览器也能够. 非技术人员,还能够凑合着用百度,可是技术人员必须 ...