-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathMGSplitViewController.m
More file actions
1120 lines (879 loc) · 31.5 KB
/
MGSplitViewController.m
File metadata and controls
1120 lines (879 loc) · 31.5 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// MGSplitViewController.m
// MGSplitView
//
// Created by Matt Gemmell on 26/07/2010.
// Copyright 2010 Instinctive Code.
//
#import "MGSplitViewController.h"
#import "MGSplitDividerView.h"
#import "MGSplitCornersView.h"
#define MG_DEFAULT_SPLIT_POSITION 320.0 // default width of master view in UISplitViewController.
#define MG_DEFAULT_SPLIT_WIDTH 1.0 // default width of split-gutter in UISplitViewController.
#define MG_DEFAULT_CORNER_RADIUS 5.0 // default corner-radius of overlapping split-inner corners on the master and detail views.
#define MG_DEFAULT_CORNER_COLOR [UIColor blackColor] // default color of intruding inner corners (and divider background).
#define MG_PANESPLITTER_CORNER_RADIUS 0.0 // corner-radius of split-inner corners for MGSplitViewDividerStylePaneSplitter style.
#define MG_PANESPLITTER_SPLIT_WIDTH 25.0 // width of split-gutter for MGSplitViewDividerStylePaneSplitter style.
#define MG_MIN_VIEW_WIDTH 200.0 // minimum width a view is allowed to become as a result of changing the splitPosition.
#define MG_ANIMATION_CHANGE_SPLIT_ORIENTATION @"ChangeSplitOrientation" // Animation ID for internal use.
#define MG_ANIMATION_CHANGE_SUBVIEWS_ORDER @"ChangeSubviewsOrder" // Animation ID for internal use.
@interface MGSplitViewController (MGPrivateMethods)
- (void)setup;
- (CGSize)splitViewSizeForOrientation:(UIInterfaceOrientation)theOrientation;
- (void)layoutSubviews;
- (void)layoutSubviewsWithAnimation:(BOOL)animate;
- (void)layoutSubviewsForInterfaceOrientation:(UIInterfaceOrientation)theOrientation withAnimation:(BOOL)animate;
- (BOOL)shouldShowMasterForInterfaceOrientation:(UIInterfaceOrientation)theOrientation;
- (BOOL)shouldShowMaster;
- (NSString *)nameOfInterfaceOrientation:(UIInterfaceOrientation)theOrientation;
- (void)reconfigureForMasterInPopover:(BOOL)inPopover;
@end
@implementation MGSplitViewController
#pragma mark -
#pragma mark Orientation helpers
- (NSString *)nameOfInterfaceOrientation:(UIInterfaceOrientation)theOrientation
{
NSString *orientationName = nil;
switch (theOrientation) {
case UIInterfaceOrientationPortrait:
orientationName = @"Portrait"; // Home button at bottom
break;
case UIInterfaceOrientationPortraitUpsideDown:
orientationName = @"Portrait (Upside Down)"; // Home button at top
break;
case UIInterfaceOrientationLandscapeLeft:
orientationName = @"Landscape (Left)"; // Home button on left
break;
case UIInterfaceOrientationLandscapeRight:
orientationName = @"Landscape (Right)"; // Home button on right
break;
default:
break;
}
return orientationName;
}
- (BOOL)isLandscape
{
return UIInterfaceOrientationIsLandscape(self.interfaceOrientation);
}
- (BOOL)shouldShowMasterForInterfaceOrientation:(UIInterfaceOrientation)theOrientation
{
// Returns YES if master view should be shown directly embedded in the splitview, instead of hidden in a popover.
return ((UIInterfaceOrientationIsLandscape(theOrientation)) ? _showsMasterInLandscape : _showsMasterInPortrait);
}
- (BOOL)shouldShowMaster
{
return [self shouldShowMasterForInterfaceOrientation:self.interfaceOrientation];
}
- (BOOL)isShowingMaster
{
return [self shouldShowMaster] && self.masterViewController && self.masterViewController.view && ([self.masterViewController.view superview] == self.view);
}
#pragma mark -
#pragma mark Setup and Teardown
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder])) {
[self setup];
}
return self;
}
- (void)setup
{
// Configure default behaviour.
_viewControllers = [[NSMutableArray alloc] initWithObjects:[NSNull null], [NSNull null], nil];
_splitWidth = MG_DEFAULT_SPLIT_WIDTH;
_showsMasterInPortrait = NO;
_showsMasterInLandscape = YES;
_reconfigurePopup = NO;
_vertical = YES;
_masterBeforeDetail = YES;
_splitPosition = MG_DEFAULT_SPLIT_POSITION;
CGRect divRect = self.view.bounds;
if ([self isVertical]) {
divRect.origin.y = _splitPosition;
divRect.size.height = _splitWidth;
} else {
divRect.origin.x = _splitPosition;
divRect.size.width = _splitWidth;
}
_dividerView = [[MGSplitDividerView alloc] initWithFrame:divRect];
_dividerView.splitViewController = self;
_dividerView.backgroundColor = MG_DEFAULT_CORNER_COLOR;
_dividerStyle = MGSplitViewDividerStyleThin;
}
- (void)dealloc
{
_delegate = nil;
[self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
#pragma mark -
#pragma mark View management
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self.masterViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self.detailViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self.masterViewController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
[self.detailViewController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
[self.masterViewController willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self.detailViewController willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
// Hide popover.
if (_hiddenPopoverController && _hiddenPopoverController.popoverVisible) {
[_hiddenPopoverController dismissPopoverAnimated:NO];
}
// Re-tile views.
_reconfigurePopup = YES;
[self layoutSubviewsForInterfaceOrientation:toInterfaceOrientation withAnimation:YES];
}
- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self.masterViewController willAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self.detailViewController willAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
- (void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
[self.masterViewController didAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation];
[self.detailViewController didAnimateFirstHalfOfRotationToInterfaceOrientation:toInterfaceOrientation];
}
- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration
{
[self.masterViewController willAnimateSecondHalfOfRotationFromInterfaceOrientation:fromInterfaceOrientation duration:duration];
[self.detailViewController willAnimateSecondHalfOfRotationFromInterfaceOrientation:fromInterfaceOrientation duration:duration];
}
- (CGSize)splitViewSizeForOrientation:(UIInterfaceOrientation)theOrientation
{
UIScreen *screen = [UIScreen mainScreen];
CGRect fullScreenRect = screen.bounds; // always implicitly in Portrait orientation.
CGRect appFrame = screen.applicationFrame;
// Find status bar height by checking which dimension of the applicationFrame is narrower than screen bounds.
// Little bit ugly looking, but it'll still work even if they change the status bar height in future.
float statusBarHeight = MAX((fullScreenRect.size.width - appFrame.size.width), (fullScreenRect.size.height - appFrame.size.height));
// Initially assume portrait orientation.
float width = fullScreenRect.size.width;
float height = fullScreenRect.size.height;
// Correct for orientation.
if (UIInterfaceOrientationIsLandscape(theOrientation)) {
width = height;
height = fullScreenRect.size.width;
}
// Account for status bar, which always subtracts from the height (since it's always at the top of the screen).
height -= statusBarHeight;
return CGSizeMake(width, height);
}
- (void)layoutSubviewsForInterfaceOrientation:(UIInterfaceOrientation)theOrientation withAnimation:(BOOL)animate
{
if (_reconfigurePopup) {
[self reconfigureForMasterInPopover:![self shouldShowMasterForInterfaceOrientation:theOrientation]];
}
// Layout the master, detail and divider views appropriately, adding/removing subviews as needed.
// First obtain relevant geometry.
CGSize fullSize = [self splitViewSizeForOrientation:theOrientation];
float width = fullSize.width;
float height = fullSize.height;
if (NO) { // Just for debugging.
NSLog(@"Target orientation is %@, dimensions will be %.0f x %.0f",
[self nameOfInterfaceOrientation:theOrientation], width, height);
}
// Layout the master, divider and detail views.
CGRect newFrame = CGRectMake(0, 0, width, height);
UIViewController *controller;
UIView *theView;
BOOL shouldShowMaster = [self shouldShowMasterForInterfaceOrientation:theOrientation];
BOOL masterFirst = [self isMasterBeforeDetail];
if ([self isVertical]) {
// Master on left, detail on right (or vice versa).
CGRect masterRect, dividerRect, detailRect;
if (masterFirst) {
if (!shouldShowMaster) {
// Move off-screen.
newFrame.origin.x -= (_splitPosition + _splitWidth);
}
newFrame.size.width = _splitPosition;
masterRect = newFrame;
newFrame.origin.x += newFrame.size.width;
newFrame.size.width = _splitWidth;
dividerRect = newFrame;
newFrame.origin.x += newFrame.size.width;
newFrame.size.width = width - newFrame.origin.x;
detailRect = newFrame;
} else {
if (!shouldShowMaster) {
// Move off-screen.
newFrame.size.width += (_splitPosition + _splitWidth);
}
newFrame.size.width -= (_splitPosition + _splitWidth);
detailRect = newFrame;
newFrame.origin.x += newFrame.size.width;
newFrame.size.width = _splitWidth;
dividerRect = newFrame;
newFrame.origin.x += newFrame.size.width;
newFrame.size.width = _splitPosition;
masterRect = newFrame;
}
// Position master.
controller = self.masterViewController;
if (controller && [controller isKindOfClass:[UIViewController class]]) {
theView = controller.view;
if (theView) {
theView.frame = masterRect;
if (!theView.superview) {
[controller viewWillAppear:NO];
[self.view addSubview:theView];
[controller viewDidAppear:NO];
}
}
}
// Position divider.
theView = _dividerView;
theView.frame = dividerRect;
if (!theView.superview) {
[self.view addSubview:theView];
}
// Position detail.
controller = self.detailViewController;
if (controller && [controller isKindOfClass:[UIViewController class]]) {
theView = controller.view;
if (theView) {
theView.frame = detailRect;
if (!theView.superview) {
[self.view insertSubview:theView aboveSubview:self.masterViewController.view];
} else {
[self.view bringSubviewToFront:theView];
}
}
}
} else {
// Master above, detail below (or vice versa).
CGRect masterRect, dividerRect, detailRect;
if (masterFirst) {
if (!shouldShowMaster) {
// Move off-screen.
newFrame.origin.y -= (_splitPosition + _splitWidth);
}
newFrame.size.height = _splitPosition;
masterRect = newFrame;
newFrame.origin.y += newFrame.size.height;
newFrame.size.height = _splitWidth;
dividerRect = newFrame;
newFrame.origin.y += newFrame.size.height;
newFrame.size.height = height - newFrame.origin.y;
detailRect = newFrame;
} else {
if (!shouldShowMaster) {
// Move off-screen.
newFrame.size.height += (_splitPosition + _splitWidth);
}
newFrame.size.height -= (_splitPosition + _splitWidth);
detailRect = newFrame;
newFrame.origin.y += newFrame.size.height;
newFrame.size.height = _splitWidth;
dividerRect = newFrame;
newFrame.origin.y += newFrame.size.height;
newFrame.size.height = _splitPosition;
masterRect = newFrame;
}
// Position master.
controller = self.masterViewController;
if (controller && [controller isKindOfClass:[UIViewController class]]) {
theView = controller.view;
if (theView) {
theView.frame = masterRect;
if (!theView.superview) {
[controller viewWillAppear:NO];
[self.view addSubview:theView];
[controller viewDidAppear:NO];
}
}
}
// Position divider.
theView = _dividerView;
theView.frame = dividerRect;
if (!theView.superview) {
[self.view addSubview:theView];
}
// Position detail.
controller = self.detailViewController;
if (controller && [controller isKindOfClass:[UIViewController class]]) {
theView = controller.view;
if (theView) {
theView.frame = detailRect;
if (!theView.superview) {
[self.view insertSubview:theView aboveSubview:self.masterViewController.view];
} else {
[self.view bringSubviewToFront:theView];
}
}
}
}
// Create corner views if necessary.
MGSplitCornersView *leadingCorners; // top/left of screen in vertical/horizontal split.
MGSplitCornersView *trailingCorners; // bottom/right of screen in vertical/horizontal split.
if (!_cornerViews) {
CGRect cornerRect = CGRectMake(0, 0, 10, 10); // arbitrary, will be resized below.
leadingCorners = [[MGSplitCornersView alloc] initWithFrame:cornerRect];
leadingCorners.splitViewController = self;
leadingCorners.cornerBackgroundColor = MG_DEFAULT_CORNER_COLOR;
leadingCorners.cornerRadius = MG_DEFAULT_CORNER_RADIUS;
trailingCorners = [[MGSplitCornersView alloc] initWithFrame:cornerRect];
trailingCorners.splitViewController = self;
trailingCorners.cornerBackgroundColor = MG_DEFAULT_CORNER_COLOR;
trailingCorners.cornerRadius = MG_DEFAULT_CORNER_RADIUS;
_cornerViews = [[NSArray alloc] initWithObjects:leadingCorners, trailingCorners, nil];
} else if ([_cornerViews count] == 2) {
leadingCorners = [_cornerViews objectAtIndex:0];
trailingCorners = [_cornerViews objectAtIndex:1];
}
// Configure and layout the corner-views.
leadingCorners.cornersPosition = (_vertical) ? MGCornersPositionLeadingVertical : MGCornersPositionLeadingHorizontal;
trailingCorners.cornersPosition = (_vertical) ? MGCornersPositionTrailingVertical : MGCornersPositionTrailingHorizontal;
leadingCorners.autoresizingMask = (_vertical) ? UIViewAutoresizingFlexibleBottomMargin : UIViewAutoresizingFlexibleRightMargin;
trailingCorners.autoresizingMask = (_vertical) ? UIViewAutoresizingFlexibleTopMargin : UIViewAutoresizingFlexibleLeftMargin;
float x, y, cornersWidth, cornersHeight;
CGRect leadingRect, trailingRect;
float radius = leadingCorners.cornerRadius;
if (_vertical) { // left/right split
cornersWidth = (radius * 2.0) + _splitWidth;
cornersHeight = radius;
x = ((shouldShowMaster) ? ((masterFirst) ? _splitPosition : width - (_splitPosition + _splitWidth)) : (0 - _splitWidth)) - radius;
y = 0;
leadingRect = CGRectMake(x, y, cornersWidth, cornersHeight); // top corners
trailingRect = CGRectMake(x, (height - cornersHeight), cornersWidth, cornersHeight); // bottom corners
} else { // top/bottom split
x = 0;
y = ((shouldShowMaster) ? ((masterFirst) ? _splitPosition : height - (_splitPosition + _splitWidth)) : (0 - _splitWidth)) - radius;
cornersWidth = radius;
cornersHeight = (radius * 2.0) + _splitWidth;
leadingRect = CGRectMake(x, y, cornersWidth, cornersHeight); // left corners
trailingRect = CGRectMake((width - cornersWidth), y, cornersWidth, cornersHeight); // right corners
}
leadingCorners.frame = leadingRect;
trailingCorners.frame = trailingRect;
// Ensure corners are visible and frontmost.
if (!leadingCorners.superview) {
[self.view insertSubview:leadingCorners aboveSubview:self.detailViewController.view];
[self.view insertSubview:trailingCorners aboveSubview:self.detailViewController.view];
} else {
[self.view bringSubviewToFront:leadingCorners];
[self.view bringSubviewToFront:trailingCorners];
}
}
- (void)layoutSubviewsWithAnimation:(BOOL)animate
{
[self layoutSubviewsForInterfaceOrientation:self.interfaceOrientation withAnimation:animate];
}
- (void)layoutSubviews
{
[self layoutSubviewsForInterfaceOrientation:self.interfaceOrientation withAnimation:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if ([self isShowingMaster]) {
[self.masterViewController viewWillAppear:animated];
}
[self.detailViewController viewWillAppear:animated];
_reconfigurePopup = YES;
[self layoutSubviews];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ([self isShowingMaster]) {
[self.masterViewController viewDidAppear:animated];
}
[self.detailViewController viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if ([self isShowingMaster]) {
[self.masterViewController viewWillDisappear:animated];
}
[self.detailViewController viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
if ([self isShowingMaster]) {
[self.masterViewController viewDidDisappear:animated];
}
[self.detailViewController viewDidDisappear:animated];
}
#pragma mark -
#pragma mark Popover handling
- (void)reconfigureForMasterInPopover:(BOOL)inPopover
{
_reconfigurePopup = NO;
if ((inPopover && _hiddenPopoverController) || (!inPopover && !_hiddenPopoverController) || !self.masterViewController) {
// Nothing to do.
return;
}
if (inPopover && !_hiddenPopoverController && !_barButtonItem) {
// Create and configure popover for our masterViewController.
_hiddenPopoverController = nil;
[self.masterViewController viewWillDisappear:NO];
_hiddenPopoverController = [[UIPopoverController alloc] initWithContentViewController:self.masterViewController];
[self.masterViewController viewDidDisappear:NO];
// Create and configure _barButtonItem.
_barButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Master", nil)
style:UIBarButtonItemStyleBordered
target:self
action:@selector(showMasterPopover:)];
// Inform delegate of this state of affairs.
if (_delegate && [_delegate respondsToSelector:@selector(splitViewController:willHideViewController:withBarButtonItem:forPopoverController:)]) {
[(NSObject <MGSplitViewControllerDelegate> *)_delegate splitViewController:self
willHideViewController:self.masterViewController
withBarButtonItem:_barButtonItem
forPopoverController:_hiddenPopoverController];
}
} else if (!inPopover && _hiddenPopoverController && _barButtonItem) {
// I know this looks strange, but it fixes a bizarre issue with UIPopoverController leaving masterViewController's views in disarray.
[_hiddenPopoverController presentPopoverFromRect:CGRectZero inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
// Remove master from popover and destroy popover, if it exists.
[_hiddenPopoverController dismissPopoverAnimated:NO];
_hiddenPopoverController = nil;
// Inform delegate that the _barButtonItem will become invalid.
if (_delegate && [_delegate respondsToSelector:@selector(splitViewController:willShowViewController:invalidatingBarButtonItem:)]) {
[(NSObject <MGSplitViewControllerDelegate> *)_delegate splitViewController:self
willShowViewController:self.masterViewController
invalidatingBarButtonItem:_barButtonItem];
}
// Destroy _barButtonItem.
_barButtonItem = nil;
// Move master view.
UIView *masterView = self.masterViewController.view;
if (masterView && masterView.superview != self.view) {
[masterView removeFromSuperview];
}
}
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
[self reconfigureForMasterInPopover:NO];
}
- (void)notePopoverDismissed
{
[self popoverControllerDidDismissPopover:_hiddenPopoverController];
}
#pragma mark -
#pragma mark Animations
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
if (([animationID isEqualToString:MG_ANIMATION_CHANGE_SPLIT_ORIENTATION] ||
[animationID isEqualToString:MG_ANIMATION_CHANGE_SUBVIEWS_ORDER])
&& _cornerViews) {
for (UIView *corner in _cornerViews) {
corner.hidden = NO;
}
_dividerView.hidden = NO;
}
}
#pragma mark -
#pragma mark IB Actions
- (IBAction)toggleSplitOrientation:(id)sender
{
BOOL showingMaster = [self isShowingMaster];
if (showingMaster) {
if (_cornerViews) {
for (UIView *corner in _cornerViews) {
corner.hidden = YES;
}
_dividerView.hidden = YES;
}
[UIView beginAnimations:MG_ANIMATION_CHANGE_SPLIT_ORIENTATION context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
}
self.vertical = (!self.vertical);
if (showingMaster) {
[UIView commitAnimations];
}
}
- (IBAction)toggleMasterBeforeDetail:(id)sender
{
BOOL showingMaster = [self isShowingMaster];
if (showingMaster) {
if (_cornerViews) {
for (UIView *corner in _cornerViews) {
corner.hidden = YES;
}
_dividerView.hidden = YES;
}
[UIView beginAnimations:MG_ANIMATION_CHANGE_SUBVIEWS_ORDER context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
}
self.masterBeforeDetail = (!self.masterBeforeDetail);
if (showingMaster) {
[UIView commitAnimations];
}
}
- (IBAction)toggleMasterView:(id)sender
{
if (_hiddenPopoverController && _hiddenPopoverController.popoverVisible) {
[_hiddenPopoverController dismissPopoverAnimated:NO];
}
if (![self isShowingMaster]) {
// We're about to show the master view. Ensure it's in place off-screen to be animated in.
_reconfigurePopup = YES;
[self reconfigureForMasterInPopover:NO];
[self layoutSubviews];
}
// This action functions on the current primary orientation; it is independent of the other primary orientation.
[UIView beginAnimations:@"toggleMaster" context:nil];
if (self.isLandscape) {
self.showsMasterInLandscape = !_showsMasterInLandscape;
} else {
self.showsMasterInPortrait = !_showsMasterInPortrait;
}
[UIView commitAnimations];
}
- (IBAction)showMasterPopover:(id)sender
{
if (_hiddenPopoverController && !(_hiddenPopoverController.popoverVisible)) {
// Inform delegate.
if (_delegate && [_delegate respondsToSelector:@selector(splitViewController:popoverController:willPresentViewController:)]) {
[(NSObject <MGSplitViewControllerDelegate> *)_delegate splitViewController:self
popoverController:_hiddenPopoverController
willPresentViewController:self.masterViewController];
}
// Show popover.
[_hiddenPopoverController presentPopoverFromBarButtonItem:_barButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
#pragma mark -
#pragma mark Accessors and properties
- (id)delegate
{
return _delegate;
}
- (void)setDelegate:(id <MGSplitViewControllerDelegate>)newDelegate
{
if (newDelegate != _delegate &&
(!newDelegate || [(NSObject *)newDelegate conformsToProtocol:@protocol(MGSplitViewControllerDelegate)])) {
_delegate = newDelegate;
}
}
- (BOOL)showsMasterInPortrait
{
return _showsMasterInPortrait;
}
- (void)setShowsMasterInPortrait:(BOOL)flag
{
if (flag != _showsMasterInPortrait) {
_showsMasterInPortrait = flag;
if (![self isLandscape]) { // i.e. if this will cause a visual change.
if (_hiddenPopoverController && _hiddenPopoverController.popoverVisible) {
[_hiddenPopoverController dismissPopoverAnimated:NO];
}
// Rearrange views.
_reconfigurePopup = YES;
[self layoutSubviews];
}
}
}
- (BOOL)showsMasterInLandscape
{
return _showsMasterInLandscape;
}
- (void)setShowsMasterInLandscape:(BOOL)flag
{
if (flag != _showsMasterInLandscape) {
_showsMasterInLandscape = flag;
if ([self isLandscape]) { // i.e. if this will cause a visual change.
if (_hiddenPopoverController && _hiddenPopoverController.popoverVisible) {
[_hiddenPopoverController dismissPopoverAnimated:NO];
}
// Rearrange views.
_reconfigurePopup = YES;
[self layoutSubviews];
}
}
}
- (BOOL)isVertical
{
return _vertical;
}
- (void)setVertical:(BOOL)flag
{
if (flag != _vertical) {
if (_hiddenPopoverController && _hiddenPopoverController.popoverVisible) {
[_hiddenPopoverController dismissPopoverAnimated:NO];
}
_vertical = flag;
// Inform delegate.
if (_delegate && [_delegate respondsToSelector:@selector(splitViewController:willChangeSplitOrientationToVertical:)]) {
[_delegate splitViewController:self willChangeSplitOrientationToVertical:_vertical];
}
[self layoutSubviews];
}
}
- (BOOL)isMasterBeforeDetail
{
return _masterBeforeDetail;
}
- (void)setMasterBeforeDetail:(BOOL)flag
{
if (flag != _masterBeforeDetail) {
if (_hiddenPopoverController && _hiddenPopoverController.popoverVisible) {
[_hiddenPopoverController dismissPopoverAnimated:NO];
}
_masterBeforeDetail = flag;
if ([self isShowingMaster]) {
[self layoutSubviews];
}
}
}
- (float)splitPosition
{
return _splitPosition;
}
- (void)setSplitPosition:(float)posn
{
// Check to see if delegate wishes to constrain the position.
float newPosn = posn;
BOOL constrained = NO;
CGSize fullSize = [self splitViewSizeForOrientation:self.interfaceOrientation];
if (_delegate && [_delegate respondsToSelector:@selector(splitViewController:constrainSplitPosition:splitViewSize:)]) {
newPosn = [_delegate splitViewController:self constrainSplitPosition:newPosn splitViewSize:fullSize];
constrained = YES; // implicitly trust delegate's response.
} else {
// Apply default constraints if delegate doesn't wish to participate.
float minPos = MG_MIN_VIEW_WIDTH;
float maxPos = ((_vertical) ? fullSize.width : fullSize.height) - (MG_MIN_VIEW_WIDTH + _splitWidth);
constrained = (newPosn != _splitPosition && newPosn >= minPos && newPosn <= maxPos);
}
if (constrained) {
if (_hiddenPopoverController && _hiddenPopoverController.popoverVisible) {
[_hiddenPopoverController dismissPopoverAnimated:NO];
}
_splitPosition = newPosn;
// Inform delegate.
if (_delegate && [_delegate respondsToSelector:@selector(splitViewController:willMoveSplitToPosition:)]) {
[_delegate splitViewController:self willMoveSplitToPosition:_splitPosition];
}
if ([self isShowingMaster]) {
[self layoutSubviews];
}
}
}
- (void)setSplitPosition:(float)posn animated:(BOOL)animate
{
BOOL shouldAnimate = (animate && [self isShowingMaster]);
if (shouldAnimate) {
[UIView beginAnimations:@"SplitPosition" context:nil];
}
[self setSplitPosition:posn];
if (shouldAnimate) {
[UIView commitAnimations];
}
}
- (float)splitWidth
{
return _splitWidth;
}
- (void)setSplitWidth:(float)width
{
if (width != _splitWidth && width >= 0) {
_splitWidth = width;
if ([self isShowingMaster]) {
[self layoutSubviews];
}
}
}
- (NSArray *)viewControllers
{
return [_viewControllers copy];
}
- (void)setViewControllers:(NSArray *)controllers
{
if (controllers != _viewControllers) {
for (UIViewController *controller in _viewControllers) {
if ([controller isKindOfClass:[UIViewController class]]) {
[controller.view removeFromSuperview];
}
}
_viewControllers = [[NSMutableArray alloc] initWithCapacity:2];
if (controllers && [controllers count] >= 2) {
self.masterViewController = [controllers objectAtIndex:0];
self.detailViewController = [controllers objectAtIndex:1];
} else {
NSLog(@"Error: %@ requires 2 view-controllers. (%@)", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
}
[self layoutSubviews];
}
}
- (UIViewController *)masterViewController
{
if (_viewControllers && [_viewControllers count] > 0) {
NSObject *controller = [_viewControllers objectAtIndex:0];
if ([controller isKindOfClass:[UIViewController class]]) {
return controller;
}
}
return nil;
}
- (void)setMasterViewController:(UIViewController *)master
{
if (!_viewControllers) {
_viewControllers = [[NSMutableArray alloc] initWithCapacity:2];
}
NSObject *newMaster = master;
if (!newMaster) {
newMaster = [NSNull null];
}
BOOL changed = YES;
if ([_viewControllers count] > 0) {
if ([_viewControllers objectAtIndex:0] == newMaster) {
changed = NO;
} else {
[_viewControllers replaceObjectAtIndex:0 withObject:newMaster];
}
} else {
[_viewControllers addObject:newMaster];
}
if (changed) {
[self layoutSubviews];
}
}
- (UIViewController *)detailViewController
{
if (_viewControllers && [_viewControllers count] > 1) {
NSObject *controller = [_viewControllers objectAtIndex:1];
if ([controller isKindOfClass:[UIViewController class]]) {
return controller;
}
}
return nil;
}
- (void)setDetailViewController:(UIViewController *)detail
{
if (!_viewControllers) {
_viewControllers = [[NSMutableArray alloc] initWithCapacity:2];
[_viewControllers addObject:[NSNull null]];
}
BOOL changed = YES;
if ([_viewControllers count] > 1) {
if ([_viewControllers objectAtIndex:1] == detail) {
changed = NO;
} else {
[_viewControllers replaceObjectAtIndex:1 withObject:detail];
}
} else {
[_viewControllers addObject:detail];
}
if (changed) {
[self layoutSubviews];
}
}