Skip to content

Commit

Permalink
Updated for Swift 2 & Added Objective-C
Browse files Browse the repository at this point in the history
  • Loading branch information
mickmaccallum committed Sep 11, 2015
1 parent 105a4c6 commit bc0a211
Showing 1 changed file with 60 additions and 6 deletions.
66 changes: 60 additions & 6 deletions 2014-11-03-uisplitviewcontroller.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ author: Natasha Murashev
category: Cocoa
excerpt: "The introduction of iPhone 6+ brought on a new importance for UISplitViewController. With just a few little tweaks, an app can now become Universal, with Apple handling most of the UI logic for all the different screen sizes."
status:
swift: 1.0
swift: 2.0
reviewed: September 11, 2015
---

The introduction of iPhone 6+ brought on a new importance for `UISplitViewController`. With just a few little tweaks, an app can now become Universal, with Apple handling most of the UI logic for all the different screen sizes.
Expand Down Expand Up @@ -71,7 +72,7 @@ Even when the navigation controller is in place, the UI is not that much better

The simplest way to fix this issue would be to somehow indicate that there is more to the app than what's currently on-screen. Luckily, the UISplitViewController has a **displayModeButtonItem**, which can be added to the navigation bar:

```swift
~~~{swift}
override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -80,7 +81,18 @@ override func viewDidLoad() {
navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem()
navigationItem.leftItemsSupplementBackButton = true
}
```
~~~

~~~{objective-c}
- (void)viewDidLoad {
[super viewDidLoad];
// ...
self.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
self.navigationItem.leftItemsSupplementBackButton = YES;
}
~~~

Build and Run on the iPad again, and now the user gets a nice indication of how to get at the rest of the app:

Expand All @@ -98,7 +110,7 @@ There is one more optimization we can do for the iPhone 6+ via [`UISplitViewCont

When the user first launches the app, we can make the master view controller fully displayed until the user selects a list item:

```swift
~~~{swift}
import UIKit
class SelectColorTableViewController: UITableViewController, UISplitViewControllerDelegate {
Expand All @@ -122,11 +134,53 @@ class SelectColorTableViewController: UITableViewController, UISplitViewControll
// MARK: - UISplitViewControllerDelegate
func splitViewController(splitViewController: UISplitViewController, collapseSecondaryViewController secondaryViewController: UIViewController!, ontoPrimaryViewController primaryViewController: UIViewController!) -> Bool {
func splitViewController(splitViewController: UISplitViewController, collapseSecondaryViewController secondaryViewController: UIViewController, ontoPrimaryViewController primaryViewController: UIViewController) -> Bool {
return collapseDetailViewController
}
}
```
~~~

~~~{objective-c}
@import UIKit;
@interface SelectColorTableViewController : UITableViewController <UISplitViewControllerDelegate>
@end
@interface SelectColorTableViewController ()
@property (nonatomic) BOOL shouldCollapseDetailViewController;
@end
@implementation SelectColorTableViewController
#pragma mark - UITableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// ...
self.shouldCollapseDetailViewController = true;
self.splitViewController.delegate = self;
}
// ...
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.shouldCollapseDetailViewController = false;
}
#pragma mark - UISplitViewControllerDelegate
- (BOOL)splitViewController:(UISplitViewController *)splitViewController collapseSecondaryViewController:(UIViewController *)secondaryViewController ontoPrimaryViewController:(UIViewController *)primaryViewController {
return self.shouldCollapseDetailViewController;
}
@end
~~~

When the user first opens up the app on iPhone 6+ in portrait orientation, `SelectColorViewController` gets displayed as the primary view controller. Once the user selects a color or the app goes into the background, the `SelectColorViewController` gets collapsed again, and the `ColorViewController` is displayed:

Expand Down

0 comments on commit bc0a211

Please sign in to comment.