Skip to content

Commit

Permalink
Renamed delegate to splitDelegate to avoid confusion and possible con…
Browse files Browse the repository at this point in the history
…flicts.

Changed minimum view width to 100 (I had thumbnails in the master view that size).
Added MG_PANESPLITTER_ANIM_DURATION to control the speed of using [UIView animateWithDuration:].
Moved call to [self setup] to viewDidLoad, which makes MGSplitViewController Storyboard-friendly.
setSplitPosition now uses [UIView animateWithDuration:] instead of old [UIView beginAnimations .. commitAnimations].
  • Loading branch information
rae committed Jul 26, 2012
1 parent 9215d9f commit 50da3fe
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
4 changes: 2 additions & 2 deletions Classes/MGSplitViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef enum _MGSplitViewDividerStyle {
BOOL _showsMasterInPortrait;
BOOL _showsMasterInLandscape;
float _splitWidth;
id _delegate;
id _splitDelegate;
BOOL _vertical;
BOOL _masterBeforeDetail;
NSMutableArray *_viewControllers;
Expand All @@ -33,7 +33,7 @@ typedef enum _MGSplitViewDividerStyle {
MGSplitViewDividerStyle _dividerStyle; // Meta-setting which configures several aspects of appearance and behaviour.
}

@property (nonatomic, unsafe_unretained) IBOutlet id <MGSplitViewControllerDelegate> delegate;
@property (nonatomic, unsafe_unretained) IBOutlet id <MGSplitViewControllerDelegate> splitDelegate;
@property (nonatomic, assign) BOOL showsMasterInPortrait; // applies to both portrait orientations (default NO)
@property (nonatomic, assign) BOOL showsMasterInLandscape; // applies to both landscape orientations (default YES)
@property (nonatomic, assign, getter=isVertical) BOOL vertical; // if NO, split is horizontal, i.e. master above detail (default YES)
Expand Down
66 changes: 36 additions & 30 deletions Classes/MGSplitViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
#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_PANESPLITTER_ANIM_DURATION 0.25 // how long it take to animate changing the split position

#define MG_MIN_VIEW_WIDTH 100.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.
Expand Down Expand Up @@ -102,7 +104,7 @@ - (BOOL)isShowingMaster
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
[self setup];
// setup called in viewDidLoad, which is more Storyboard-friendly and avoid duplication
}

return self;
Expand All @@ -112,12 +114,16 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder])) {
[self setup];
// setup called in viewDidLoad
}

return self;
}

-(void)viewDidLoad
{
[self setup];
}

