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);
}