-
Notifications
You must be signed in to change notification settings - Fork 0
/
VKSplitViewController.swift
225 lines (187 loc) · 9.06 KB
/
VKSplitViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//
// VKSplitViewControlelrViewController.swift
//
// Created by Viacheslav Karamov on 04.07.15.
// Copyright (c) 2015 Viacheslav Karamov. All rights reserved.
//
import UIKit
/**
* An easy-to-use UISplitViewController replacement with the simple interface
*/
@available(iOS, introduced=7.0)
public class VKSplitViewController: UIViewController
{
/** Assigning a new value to this property animating showing or hiding master view controller respectively. Default is true. See `showMasterViewController(show : Bool, completion : (() -> Void)?)`
*/
public var masterViewControllerVisible : Bool
{
set
{
showMasterViewController(newValue, animated:true, completion:nil);
}
get
{
return self.isMasterVisible;
}
}
/** Width of master view controller in points. The rest of the space will be occupied by the detail controller and the separator. Default is 320pt.
*/
public var masterViewControllerWidth : CGFloat = 320.0
{
didSet
{
self.masterViewWidthConstraint.constant = masterViewControllerWidth;
}
}
/** Separator color. Default is UIColor.lightGrayColor()
*/
public var separatorColor : UIColor = UIColor.lightGrayColor()
{
didSet
{
separator.backgroundColor = separatorColor;
}
}
/** Width of the separator. Default is 1pt.
*/
public var separatorWidth : CGFloat = 1.0
{
didSet
{
separatorWidthConstraint.constant = separatorWidth;
}
}
/** Controller added to the left of the widget. **Read-only**. To set its value use `setViewControllers(masterController : UIViewController, detailController : UIViewController)`
*/
public var masterViewController : UIViewController?
{
get
{
return self.masterVc;
}
}
/** Controller added to the right of the widget. **Read-only**. To set its value use `setViewControllers(masterController : UIViewController, detailController : UIViewController)`
*/
public var detailViewController : UIViewController?
{
get
{
return self.detailVc;
}
}
/** Sets both master and detail controllers. They will be imediatelly added as a child view controllers of the widget. Also sets `masterViewController` and `detailViewController` properties.
:param: masterController Controller added to the left of the widget.
:param: detailController Controller added to the right of the widget.
*/
public func setViewControllers(masterController : UIViewController, detailController : UIViewController)
{
self._setViewControllers(masterController, detailController);
}
/** Shows or hides master view controller respectively depending of the `show` parameter value.
:param: show If true, shows master controller using simple animation.
:param: animated If true, show/hide is performed using animation.
:param: completion: A block object to be executed when the animation sequence ends.
*/
public func showMasterViewController(show : Bool, animated: Bool, completion : (() -> Void)?)
{
assert(nil != self.masterVc && nil != self.detailVc, "You should call setViewControllers() before calling this method \(__FUNCTION__)");
if (self.isMasterVisible != show)
{
self.isMasterVisible = show;
self.masterViewLeadingConstraint.constant = show ? 0.0 : -self.masterViewControllerWidth - self.separatorWidth;
self.detailVc?.view.setNeedsLayout();
self.masterVc?.view.hidden = false;
UIView .animateWithDuration(self.animationDuration,
animations: { () -> Void in
self.view.layoutIfNeeded();
}, completion: { (Bool finished) -> Void in
self.masterVc?.view.hidden = !show;
if (nil != completion)
{
completion!();
}
});
}
}
/** Duration of master controller's hide/show animation. Default is 0.5
*/
public var animationDuration : NSTimeInterval = 0.5;
private var separator : UIView!;
private var masterViewLeadingConstraint : NSLayoutConstraint!;
private var masterViewWidthConstraint : NSLayoutConstraint!;
private var separatorWidthConstraint : NSLayoutConstraint!;
private var masterVc : UIViewController?;
private var detailVc : UIViewController?;
private var isMasterVisible = true;
override public func viewDidLoad() {
super.viewDidLoad()
separator = UIView(frame: CGRectZero);
separator.translatesAutoresizingMaskIntoConstraints = false;
separator.backgroundColor = separatorColor;
view.addSubview(separator);
}
}
private extension VKSplitViewController
{
func _setViewControllers(masterController : UIViewController, _ detailController : UIViewController)
{
masterController.view.translatesAutoresizingMaskIntoConstraints = false;
detailController.view.translatesAutoresizingMaskIntoConstraints = false;
if (nil != self.masterVc && nil != self.detailVc)
{
if (self.masterVc! != masterController)
{
replaceExistingViewController(existingController: self.masterVc!, newController :masterController);
}
if (self.detailVc! != detailController)
{
replaceExistingViewController(existingController: self.detailVc!, newController: detailController);
}
self.masterVc = masterController;
self.detailVc = detailController;
addConstraintsTo(masterController: masterController, detailController: detailController);
}
else
{
addChildViewController(masterController);
addChildViewController(detailController);
view.addSubview(masterController.view);
view.addSubview(detailController.view);
addConstraintsTo(masterController: masterController, detailController: detailController);
masterController.didMoveToParentViewController(self);
detailController.didMoveToParentViewController(self);
self.masterVc = masterController;
self.detailVc = detailController;
view.layoutIfNeeded();
}
}
func replaceExistingViewController(existingController existingController : UIViewController, newController : UIViewController)
{
existingController.willMoveToParentViewController(nil);
addChildViewController(newController);
view.addSubview(newController.view);
UIView .animateWithDuration(0.2, animations: { () -> Void in
existingController.view.alpha = 0.0;
}) { (finished) -> Void in
existingController.view.removeFromSuperview();
existingController.removeFromParentViewController();
newController.didMoveToParentViewController(self);
}
}
func addConstraintsTo(masterController masterController : UIViewController, detailController : UIViewController)
{
self.masterViewLeadingConstraint = NSLayoutConstraint(item: masterController.view, attribute: .Leading, relatedBy: .Equal, toItem: self.view, attribute: .Leading, multiplier: 1.0, constant: 0);
self.masterViewWidthConstraint = NSLayoutConstraint(item: masterController.view, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: self.masterViewControllerWidth);
self.separatorWidthConstraint = NSLayoutConstraint(item: self.separator!, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: self.separatorWidth);
self.view.addConstraint(self.masterViewLeadingConstraint);
self.view.addConstraint(self.masterViewWidthConstraint);
self.view.addConstraint(self.separatorWidthConstraint);
let views = ["master" : masterController.view,
"separator": self.separator!,
"detail": detailController.view];
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[master]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views));
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[separator]|", options:NSLayoutFormatOptions(rawValue: 0), metrics:nil, views:views));
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[detail]|", options:NSLayoutFormatOptions(rawValue: 0), metrics:nil, views:views));
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[master]-0-[separator]-0-[detail]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views));
}
}