UIKit之UIView

#import "TestController.h"

@interface TestController ()

@property (strong, nonatomic)UIView *view1;
@property (strong, nonatomic)UIView *view2;

@end

@implementation TestController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加视图
    // 1. 把视图view1 插入view2的上面
    [self.view insertSubview:self.view1 aboveSubview:self.view2];
    // 2. 把视图view2 插入view的下面
    [self.view insertSubview:self.view1 belowSubview:self.view2];
    // 3. 把视图view2 插入索引是1的位置
    [self.view insertSubview:self.view2 atIndex:1];
    
    // 视图层次操作
    // 4. 把view2移到最前面
    [self.view bringSubviewToFront:self.view2];
    // 5. 把view2移到最后面
    [self.view sendSubviewToBack:self.view2];
    // 6. 根据索引交换两个视图
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    
    // 移除视图
    // 7. 从父视图移除
    [self.view2 removeFromSuperview];
    
    // 1. 隐藏
    self.view1.hidden = YES;
    // 2. 设置透明度
    self.view1.alpha = 0.5;
    // 3. 获取所有子视图
    NSArray *arr = self.view.subviews;
    // 4. 获取父视图
    UIView *superView = self.view1.superview;
    // 5. 给视图添加标签(被添加标签的view可以通过viewWithTag:方法获取)
    self.view1.tag = 100;
    UIView *tagView = [self.view viewWithTag:100];
}
#pragma mark - lazyload
- (UIView *)view1{
    if (!_view1) {
        _view1 = [UIView new];
        _view1.backgroundColor = [UIColor redColor];
        [self.view addSubview:_view1];
        [_view1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.offset(100);
            make.size.mas_equalTo(CGSizeMake(100, 100));
        }];
    }
    return _view1;
}

- (UIView *)view2{
    if (!_view2) {
        _view2 = [UIView new];
        _view2.backgroundColor = [UIColor blueColor];
        [self.view addSubview:_view2];
        [_view2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.offset(200);
            make.size.mas_equalTo(CGSizeMake(100, 100));
        }];
    }
    return _view2;
}

@end

Frame 和 Bounds

  • iPhone屏幕左上角 为我们开发时 frame原点
  • 但是当有子视图时,子视图的原点是其父视图的point
  • frame 是一个包含 位置point(x,y)和宽高size(width,height)的结构体
  • bounds 是以父类视图的point为原点创建的一个坐标系
// point
CGPoint point = CGPointMake(100, 100);
NSLog(@"%@",NSStringFromCGPoint(point));
NSLog(@"%f",point.x);

// size
CGSize size = CGSizeMake(100, 100);
NSLog(@"%@",NSStringFromCGSize(size));
NSLog(@"%f",size.height);

// frame
CGRect rect = CGRectMake(100, 100, 100, 100);
NSLog(@"%@",NSStringFromCGRect(rect));
NSLog(@"%@",NSStringFromCGRect(view.frame));
NSLog(@"%f",rect.origin.x);

// center:(表示view视图的中心点) 其坐标根据父视图的原点而定
NSLog(@"%@",NSStringFromCGPoint(view.center));

你可能感兴趣的:(UIKit之UIView)