简体   繁体   中英

Remove UIBarButtonItem from master view controller when detail view controller is visible

I am converting my view controllers into a split view controller with a Universal Storyboard to show two views on screen at the same time, which will only appear on iPad - only one will be visible on iPhone. I have a situation where a UIBarButtonItem is only relevant when only one view controllers is on screen. I want this button removed or hidden when both view controllers in the split view controller are visible.

I cannot use Size Classes to do this, since the button I want removed is in the master view controller (actually I have button in both the master and detail that should be removed), which won't have a size class of Regular w Regular h. Plus Interface Builder doesn't provide an "Installed" option for UIBarButtonItem s, and I cannot check size classes in code because the app will also run on iOS 7.

How can I remove/hide a UIBarButtonItem when both the master and detail view controller are on screen? Or I could not add this button in IB, then add it in code when deemed appropriate, but how would I know if both view controllers will be on screen?

Here's a nice solution that works great for iOS 8 and iOS 7 on iPhone and iPad. You simply detect if there is a split view controller and if so, check if it's collapsed or not. If it's collapsed you know only one view controller is on screen. Knowing that info you can do anything you need to.

//remove right bar button item if more than one view controller is on screen
if (self.splitViewController) {
    if ([UISplitViewController instancesRespondToSelector:@selector(isCollapsed)]) {
        if (!self.splitViewController.collapsed) {
            self.navigationController.navigationBar.topItem.rightBarButtonItem = nil;
        }
    } else {
        self.navigationController.navigationBar.topItem.rightBarButtonItem = nil;
    }
}

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