iOS 7: Custom container view controller and content inset

前端 未结 6 620
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-01 05:15

I have a table view controller wrapped in a navigation controller. The navigation controller seems to automatically apply the correct content inset to the table view controller

6条回答
  •  无人及你
    2021-02-01 05:37

    UINavigationController calls undocumented method _updateScrollViewFromViewController:toViewController in transition between controllers to update content insets. Here is my solution:

    UINavigationController+ContentInset.h

    #import 
    @interface UINavigationController (ContentInset)
    - (void) updateScrollViewFromViewController:(UIViewController*) from toViewController:(UIViewController*) to;
    @end
    

    UINavigationController+ContentInset.m

    #import "UINavigationController+ContentInset.h"
    @interface UINavigationController()
    - (void) _updateScrollViewFromViewController:(UIViewController*) from toViewController:(UIViewController*) to;
    @end
    
    @implementation UINavigationController (ContentInset)
    - (void) updateScrollViewFromViewController:(UIViewController*) from toViewController:(UIViewController*) to {
        if ([UINavigationController instancesRespondToSelector:@selector(_updateScrollViewFromViewController:toViewController:)])
            [self _updateScrollViewFromViewController:from toViewController:to];
    }
    @end
    

    Call updateScrollViewFromViewController:toViewController somewhere in your custom container view controller

    [self addChildViewController:newContentViewController];
    [self transitionFromViewController:previewsContentViewController
                      toViewController:newContentViewController
                              duration:0.5f
                               options:0
                            animations:^{
                                [self.navigationController updateScrollViewFromViewController:self toViewController:newContentViewController];
    
                        }
                        completion:^(BOOL finished) {
                            [newContentViewController didMoveToParentViewController:self];
                        }];
    

提交回复
热议问题