Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically adjust content insets for views with custom a tab bar

I created a custom tab bar controller. It works very similar to UITabBarController but with a more advanced layout for the UITabBar.

How do I adjust the bottom content insets for views that appear in my custom tab bar controller? For instance if I show a UITableView in my custom tab bar controller I can manually adjust the content insets like this.

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 49, 0);

However the problem gets more complicated if I push another view onto this table view like a collection view. Is there a way to get these views to automatically adjust their content insets like they do in the default UITabBarController implementation?

like image 945
Berry Blue Avatar asked Mar 12 '17 18:03

Berry Blue


1 Answers

We've recently done something similar in our app, and ended up simply having a container view that holds any content we're going to display in the controller. The advantage of this approach is that you can add whatever constraints you'd like to the container view (such as bottom padding between the container and the tab bar).

Container view with bottom padding to tab bar.

Edit: Misunderstood the question

You can simply check that the content view is able to setContentInset:

- (void)setContentInset:(UIEdgeInset)insets {

    if ([self.contentView respondsToSelector:@selector(setContentInset:)]) {
        [self.contentView setValue:insets forKey:@"contentInset"];
    }
}
like image 78
Allan Poole Avatar answered Oct 09 '22 20:10

Allan Poole