- (void)setup
{
Expand Down Expand Up @@ -147,7 +153,7 @@ - (void)setup

- (void)dealloc
{
_delegate = nil;
_splitDelegate = nil;
[self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

}
Expand Down Expand Up @@ -558,8 +564,8 @@ - (void)reconfigureForMasterInPopover:(BOOL)inPopover
action:@selector(showMasterPopover:)];

// Inform delegate of this state of affairs.
if (_delegate && [_delegate respondsToSelector:@selector(splitViewController:willHideViewController:withBarButtonItem:forPopoverController:)]) {
[(NSObject <MGSplitViewControllerDelegate> *)_delegate splitViewController:self
if (_splitDelegate && [_splitDelegate respondsToSelector:@selector(splitViewController:willHideViewController:withBarButtonItem:forPopoverController:)]) {
[(NSObject <MGSplitViewControllerDelegate> *)_splitDelegate splitViewController:self
willHideViewController:self.masterViewController
withBarButtonItem:_barButtonItem
forPopoverController:_hiddenPopoverController];
Expand All @@ -574,8 +580,8 @@ - (void)reconfigureForMasterInPopover:(BOOL)inPopover
_hiddenPopoverController = nil;

// Inform delegate that the _barButtonItem will become invalid.
if (_delegate && [_delegate respondsToSelector:@selector(splitViewController:willShowViewController:invalidatingBarButtonItem:)]) {
[(NSObject <MGSplitViewControllerDelegate> *)_delegate splitViewController:self
if (_splitDelegate && [_splitDelegate respondsToSelector:@selector(splitViewController:willShowViewController:invalidatingBarButtonItem:)]) {
[(NSObject <MGSplitViewControllerDelegate> *)_splitDelegate splitViewController:self
willShowViewController:self.masterViewController
invalidatingBarButtonItem:_barButtonItem];
}
Expand Down Expand Up @@ -695,8 +701,8 @@ - (IBAction)showMasterPopover:(id)sender
{
if (_hiddenPopoverController && !(_hiddenPopoverController.popoverVisible)) {
// Inform delegate.
if (_delegate && [_delegate respondsToSelector:@selector(splitViewController:popoverController:willPresentViewController:)]) {
[(NSObject <MGSplitViewControllerDelegate> *)_delegate splitViewController:self
if (_splitDelegate && [_splitDelegate respondsToSelector:@selector(splitViewController:popoverController:willPresentViewController:)]) {
[(NSObject <MGSplitViewControllerDelegate> *)_splitDelegate splitViewController:self
popoverController:_hiddenPopoverController
willPresentViewController:self.masterViewController];
}
Expand All @@ -711,17 +717,17 @@ - (IBAction)showMasterPopover:(id)sender
#pragma mark Accessors and properties


- (id)delegate
- (id<MGSplitViewControllerDelegate>)delegate
{
return _delegate;
return _splitDelegate;
}


- (void)setDelegate:(id <MGSplitViewControllerDelegate>)newDelegate
- (void)setSplitDelegate:(id <MGSplitViewControllerDelegate>)newDelegate
{
if (newDelegate != _delegate &&
if (newDelegate != _splitDelegate &&
(!newDelegate || [(NSObject *)newDelegate conformsToProtocol:@protocol(MGSplitViewControllerDelegate)])) {
_delegate = newDelegate;
_splitDelegate = newDelegate;
}
}

Expand Down Expand Up @@ -790,8 +796,8 @@ - (void)setVertical:(BOOL)flag
_vertical = flag;

// Inform delegate.
if (_delegate && [_delegate respondsToSelector:@selector(splitViewController:willChangeSplitOrientationToVertical:)]) {
[_delegate splitViewController:self willChangeSplitOrientationToVertical:_vertical];
if (_splitDelegate && [_splitDelegate respondsToSelector:@selector(splitViewController:willChangeSplitOrientationToVertical:)]) {
[_splitDelegate splitViewController:self willChangeSplitOrientationToVertical:_vertical];
}

[self layoutSubviews];
Expand Down Expand Up @@ -833,8 +839,8 @@ - (void)setSplitPosition:(float)posn
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];
if (_splitDelegate && [_splitDelegate respondsToSelector:@selector(splitViewController:constrainSplitPosition:splitViewSize:)]) {
newPosn = [_splitDelegate splitViewController:self constrainSplitPosition:newPosn splitViewSize:fullSize];
constrained = YES; // implicitly trust delegate's response.

} else {
Expand All @@ -852,8 +858,8 @@ - (void)setSplitPosition:(float)posn
_splitPosition = newPosn;

// Inform delegate.
if (_delegate && [_delegate respondsToSelector:@selector(splitViewController:willMoveSplitToPosition:)]) {
[_delegate splitViewController:self willMoveSplitToPosition:_splitPosition];
if (_splitDelegate && [_splitDelegate respondsToSelector:@selector(splitViewController:willMoveSplitToPosition:)]) {
[_splitDelegate splitViewController:self willMoveSplitToPosition:_splitPosition];
}

if ([self isShowingMaster]) {
Expand All @@ -867,11 +873,11 @@ - (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];
[UIView animateWithDuration:MG_PANESPLITTER_ANIM_DURATION animations:^{
[self setSplitPosition:posn];
}];
} else {
[self setSplitPosition:posn];
}
}

Expand Down Expand Up @@ -925,7 +931,7 @@ - (UIViewController *)masterViewController
if (_viewControllers && [_viewControllers count] > 0) {
NSObject *controller = [_viewControllers objectAtIndex:0];
if ([controller isKindOfClass:[UIViewController class]]) {
return controller;
return (UIViewController *)controller;
}
}

Expand Down Expand Up @@ -967,7 +973,7 @@ - (UIViewController *)detailViewController
if (_viewControllers && [_viewControllers count] > 1) {
NSObject *controller = [_viewControllers objectAtIndex:1];
if ([controller isKindOfClass:[UIViewController class]]) {
return controller;
return (UIViewController *)controller;
}
}

Expand Down Expand Up @@ -1055,7 +1061,7 @@ - (void)setDividerStyle:(MGSplitViewDividerStyle)newStyle
_dividerStyle = newStyle;

// Reconfigure general appearance and behaviour.
float cornerRadius;
float cornerRadius = MG_DEFAULT_CORNER_RADIUS;
if (_dividerStyle == MGSplitViewDividerStyleThin) {
cornerRadius = MG_DEFAULT_CORNER_RADIUS;
_splitWidth = MG_DEFAULT_SPLIT_WIDTH;
Expand Down Expand Up @@ -1106,7 +1112,7 @@ - (NSArray *)cornerViews
@synthesize showsMasterInPortrait;
@synthesize showsMasterInLandscape;
@synthesize vertical;
@synthesize delegate;
@synthesize splitDelegate;
@synthesize viewControllers;
@synthesize masterViewController;
@synthesize detailViewController;
Expand Down

0 comments on commit 50da3fe

Please sign in to comment.