Does someone know a way to change the text (and appearance?) of the button returned from the UISplitViewController delegate:
func displayModeButtonItem() -> UIBarButtonItem?
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.
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 .
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.
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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With