iOS 7 Table view fail to auto adjust content inset

前端 未结 7 1625
情话喂你
情话喂你 2020-12-04 14:24

I am transiting my project to iOS7. I am facing a strange problem related to the translucent navigation bar.

I have a view controller and it has a tableview as subv

相关标签:
7条回答
  • 2020-12-04 15:20

    There's probably too many answers on this already, but I had to take Christopher's solution and modify it slightly to support view resizing and allowing the content inset to be changed in a subclass of the UIViewController.

    @interface MyViewController ()
    
    @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
    @property (assign, nonatomic) UIEdgeInsets scrollViewInitialContentInset;
    
    @end
    
    
    @implementation MyViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [self setScrollViewInitialContentInset:UIEdgeInsetsZero];
    }
    
    - (void)viewWillLayoutSubviews {
        [super viewWillLayoutSubviews];
    
        if (UIEdgeInsetsEqualToEdgeInsets([self scrollViewInitialContentInset], UIEdgeInsetsZero)) {
            [self setScrollViewInitialContentInset:[self.scrollView contentInset]];
        }
    }
    
    - (void)viewDidLayoutSubviews {
        [super viewDidLayoutSubviews];
    
        UIEdgeInsets scrollViewInset = [self scrollViewInitialContentInset];
    
        if (UIEdgeInsetsEqualToEdgeInsets(scrollViewInset, UIEdgeInsetsZero) {
    
            if ([self respondsToSelector:@selector(topLayoutGuide)]) {
                scrollViewInset.top = [self.topLayoutGuide length];
            }
    
            if ([self respondsToSelector:@selector(bottomLayoutGuide)]) {
                scrollViewInset.bottom = [self.bottomLayoutGuide length];
            }
    
            [self.scrollView setContentInset:scrollViewInset];
        }
    }
    
    @end
    

    To explain the point:

    Any subclass of MyViewController can now modify the contentInset of scrollView in viewDidLoad and it will be respected. However, if the contentInset of scrollView is UIEdgeInsetsZero: it will be expanded to topLayoutGuide and bottomLayoutGuide.

    0 讨论(0)
提交回复
热议问题