美文网首页
iOS - frame和bounds的区别

iOS - frame和bounds的区别

作者: 搬砖的crystal | 来源:发表于2022-07-20 14:19 被阅读0次
    view.frame = CGRectMake(CGFloat x, CGFloat y, CGFloat 
width, CGFloat height)

    view.bounds = CGRectMake(CGFloat x, CGFloat y, 
CGFloat width, CGFloat height)

两者的共同点是后面两个参数都是设置控件的大小,区别是前两个参数,对于 frame 来说,CGFloat x、 CGFloat y 两个参数是 view 相对于父视图的位置,boundsview 上面的子视图在 view 上面的起始位置。

    UIView *viewA = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    [self.view addSubview:viewA];
    viewA.backgroundColor = [UIColor orangeColor];
    
    UIView *viewB = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [viewA addSubview:viewB];
    viewB.backgroundColor = [UIColor grayColor];

修改 viewAbounds
    viewA.bounds = CGRectMake(20, 20, 100, 100);

相关文章

网友评论

      本文标题:iOS - frame和bounds的区别

      本文链接:https://www.haomeiwen.com/subject/hjzjirtx.html