设置 tableView 的内边距
注意: 不能将内边距设置在其父控件 scrollView 身上, 如果将内边距设置在了 scrollView 身上, 会导致 tableView 的也跟着下移, 然而上下滚动由 tableView 负责(第一响应者), 因此会导致 scrollView 不会往上移动, 从而导致 tableView 的内容不会再导航条下面出现, 导致不具备有穿透效果, 并且还会导致有部分内容(其高度就是设置的顶部内边距高度的内容)都会被 tabBar 挡住
// 取消自动调整scrollView的内边距 self.automaticallyAdjustsScrollViewInsets = NO;scrollView 负责左右滚动
// 设置 contenSize 可滚动范围 只负责左右滚动 scrollView.contentSize = CGSizeMake(5 * scrollView.xmg_width, 0); // 设置分页功能 scrollView.pagingEnabled = YES;
- 注意: 设置内边距是在 tableViewController 控制器 ViewDidLoad 方法中设置,而不是在精华控制器中统一设置
我的做法是先自定义一个 CDHTableViewController 类, 用来展示数据的那 5 个 tableViewController 继承这个类, 即可统一在这个类的 ViewDidLoad 中统一设置控制器内边距
#import "CDHTableViewController.h" @interface CDHTableViewController () @end @implementation CDHTableViewController - (void)viewDidLoad { [super viewDidLoad]; // 在这里统一设置内边距 self.tableView.backgroundColor = CDHRandomColor; self.tableView.contentInset = UIEdgeInsetsMake( CDHNavBarMaxy + CDHTitleViewH , 0, CDHTabBarH, 0); self.tableView.scrollIndicatorInsets = self.tableView.contentInset; }