UISplitViewController willHideViewController/willShowViewController not called after forcing detail view to be hidden

一笑奈何 提交于 2019-12-06 10:57:25

I was having a very similar problem, using Apple's sample code with the SubstitutableDetailViewController delegate combined with rotation.

What I did was store a BOOL for when the orientation was landscape, updating it on willRotateToInterfaceOrientation with this code:

isLandscape = UIInterfaceOrientationIsLandscape(self.interfaceOrientation);

On viewWillAppear I checked whether the current orientation is landscape, and if it did not match the stored value, called this method:

-(void)adjustLayoutForOrientation{
    if (isLandscape) {
        [self invalidateRootPopoverButtonItem:  self.navigationController.navigationItem.leftBarButtonItem];
}else{
        LeftViewController *lvc = (LeftViewController *)[self.splitViewController delegate];
        [self showRootPopoverButtonItem:  lvc.rootPopoverButtonItem ];
    }
}

Obviously I am using the delegation code provided by Apple, but for completeness those methods are these and can be implemented easily:

#pragma mark -
#pragma mark Managing the popover

- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    // Add the popover button to the left navigation item.
    [self.navigationController.navigationBar.topItem setLeftBarButtonItem:barButtonItem animated:NO];
}


- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    // Remove the popover button.
    [self.navigationController.navigationBar.topItem setLeftBarButtonItem:nil animated:NO];
}

The master view controller (left side) is maintaining the reference to the rootPopoverButtonItem, which makes this work.

EDIT: Note that I am also calling my adjustLayoutForOrientation method on viewDidLoad and willRotateToInterfaceOrientation... A better way might be to register with the Notification Center but I wasn't sure if background/invisible view controllers would get these notifications...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!