问题
I started a Split View project in xcode 6 and its working great. Out of the box I got a split view that has a navigation button (upper left) when in portrait mode such that the master view can be popped in/out.
Main issue is that it does not work in iOS7 as displayModeButtonItem and targetDisplayModeForActionInSplitViewController are iOS8 only.
I have seen a few apps that achieve that same effect and work in iOS7, yet I have no idea how to do this. Does anyone have a good example or workaround to achieve this in iOS7.
Bummer that out of the box xcode builds a project that will only work in iOS8, but I guess doesn't completely surprise me with apple.
回答1:
You can still use deprecated callback function in UISplitViewControllerDelegate
to add and remove UIBarButtonItem
to your detail view for iOS 7 platform. Implement as below in your UISplitViewControllerDelegate
:
func splitViewController(svc: UISplitViewController, willHideViewController aViewController: UIViewController, withBarButtonItem barButtonItem: UIBarButtonItem, forPopoverController pc: UIPopoverController) {
if (!self.respondsToSelector(Selector("displayModeButtonItem"))) {
let navigationController = self.viewControllers.last as UINavigationController
let detailViewController: UIViewController? = navigationController.viewControllers?.last as? UIViewController
barButtonItem.image = UIImage(named: "IC_BackChevron")
detailViewController?.navigationItem.leftBarButtonItem = barButtonItem
} else {
// This callback function is depreciated in IOS8. We use displayModeButtonItem.
}
}
func splitViewController(svc: UISplitViewController!, willShowViewController aViewController: UIViewController!, invalidatingBarButtonItem barButtonItem: UIBarButtonItem!) {
if (!self.respondsToSelector(Selector("displayModeButtonItem"))) {
let navigationController = self.viewControllers.last as UINavigationController
let detailViewController: UIViewController? = navigationController.viewControllers?.last as? UIViewController
detailViewController?.navigationItem.leftBarButtonItem = nil
} else {
// This callback function is depreciated in IOS8. We use displayModeButtonItem.
}
}
来源:https://stackoverflow.com/questions/26110594/ios7-no-displaymodebuttonitem-or-targetdisplaymodeforactioninsplitviewcontroller