bounds、frame、contentoffset及contentSize和contentInset的新认识

1、矩形框:控件自己的显示位置和尺寸(相当于frame)。
2、内容:控件内部的东西,比如它的子控件(相当于bounds,如果bounds改变它的内容或控件的位置就会发生变化)。
3、bounds:以控件自己内容的左上角为坐标点,计算出来的矩形框的位置和大小 。
4、frame:以父控件内容的左上角为坐标原点,计算出来的矩形框的位置和大小。

 

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    [redView setFrame:CGRectMake(100, 200, 300, 300)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
    [label setBackgroundColor:[UIColor blueColor]];
    [redView addSubview:label];
    [self.view addSubview:redView];
    self.redView = redView;
    //frame 是相当于框架
    //bounds 相当于内容
}
//touch began
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGRect bounds = self.redView.bounds;
    bounds.origin = CGPointMake(-50, -50);
    self.redView.bounds = bounds;
    //orgion变了,bounds中内容会发生改变,但frame框架没有改变,所以self.redView还没变
    //随着bounds的改变self.redViwiew的子控件Label会发生变化
}

5、UIScrollView的CGpoint与view视图的bounds一样。

6、 contentInset内边距

   6.1 但凡是属于ScrollView或ScrollView的子类在遇到页面有导航或导航和tabBar都会自动添加64的间距,比如UITableViewController

   6.2 关闭自动适应约束

 // @property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets NS_AVAILABLE_IOS(7_0); // Defaults to YES,默认是添加自动适应的
    self.automaticallyAdjustsScrollViewInsets = NO;

64,且只会对第一个继承自UIScrollViewf起作用。

 穿透效果就是这么做成的

总结:当tableVIew顶部间距不对,或者尾部显示不到位,都可以通过ContentInset来设置

 

posted @ 2017-03-09 22:13  TheYouth  阅读(344)  评论(0编辑  收藏  举报