ios frame和bounds区别

1. frames和 bounds理解

frame:     是子控件相对于 父控件内容左上角为坐标原点
bounds:    contentView相对于自己控件坐标位置,控件内部内容区域

滚动的时候frame和bounds体现:

frame和bounds理解图: 

如果有顶部内边距的时候,理解图:

程序代码: 

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>


@property (strong, nonatomic) UIScrollView *myscrollview;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    UIScrollView* scroll=[[UIScrollView alloc] init];
// 设置坐标
    scroll.frame= CGRectMake(100, 100, 200, 400);
//设置 水平不可以不能动,垂直可以滚动
    scroll.contentSize=CGSizeMake(0, 500);
    scroll.backgroundColor=[UIColor orangeColor];
  //  scroll.contentInset= UIEdgeInsetsMake(60, 0, 0, 0);
     self.myscrollview=scroll;
 // 设置代理
    scroll.delegate=self;
   // 添加
    [self.view addSubview:scroll];
    UISwitch* switch1= [[UISwitch  alloc] init];
    [self.myscrollview addSubview:switch1];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// 相等,根据上图理解
    NSLog(@"bounds=%@,offset=%@", NSStringFromCGPoint(self.myscrollview.bounds.origin),
          NSStringFromCGPoint(self.myscrollview.contentOffset));
// 相等,根据上图理解
NSLog(@"frame.size=%@,bounds.size=%@", NSStringFromCGSize(self.myscrollview.frame.size),
          NSStringFromCGSize(self.myscrollview.bounds.size));
}
@end

总结: self.myscrollview.bounds.origin)== 
          NSStringFromCGPoint(self.myscrollview.contentOffset)

     NSStringFromCGSize(self.myscrollview.frame.size) == 
          NSStringFromCGSize(self.myscrollview.bounds.size);

     设置内边距:
     scroll.contentInset= UIEdgeInsetsMake(60, 0, 0, 0);  不会影响bouds的size

2.   UITableView中默认 内边距和offset设置

uitableview显示内容的时候,如下图,会内容不会被导航栏覆盖,uitableview内部设置了默认的内边距和offset

内部代码设置: 

- (void)viewDidLoad {
    [super viewDidLoad];
    
      // self.tableView.automaticallyAdjustsScrollIndicatorInsets=NO; 
       // 设置sb中uiscrollview,uitableview不自动设置 内边距

          // 1. tableView会设置默认内边距,下滑避免内容被遮挡
   // self.tableView.contentInset= UIEdgeInsetsMake(64, 0, 0, 0);
  //  self.tableView.contentOffset = CGPointMake(0, -64);
    
    //  offset:{0, -64}
    NSLog(@"offset:%@",NSStringFromCGPoint( self.tableView.contentOffset));
     //  bounds={
   
   {0, -64}, {414, 736}}
    NSLog(@"bounds=%@",NSStringFromCGRect(self.tableView.bounds));
    
    // 2. 设置底部内边距,避免底部内容被遮挡
   //   self.tableView.contentInset=UIEdgeInsetsMake(0, 0, 49, 0);
}

猜你喜欢

转载自blog.csdn.net/dreams_deng/article/details/106891981