Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISplitViewController displayModeButtonItem()

Tags:

swift

ios8

Does someone know a way to change the text (and appearance?) of the button returned from the UISplitViewController delegate:

func displayModeButtonItem() -> UIBarButtonItem?
like image 393
valfer Avatar asked Sep 15 '14 16:09

valfer


People also ask

What is the uisplitviewcontroller?

Apple has built a rather handy view controller just for us called UISplitViewController and it harks right back to the iPad’s lowly beginnings. In this UISplitViewController tutorial, you’ll learn all about how to tame it! Also, since iOS 8, the split view controller works on both iPad and iPhone.

What is a split view controller for iOS?

You’ll use a split view controller to handle the navigation and display. It’ll adapt to work on both iPhone and iPad. Note: This tutorial focuses on split view controllers. You should already be familiar with the basics of creating an iOS app first, such as Auto Layout and storyboards .

How do I get the Master View Controller in Split View?

A split view controller has an array property viewControllers which contains the master and detail view controllers. The master view controller, in your case, is actually a navigation controller. So to get the actual MasterViewController instance, you take the navigation controller’s first view controller.

How do I find the initial view controller in iOS 14?

Check the Is Initial View Controller option. You’ll see an arrow to the left of the split view controller. This tells you it’s the initial view controller of this storyboard. Build and run the app on an iPad simulator. Rotate your simulator to landscape.


2 Answers

I resolved it for myself in such way:

UIBarButtonItem(image: UIImage(named:"home"),
            landscapeImagePhone: UIImage(named:"home"),
            style: UIBarButtonItemStyle.Plain,
            target: splitViewController.displayModeButtonItem().target,
            action: splitViewController.displayModeButtonItem().action)

I use this in AppDelegate instead of splitViewController.displayModeButtonItem() and it works fine for me.

like image 125
voluntas88 Avatar answered Sep 20 '22 14:09

voluntas88


I made a combination of josh's and voluntas88's solutions on my solution.

First you need to use the UISplitViewControllerDelegate method func targetDisplayModeForActionInSplitViewController(_ svc: UISplitViewController) -> UISplitViewControllerDisplayMode and then add a customized UIBarButtonItem. Here is my solution:

func targetDisplayModeForActionInSplitViewController(svc: UISplitViewController) -> UISplitViewControllerDisplayMode {
        if svc.displayMode == UISplitViewControllerDisplayMode.PrimaryHidden || svc.displayMode == UISplitViewControllerDisplayMode.PrimaryOverlay {
            self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named:"menu.png"),
                landscapeImagePhone: UIImage(named:"menu.png"),
                style: UIBarButtonItemStyle.Plain,
                target: self.splitViewController!.displayModeButtonItem().target,
                action: self.splitViewController!.displayModeButtonItem().action)
        }else {
// disable button on landscape
            if UIApplication.sharedApplication().statusBarOrientation == UIInterfaceOrientation.LandscapeLeft || UIApplication.sharedApplication().statusBarOrientation == UIInterfaceOrientation.LandscapeRight {
                self.navigationItem.leftBarButtonItem?.enabled = false
            }
        }
        return UISplitViewControllerDisplayMode.Automatic
    }
like image 40
Rodrigo Gonzalez Avatar answered Sep 18 '22 14:09

Rodrigo Gonzalez