ios7 no displayModeButtonItem or targetDisplayModeForActionInSplitViewController

放肆的年华 提交于 2019-12-13 14:17:37

问题


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

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