OC 日常筆記碎片知識
- 1-分析ScrollView系統(tǒng)什么時候調(diào)整內(nèi)邊距?
ViewController添加3個ScrollView,每個ScrollView添加開一個swich.
1.通過觀察發(fā)現(xiàn)并沒有修改內(nèi)邊距.
- 2-思考:如果添加了導(dǎo)航控制器呢?
2.橙色ScrollView內(nèi)邊距發(fā)生變化, 默認(rèn)首個控件會調(diào)整內(nèi)邊距.
- 3-思考:如果不是首個控件呢?
首個控件不是ScrollView,系統(tǒng)不會調(diào)整內(nèi)邊距.
- 4-如何解決額外增加的屬性
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 不要自動調(diào)整scrollView的內(nèi)邊距
//方法一
// self.automaticallyAdjustsScrollViewInsets = NO;
//方法二
self.scrollView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"%@", NSStringFromUIEdgeInsets(self.scrollView.contentInset));
}
@end
- 5-思考:蘋果為什么增加額外內(nèi)邊距?
為了不被導(dǎo)航控制器擋住可視控件.
研究TableView的ContentInset
*代碼演示:
#import "TestTableViewController.h"
@interface TestTableViewController ()
@end
@implementation TestTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// self.automaticallyAdjustsScrollViewInsets = NO;
// self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 30;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.確定重用標(biāo)示:
static NSString *ID = @"cell";
// 2.從緩存池中取
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 3.如果空就手動創(chuàng)建
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
cell.backgroundColor = [UIColor redColor];
}
cell.textLabel.text = [NSString stringWithFormat:@"%zd", indexPath.row];
return cell;
}
//點擊每行cell打印contentInset
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@", NSStringFromUIEdgeInsets(tableView.contentInset));
}
@end
- 1-無導(dǎo)航欄演示:
- 2-有導(dǎo)航欄與TabBar
通過添加導(dǎo)航欄與TabBar觀察發(fā)現(xiàn)有穿透效果.
-
3-官方文檔解釋
分析.png
這個屬性默認(rèn)是YES,允許控制器調(diào)整內(nèi)部的內(nèi)邊距,當(dāng)scrollView有狀態(tài)欄,導(dǎo)航欄, tabBar就會調(diào)整內(nèi)邊距.如何設(shè)置為NO,就手動調(diào)整.
- 4-歷史
設(shè)置tableView的背景顏色
從IOS7開始有穿透效果,是由內(nèi)邊距造成.
從iOS7開始,默認(rèn)情況下控制器的View占據(jù)正個屏幕.tableView的內(nèi)容有很多cell, 非常長.還可以往上移動,當(dāng)穿過導(dǎo)航欄就有穿透效果,超出父控件, Y值越大. 超出離開屏幕看不見被循環(huán)利用.
iOS7以前, 整個tableView不會填充屏幕, 假如屏幕高度480 , 會480 - 64 = 416. 那么屏幕高度是416.
內(nèi)容顯示上去是不會調(diào)整內(nèi)邊距,沒有穿透效果.
- 5-總結(jié):
contentInset的調(diào)整
默認(rèn)情況下, 如果一個控制器A處在導(dǎo)航控制器管理中, 并且控制器A的第一個子控件是UIScrollView, 那么就會自動調(diào)整這個UIScrollView的contentInsetUIEdgeInsetsMake(64, 0, 0, 0) // 有導(dǎo)航欄
UIEdgeInsetsMake(20, 0, 0, 0) // 沒有導(dǎo)航欄
默認(rèn)情況下, 如果一個控制器A處在導(dǎo)航控制器管理中, 并且導(dǎo)航控制器又處在UITabBarController管理中, 并且控制器A的第一個子控件是UIScrollView, 那么就會自動調(diào)整這個UIScrollView的contentInsetUIEdgeInsetsMake(64, 0, 49, 0)
如何禁止上述的默認(rèn)問題?
控制器A.automaticallyAdjustsScrollViewInsets = NO;