Change the width of Master in UISplitViewController
If you subclass UISplitViewController, you can implement -viewDidLayoutSubviews
and adjust the width there. This is clean, no hacks or private APIs, and works even with rotation.
- (void)viewDidLayoutSubviews
{
const CGFloat kMasterViewWidth = 240.0;
UIViewController *masterViewController = [self.viewControllers objectAtIndex:0];
UIViewController *detailViewController = [self.viewControllers objectAtIndex:1];
if (detailViewController.view.frame.origin.x > 0.0) {
// Adjust the width of the master view
CGRect masterViewFrame = masterViewController.view.frame;
CGFloat deltaX = masterViewFrame.size.width - kMasterViewWidth;
masterViewFrame.size.width -= deltaX;
masterViewController.view.frame = masterViewFrame;
// Adjust the width of the detail view
CGRect detailViewFrame = detailViewController.view.frame;
detailViewFrame.origin.x -= deltaX;
detailViewFrame.size.width += deltaX;
detailViewController.view.frame = detailViewFrame;
[masterViewController.view setNeedsLayout];
[detailViewController.view setNeedsLayout];
}
}
How to change width sizes of split view controller items (Master View Controller - Detail View Controller)?
The class UISplitViewController
exposes a property to change the width of the master view. This property is preferredPrimaryColumnWidthFraction
with which you can specify the width of the master view as a percentage respect the total width of the split view.
Resize master and detail view controllers in a split view controller?
Edit:
In iOS 8+, the relative widths can be changed by specifying the minimum/maximumPrimaryColumnWidth properties or the preferredPrimaryColumnFraction.
The below answer is still true for iOS < 8:
You can't change the sizes for a split view controller.
See here: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html
"The UISplitViewController class is a container view controller that manages two panes of information. The first pane has a fixed width of 320 points and a height that matches the visible window height. The second pane fills the remaining space."
Refresh UISplitViewController after changing the Master's width
This works for me:
[splitViewController.view setNeedsLayout];
I use it to show/hide the MasterViewContoller in landscape mode.
iOS SplitViewController: Show master view when loading in compact width
You should use method
func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController: UIViewController, onto primaryViewController: UIViewController) -> Bool
which provided by UISplitViewControllerDelegate
Open UISplitViewController to Master View rather than Detail
Swift
UISplitViewController display master view above detail in portrait orientation is not about showing the Master view, it is about presenting the Detail view in full width, underneath the Master view.
UISplitViewController in portrait on iPhone shows detail VC instead of master is about the principle of the collapse mechanism.
This present answer addresses:
- Master → Detail (Compact width)
- iPhone 4s, 5, 5s, SE, 6, 6s, 7 (any orientation)
- iPod Touch
- any iPhone Plus (portrait)
- side-by-side (all other sizes)
- iPad
- any iPhone Plus (landscape)
You must set preferredDisplayMode
. You would want is .primaryVisible
if it existed! Using .allVisible
, iOS picks Detail
if only 1 view fits (Compact width); in that size, the code below will pick Master
.
The trick is to change both the preferredDisplayMode
to .allVisible
and to return true
in collapseSecondary:onto
.
class PrimarySplitViewController: UISplitViewController,
UISplitViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.preferredDisplayMode = .allVisible
}
func splitViewController(
_ splitViewController: UISplitViewController,
collapseSecondary secondaryViewController: UIViewController,
onto primaryViewController: UIViewController) -> Bool {
// Return true to prevent UIKit from applying its default behavior
return true
}
}
Related Topics
Get the Right Color in iOS7 Translucent Navigation Bar
In Swift How to Call Method with Parameters on Gcd Main Thread
Autolayout Is Ignored in Custom Uitableviewcell
Using Audiobufferlist with Swift
Swift - Which Types to Use? Nsstring or String
How to Create Custom Mkannotationview and Custom Annotation Title and Subtitle
Generate Random Numbers with a Given Distribution
How to Shrink a Uipickerview on the Iphone
Getting a Device Udid from .Mobileconfig
Ios9 - This Application Is Modifying the Autolayout Engine from a Background Thread -- Where
Saving Email/Password to Keychain in iOS
Document Directory Path of Xcode Device Simulator
Saving Image to Documents Directory and Retrieving for Email Attachment
Swift Doesn't Convert Objective-C Nserror** to Throws
Swift - Check If a Timestamp Is Yesterday, Today, Tomorrow, or X Days Ago
How to Access File Included in App Bundle in Swift