View的创建 |
创建 | storyboard上操作 |
与代码连接 | ● 目的: 通过代码控制view ● 按住option拖动 |
View的坐标 |
view的坐标 | 左上为原点, 往右+x, x为width 往下+y, y为height |
CGRect | 处理x, y, width, height的构造体 ● 生成:
CGRect(x: x, y: y, width: width, height: height)
● 属性
minX midX maxX | | minY midY maxY | | | |
|
CGPoint | 处理坐标x, y的构造体 ● 生成:
|
CGSize | 处理长宽width, height的构造体 ● 生成:
CGSize(width: width, height: height)
|
属性 |
frame | 表示在父view中位置的CGRect | bounds | 以自己为中心的CGRect ● 原点自己左上 |
|
| |
在程序里创建View |
| addSubView(view: UIView)
let viewByCode = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 20, height: 20))
viewByCode.backgroundColor = UIColor.green
topAreaView.addSubview(viewByCode)
|
| |
UIView |
| 所有UI都是UIView的子类 |
| ● 设定坐标 CGRect ● 设定border ● 增加subView addSubView ● 下面的 |
属性 |
contentMode | 对应项目条: Content Mode | isUserInteractionEnabled | 是否接受Event 对应项目条: User Interaction Enabled | alpha | 不透明度 对应项目条: alpha | backgroundColor | 背景色 对应项目条: Background | isHidden | 是否隐藏 对应项目条: Hidden | clipsToBounds | 超过边界的是否删除 效果相当于css的overflow: hidden 对应项目条: Clip to Bounds |
|
方法 |
removeFromSuperView | 从所属的super view里删除自己 | bringSubView(toFront:) | 把指定的sub view移到最前面 | sizeToFit | 根据内容调整大小 | | |
|
| |
| |
UIImageView |
| ● 用于展示图片 |
content mode | 对应项目条: content mode
Scale to Fill | 伸缩图片填满view | Aspect Fit | 保持宽高比放入view | Aspect Fill | 保持原尺寸填满view 多余部分溢出, 设置Clip to Bounds来切除溢出部分 | 方位(Left, Right, ...) | 靠某一边对齐, 尺寸不变 |
|
| |
UIScrollView |
| UITableView, UICollectionView的父类 |
设置滚动内容大小 | ● 通过autolayout自动设置 ● contentSize属性 |
带项目条的属性 |
indicatorStyle | 滚动条的款式 对应项目条: Indicators | showsHorizontalScrollIndicator | 是否显示水平滚动条 对应项目条: Shows Horizontal Indicator | showsVerticalScrollIndecator | 是否显示垂直滚动条 对应项目条: Shows Vertical Indicator | isScrollEnabled | 是否可滚动 对应项目条: Scrolling Enabled | isDirectionLockEnabled | 滚动中是否固定方向 对应项目条: Direction Lock Enabled | keyboardDismissMode | 滚动时是否关闭键盘 ● 内部有text field/text view是自动设为有效 对应项目条: Keyboard | alpha | 不透明度 对应项目条: Alpha | backgroundColor | 背景色 对应项目条: Background | Hidden | 是否隐藏 对应项目条: isHidden | | |
|
其他属性 |
contentSize | 滚动内容的大小 | scrollsToTop | 是否触击状态栏返回最顶部 默认 true | | | | | | |
|
方法 |
scrollRectToVisible(rect: animated:) -> Void | | | | | |
|
| |
| |
UITableView |
重复利用cell | ● 提高运行效率
注册利用的cell | ● 用代码注册则为代码里的状态. 也就是说storyboard上的设定全都不生效 ● 不要代码注册, 直接storyboard上注册
register(cellClass: forCellReuseIdentifier:) -> Void
例
middleTableView.register(MiddleTableViewCell.self, forCellReuseIdentifier: "sample")
| 使用注册的cell | 存在的时候用存在的, 不存在则自动新建
dequeueReusableCell(withIdentifier: for: )
例:
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)
|
|
datasource | 必须继承UITableViewDataSource ● 分出去单独类时, 要继承NSObject
必须的方法 |
设置session的行数 |
func tableView(_ tableView: UITableView numberOfRowsInSection section: Int) -> Int
| 设置每行表示的cell |
func tableView(_ tableView: UITableView cellForRowAt indexPath: IndexPath) -> UITableViewCell
● 调用时间: 生成cell, 重绘cell时(如划动时移出view的cell) | | |
| 其他方法 |
设置section数 |
func numberOfSections(in tableView: UITableView) -> Int
| 设置section的header标题 |
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
| | | | | | | | | | |
| | | | | | |
|
delegate | 必须继承UITableViewDelegate ● 分出去单独类时, 要继承NSObject ● 没有必须的方法, 可以不实现
|
| |
| |
| |
| |
UICollectionView |
| 和UITableView差不多 |
注册与 再利用cell |
注册 | ● 用代码注册则为代码里的状态. 也就是说storyboard上的设定全都不生效 ● 不要代码注册, 直接storyboard上注册
bottomCollectionView.register(BottomCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
| 再利用 | 存在的时候用存在的, 不存在则自动新建
collectionView.dequeueReusableCell(withReuseIdentifier: self.reuseIdentifier!, for: indexPath)
|
|
dataSource | 必须继承UICollectionViewDelegate ● 分出去单独类时, 要继承NSObject ● 没有必须的方法, 可以不实现
必须 方 法 |
设置元素数 |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
| 设置cell |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
|
| | |
|
delegate | 必须继承UICollectionViewDelegate ● 分出去单独类时, 要继承NSObject ● 没有必须的方法, 可以不实现 |
| |
| |