[UIKit学习]01.关于UIView,frame,bounds,center

时间:2023-03-08 19:34:21

UIView是Cocoa大多控件的父类,本身不带事件。

UIView的常见用法

  • @property(nonatomic,readonly) UIView *superview;
  • 获得自己的父控件对象
  • @property(nonatomic,readonly,copy) NSArray *subviews;
  • 获得自己的所有子控件对象
  • @property(nonatomic) CGAffineTransform transform;
  • 控件的形变属性(可以设置旋转角度、比例缩放、平移等属性)(特别对于一些没法控制尺寸的控件,用tranform有奇效~~~
  • clipsToBounds=Yes;
  • 设置是否裁剪内容,就是当子控件超出父控件范围时,是否可见。

UIView的增删查

  • 增加    - (void)addSubview:(UIView *)view;
  • 删除    - (void)removeFromSuperview
  • 查询    - (UIView *)viewWithTag:(NSInteger)tag;

UIView的插入

// 将子控件view插入到subviews数组的index位置
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index; // 将子控件view显示到子控件siblingSubview的下面
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
// 将子控件view显示到子控件siblingSubview的上面
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview; // 将子控件view放到数组的最后面,显示在最上面
- (void)bringSubviewToFront:(UIView *)view;
// 将子控件view放到数组的最前面,显示在最下面
- (void)sendSubviewToBack:(UIView *)view;

frame

frame是相对于父控件的位置CGRectMake(x,y,w,h)

bounds

bounds是以自己左上角为坐标原点,所以一般x,y都是0

center

center指的是控件中心点相对于父控件的位置