Allen Allen - 1 month ago 24
iOS Question

UITableView contentInset changes

I have set the contentInset of the tableView in the method

- (void)viewDidLoad
and the property is
self.tableView.contentInset = UIEdgeInsetsMake(200, 0, 200, 0);

however ,
tableView.contentInset
will be changed automaticly to :
{64, 0, 0, 0}
.Why this happened, and how can I slove this problem?

Environment: XCode8.1 iOS10.1

#import "YAVideoViewController.h"
static NSString * const ID = @"cell";
@interface YAVideoViewController ()

@end

@implementation YAVideoViewController

- (void)viewDidLoad {
[super viewDidLoad];



self.view.backgroundColor = [UIColor arcColor];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];

self.tableView.contentInset = UIEdgeInsetsMake(200, 0, 200, 0);
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return 50;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];

cell.textLabel.text = [NSString stringWithFormat:@"第%ld个cell",indexPath.row];

return cell;
}

@end

Answer

You need change the content inset in viewDidAppear or viewDidLayoutSubviews method.

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.tableView.contentInset = UIEdgeInsetsMake(200, 0, 200, 0);
}