OC学习3-控件学习

207 阅读2分钟

一、基础控件的使用

1、UIButton的使用 image1.png

2、UILabel的使用 image.png 3、UIImageView的使用 image (1).png

二、UITextField的使用

UITextField是用来处理文本输入的一个控件。 创建UITextField: image (2).png 监听键盘的动作,拉起,收起,监听输入框编辑。 键盘的收起: 1、点击Return收起键盘 需要设置textField代理,实现代理方法 textfiedlegate=self; image (3).png 2、添加点击手势,关闭键盘 image.png image.png

image.png

三、UITableView的使用

UITableView是系统为我们提供的最基础的列表展示组件,同时UITableView提供了默认的基础样式,以及可以添加header,footer以及一些cell的管理。同时UITableView提供了UITableViewCell的复用回收机制,通过使用系统的函数,可以让一个非常长的列表去实现屏幕中展示的cell的数量,其他的在滚动过程中复用的技术,提供列表基础功能,如点击,删除,插入等。 在使用上创建UITableView,设置delegate以及datasource,在当中选择需要的函数,了解使用场景,进而实现自定义的操作。 1、对TableView的初始化,设置样式,grouped样式和Plain样式不同点,同时注册cell (1)grouped的section Header,section Footer的背景色默认跟tableview的背景色一样。plain反之。 (2)grouped的section Header不会悬浮。plain的section Header是悬浮状态。 (3)plain会出现多余的分割线,grouped不会。 (4)grouped某一行的上线边缘默认会有分割线 2、在interface旁边设置数据源和代理 (1)实现数据源方法,包括(多少组数据,每组有几行,每行显示的内容,每组的头部尾部) image.png image.png image.png image.png (2)cell复用 //先注册cell image.png //通过复用回收逻辑展示cell,高效显示滚动列表 image.png

四、iOS自动布局AutoLayout

AutoLayout是一个系统,可以通过创建元素之间的关系数学描述来布局应用程序的用户界面,是一种基于约束的,描述性的布局系统,当描述完视图对象之间的约束后,AutoLayout会自动的计算出视图对象的位置和大小,也就间接的设定了视图的Frame。

1、AutoLayout的使用

1、在使用AutoLayout时。需要将视图的setTranslatesAutoresizingMaskIntoConstraints 属性设置为NO。 2、NSLayoutConstraint Auto Layout中约束的类为NSLayoutConstraint,一个NSLayoutConstraint实例代表一条约束。 +(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(nullable id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c; view1的某个属性(attr1)等于view2的某个属性(attr2)的值的多少倍(multiplier)加上某个常量(constant)。描述的是一个view与另外一个view的位置和大小约束关系。其中属性attribute有上、下、左、右、宽、高等。 image.png 2、Masonry的使用 //新增约束  - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block; //更新约束  - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block; //清楚之前的所有约束,只会保留最新的约束  - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block; 使用方法: image.png