简体   繁体   中英

UITableView bottom area keeps out of screen if navigationBar.translucent is NO;

I use UITableView instance as the subview of my ViewController's view, the table's content is higher than the screen so it is scrollable. Here is the code in AppDelegate:

- (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

Now the problem is

  • if nav.navigationBar.translucent = YES , the content is scrolling right, the bottom line is visible if I scroll to bottom.
  • if nav.navigationBar.translucent = NO , the content isn't scrolling right, the bottom line automatically move out of the screen if I end the swip.

Seems like the auto hidden area is of the height of navigationBar, any one who knows how to fix it? I want it work the same no matter translucent = YES or translucent = NO .

试试这个,让我知道它是否有效:

self.tableView.contentInset = UIEdgeInsetsMake(44,0,0,0);

I figure out the solution, I set the tableView as the mainview of the controller, not a subview of the controller's mainview

[self setView:_tableView];

instead of

[self.view addSubview:_tableView];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM