美文网首页iOS SB
UI基础—实现AutoLayout

UI基础—实现AutoLayout

作者: 我是大龙啊 | 来源:发表于2016-02-14 09:36 被阅读34次

使用代码实现Autolayout的方法1

  • 创建约束
+(id)constraintWithItem:(id)view1
attribute:(NSLayoutAttribute)attr1
relatedBy:(NSLayoutRelation)relation
toItem:(id)view2
attribute:(NSLayoutAttribute)attr2
multiplier:(CGFloat)multiplier
constant:(CGFloat)c;
* view1 :要约束的控件
* attr1 :约束的类型(做怎样的约束)
* relation :与参照控件之间的关系
* view2 :参照的控件
* attr2 :约束的类型(做怎样的约束)
* multiplier :乘数
* c :常量
  • 添加约束
- (void)addConstraint:(NSLayoutConstraint *)constraint;
- (void)addConstraints:(NSArray *)constraints;
  • 注意
    • 一定要在拥有父控件之后再添加约束
    • 关闭Autoresizing功能
    view.translatesAutoresizingMaskIntoConstraints = NO;
    

使用代码实现Autolayout的方法2 - VFL

  • 使用VFL创建约束数组
+ (NSArray *)constraintsWithVisualFormat:(NSString *)format
options:(NSLayoutFormatOptions)opts
metrics:(NSDictionary *)metrics
views:(NSDictionary *)views;
* format :VFL语句
* opts :约束类型
* metrics :VFL语句中用到的具体数值
* views :VFL语句中用到的控件
  • 使用下面的宏来自动生成views和metrics参数
NSDictionaryOfVariableBindings(...)

使用代码实现Autolayout的方法3 - Masonry

  • 使用步骤
    • 添加Masonry文件夹的所有源代码到项目中
    • 添加2个宏、导入主头文件
    // 只要添加了这个宏,就不用带mas_前缀
    #define MAS_SHORTHAND
    

// 只要添加了这个宏,equalTo就等价于mas_equalTo

define MAS_SHORTHAND_GLOBALS

// 这个头文件一定要放在上面两个宏的后面

import "Masonry.h"

```
  • 添加约束的方法
// 这个方法只会添加新的约束
 [view makeConstraints:^(MASConstraintMaker *make) {

 }];

// 这个方法会将以前的所有约束删掉,添加新的约束
 [view remakeConstraints:^(MASConstraintMaker *make) {

 }];

 // 这个方法将会覆盖以前的某些特定的约束
 [view updateConstraints:^(MASConstraintMaker *make) {

 }];
  • 约束的类型
1.尺寸:width\\height\\size
2.边界:left\\leading\\right\\trailing\\top\\bottom
3.中心点:center\\centerX\\centerY
4.边界:edges
  • tableView如何显示数据
    • 设置dataSource数据源
    • 数据源要遵守UITableViewDataSource协议
    • 数据源要实现协议中的某些方法
/**
 *  告诉tableView一共有多少组数据
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

/**
 *  告诉tableView第section组有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

/**
 *  告诉tableView第indexPath行显示怎样的cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

/**
 *  告诉tableView第section组的头部标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

/**
 *  告诉tableView第section组的尾部标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

相关文章

  • UI基础—实现AutoLayout

    使用代码实现Autolayout的方法1 创建约束 添加约束 注意一定要在拥有父控件之后再添加约束关闭Autore...

  • UI基础8 Autolayout

    使用代码实现Autolayout的方法1 创建约束 添加约束 注意一定要在拥有父控件之后再添加约束关闭Autore...

  • 9.6AutoLayout约束基础

    AutoLayout约束基础 AutoLayout约束基础1.png AutoLayout约束基础2.png Au...

  • AutoLayout

    AutoLayout autolayout的概念 Autolayout是一种“自动布局”技术,专门用来布局UI界面...

  • Storyboard 中用户界面适配问题

    需求说明UI界面布局通过 Storyboard 实现,要求对界面进行横竖屏的适配; 使用了 Autolayout ...

  • 关于 Autolayout

    Autolayout 是基于 Cassowary 算法实现的一套 UI 布局算法,能够有效解析线性等式系统和线性不...

  • AutoLayout 自动布局

    Autolayout 简介 Autolayout 是一种“自动布局”技术,专门用来布局 UI 界面的 Autola...

  • Autolayout自动布局实现的方法一:storyboard的

    Autolayout简介 Autolayout是一种“自动布局”技术,专门用来布局UI界面的, Autolayou...

  • 什么是Autolayout

    什么是Autolayout Autolayout是一种“自动布局”技术,专门用来布局UI界面的 Autolayou...

  • Autolayout

    什么是Autolayout Autolayout是一种“自动布局”技术,专门用来布局UI界面的 Autolayou...

网友评论

    本文标题:UI基础—实现AutoLayout

    本文链接:https://www.haomeiwen.com/subject/quaukttx.html