iOS UIView (一)常用的基础属性

本专题整理了 UIkit 中基本的控件,及控件的一些常用属性。方便大家研究。

一、UIResponder 类别下

常用属性
userInteractionEnabled --- 是否可以响应事件
tag --- 当前view的标记,可以根据标记查找对应的view
layer --- 每一个view都对应一个CALayer,用来处理动画圆角等特效。CALayer 我也会单独出一个介绍
常用方法
- (instancetype)initWithFrame:(CGRect)frame;//根据frame初始化
- (nullable instancetype)initWithCoder:(NSCoder *)coder;//XIB中的初始化
不常用属性
layerClass --- 视图创建底层时使用
canBecomeFocused --- 是否可以成为焦点
focused --- 成为焦点
focusGroupIdentifier --- 焦点组的标识符
semanticContentAttribute --- 布局方向、屏幕转动view会重新布局,可以设置从右到左(button文字图片位置)、设置部分不需要重新布局的view
effectiveUserInterfaceLayoutDirection --- 返回合适的布局方向
不常用方法
+ (UIUserInterfaceLayoutDirection)userInterfaceLayoutDirectionForSemanticContentAttribute:(UISemanticContentAttribute)attribute API_AVAILABLE(ios(9.0)); --- 布局方向
+ (UIUserInterfaceLayoutDirection)userInterfaceLayoutDirectionForSemanticContentAttribute:(UISemanticContentAttribute)semanticContentAttribute relativeToLayoutDirection:(UIUserInterfaceLayoutDirection)layoutDirection API_AVAILABLE(ios(10.0)); --- 布局方向

二、UIViewGeometry 类别下

常用属性
frame --- 在父视图上的位置大小
bounds --- 自己的坐标原点//锚点位置大小(可以理解为左上角的初始值)
center --- 中心点,可以指定位置
multipleTouchEnabled --- 视图是否一次接收多个手势
exclusiveTouch --- 视图是否专门处理触摸事件
常用方法
- (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event; --- 获取坐标View中的位置
- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event; --- 触摸点是否在View边界内部
//更过获取位置的方法
- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;
- (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;
- (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;
- (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;

- (CGSize)sizeThatFits:(CGSize)size; --- 返回合适的尺寸
- (void)sizeToFit;  --- 使用当前边界调整大小,并更改边界
不常用属性
transform --- 用于产生形变
transform3D --- 用于产生3D形变
contentScaleFactor --- 试图的比例因子

三、UIViewHierarchy类别下

常用属性
superview --- 父视图
subviews --- 当前view的所有子视图
window --- 接收者的窗口对象
常用方法
- (void)removeFromSuperview; --- 从父视图移除
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index; --- 在指定位置插入
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2; --- 交换指定位置的视图

- (void)addSubview:(UIView *)view; --- 加入
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview; --- 加入在指定view下
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview; --- 加入在指定view上

- (void)bringSubviewToFront:(UIView *)view; --- 将指定view放在最顶端
- (void)sendSubviewToBack:(UIView *)view; --- 将指定view放在最底端
- (BOOL)isDescendantOfView:(UIView *)view;  --- 是不是自己的子视图
- (UIView *)viewWithTag:(NSInteger)tag; --- 根据tag获取子view

- (void)setNeedsLayout; --- 标记为需要重新绘制//下一个runloop
- (void)layoutIfNeeded; --- 立即重新绘制
- (void)layoutSubviews;  --- 重新绘制方法,代码不可以主动调用
不常用属性
layoutMargins --- 视图的内边界
directionalLayoutMargins --- 视图的内边界 (leading 来确定的)
preservesSuperviewLayoutMargins --- 边界是否传递
insetsLayoutMarginsFromSafeArea --- 边距是否需要更新
safeAreaInsets --- 安全区域
layoutMarginsGuide --- 视图的内边界(该边界与layoutMargins相等)
readableContentGuide --- 视图中具有可读宽度的区域
safeAreaLayoutGuide --- 视图中未被条形和其他内容遮挡的部分
不常用方法
以下方法系统会自动处理
- (void)didAddSubview:(UIView *)subview;
- (void)willRemoveSubview:(UIView *)subview;
- (void)willMoveToSuperview:(nullable UIView *)newSuperview;
- (void)didMoveToSuperview;
- (void)willMoveToWindow:(nullable UIWindow *)newWindow;
- (void)didMoveToWindow;

- (void)layoutMarginsDidChange; --- 边界更新完成
- (void)safeAreaInsetsDidChange ; --- 安全区域更新完成

四、UIViewRendering类别下

常用属性
clipsToBounds --- 是否裁剪超出部分
backgroundColor --- 背景颜色
alpha --- 透明度
hidden --- 隐藏
contentMode --- 填充模式,UIViewImage常用
maskView --- 其 alpha 通道用于屏蔽视图的内容(常用于遮罩)
tintColor --- 色调
常用方法
- (void)drawRect:(CGRect)rect; --- 重新绘制方法
- (void)setNeedsDisplay; --- 需要被重新绘图的标记
- (void)setNeedsDisplayInRect:(CGRect)rect; --- 需要被重新绘图的标记并给定位置
- (void)tintColorDidChange; --- 色调已经改变了
不常用属性
opaque --- 用于确定视图是否不透明
clearsContextBeforeDrawing --- 确定在绘制之前是否应自动清除视图的边界
contentStretch --- 定义可拉伸不可拉伸的区域(已经弃用 使用resizableImageWithCapInsets
)
tintAdjustmentMode --- 视图层次结构中的第一个非默认色调调整模式值,从视图本身开始上升

你可能感兴趣的:(iOS UIView (一)常用的基础属性)