目录
UIView 简介
- UIView 是界面上面的一个矩形区域,管理界面上的内容;
- 在运行期间,UIView 处理内容的渲染和内容的交互;
- UIView 实现了内容展示的最基本方法和属性,特殊的行为由子类实现,例如:UIButton、UILabel 等;
- 在 UIkit 框架中包括了丰富的视图组件从基本的 UIButton 到复杂的 UItable 等;
- 视图的基本功能概括如下几方面:
- 绘制和动画
- 布局和子视图管理
- 时间处理
创建 UIView
UIView *v = [[UIView alloc]initWithFrame:CGRectMake(200, 200, 200, 200)];
v.backgroundColor = [UIColor redColor];
[self.view addSubview:v];
可视化外观(部分常用的)
View’s Visual AppearanceConfiguring a View’s Visual Appearance
backgroundColor
The view’s background color.
背景颜色
hidden
A Boolean value that determines whether the view is hidden.
是否隐藏
alpha
The view’s alpha value.
透明度 0.0-1.0
opaque
A Boolean value that determines whether the view is opaque.
不透明的 YES or NO
tintColor
The first nondefault tint color value in the view’s hierarchy, ascending from and starting with the view itself.
tintAdjustmentMode
The first non-default tint adjustment mode value in the view’s hierarchy, ascending from and starting with the view itself.
clipsToBounds
A Boolean value that determines whether subviews are confined to the bounds of the view.
clearsContextBeforeDrawing
A Boolean value that determines whether the view’s bounds should be automatically cleared before drawing.
maskView
An optional view whose alpha channel is used to mask a view’s content.
layerClass
Returns the class used to create the layer for instances of this class.
layer
The view’s Core Animation layer used for rendering.
Configuring the Event-Related Behavior
userInteractionEnabled
A Boolean value that determines whether user events are ignored and removed from the event queue.
点击是否有效
multipleTouchEnabled
A Boolean value that indicates whether the view receives more than one touch at a time.
多点触屏
exclusiveTouch
A Boolean value that indicates whether the receiver handles touch events exclusively.
接受管理是否唯一
Configuring the Bounds and Frame Rectangles 重要!
frame
The frame rectangle, which describes the view’s location and size in its superview’s coordinate system.
描述在父视图坐标系统中的位置和大小
bounds
The bounds rectangle, which describes the view’s location and size in its own coordinate system.
描述在自己的坐标系统中的位置和大小
center
The center point of the view’s frame rectangle.
frame的中心坐标,用来移动视图
transform
Specifies the transform applied to the view, relative to the center of its bounds.
转换,例如:移动、缩放、旋转等
Managing the View Hierarchy 视图的层次关系
superview
The receiver’s superview, or nil if it has none.
父视图,只有一个
subviews
The receiver’s immediate subviews.、
子视图,可以有多个
- addSubview:
Adds a view to the end of the receiver’s list of subviews.
添加
- removeFromSuperview
Unlinks the view from its superview and its window, and removes it from the responder chain.
删除
Configuring the Resizing Behavior 视图的大小调整
Define how a view adjusts its content when its bounds change.
contentMode
A flag used to determine how a view lays out its content when its bounds change.
当 bounds 改变时如何布局
autoresizingMask
An integer bit mask that determines how the receiver resizes itself when its superview’s bounds change.
父视图 bounds 改变时自身大小如何调整
autoresizesSubviews
A Boolean value that determines whether the receiver automatically resizes its subviews when its bounds change.
bounds 改变时其子视图是否跟着自动调整 YES or NO
Laying out Subviews 布局子视图
Lay out views manually if your app does not use Auto Layout.
- layoutSubviews
Lays out subviews.
布局子视图
- setNeedsLayout
Invalidates the current layout of the receiver and triggers a layout update during the next update cycle.
- layoutIfNeeded
Lays out the subviews immediately, if layout updates are pending.