我使用 UITableView 实例作为我的 ViewController 视图的子视图,表格的内容高于屏幕,因此它是可滚动的。下面是应用委托中的代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)opts {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *nav = [[UINavigationController alloc] init];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
UIViewController *controller = [[ViewController alloc] init];
[nav pushViewController:controller animated:YES];
// set to No cause the problem
nav.navigationBar.translucent = NO;
return YES;
}
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] init];
cell.textLabel.text = [NSString stringWithFormat:@"test %u", indexPath.row];
return cell;
}
@end
现在的问题是
- 如果
nav.navigationBar.translucent = YES
,内容向右滚动,如果我滚动到底部,则可以看到底线。 - 如果
nav.navigationBar.translucent = NO
,内容没有正确滚动,如果我结束滑动,底线会自动移出屏幕。
似乎自动隐藏区域的高度是导航栏,有人知道如何修复它吗?我希望它无论translucent = YES
或translucent = NO
都一样工作。
试试这个,让我知道它是否有效:
self.tableView.contentInset = UIEdgeInsetsMake(44,0,0,0);
我想出了解决方案,我将表视图设置为控制器的主视图,而不是控制器主视图的子视图
[self setView:_tableView];
而不是
[self.view addSubview:_tableView];