From d06f5066411f5dcf456c71273b626880e09666c4 Mon Sep 17 00:00:00 2001 From: Maxime Epain Date: Fri, 20 Mar 2015 17:57:48 +0100 Subject: [PATCH] Fix container height while using minimum height with parallax header. Fix example. --- .../Base.lproj/Main_iPhone.storyboard | 12 ++++---- .../MXParallaxViewController.m | 10 ++++++- Pod/Classes/MXSegmentedPager+ParallaxHeader.m | 25 +++++++++++++++- Pod/Classes/MXSegmentedPager.h | 2 +- Pod/Classes/MXSegmentedPager.m | 29 ++++++++++++------- 5 files changed, 59 insertions(+), 19 deletions(-) diff --git a/Example/MXSegmentedPager/Base.lproj/Main_iPhone.storyboard b/Example/MXSegmentedPager/Base.lproj/Main_iPhone.storyboard index 363fcbc0f..cacddad3b 100644 --- a/Example/MXSegmentedPager/Base.lproj/Main_iPhone.storyboard +++ b/Example/MXSegmentedPager/Base.lproj/Main_iPhone.storyboard @@ -1,8 +1,8 @@ - + - + @@ -10,7 +10,7 @@ - + @@ -27,10 +27,12 @@ - - + + + + diff --git a/Example/MXSegmentedPager/MXParallaxViewController.m b/Example/MXSegmentedPager/MXParallaxViewController.m index 0d603c35d..46e372949 100644 --- a/Example/MXSegmentedPager/MXParallaxViewController.m +++ b/Example/MXSegmentedPager/MXParallaxViewController.m @@ -23,6 +23,11 @@ - (void)viewDidLoad { [super viewDidLoad]; + [self.navigationController.navigationBar setBackgroundImage:[UIImage new] + forBarMetrics:UIBarMetricsDefault]; + self.navigationController.navigationBar.shadowImage = [UIImage new]; + self.automaticallyAdjustsScrollViewInsets = NO; + // Setup the segmented pager properties self.segmentedPager.delegate = self; self.segmentedPager.dataSource = self; @@ -52,7 +57,10 @@ - (MXSegmentedPager *)segmentedPager { if (!_segmentedPager) { // Set a segmented pager below the cover - _segmentedPager = [[MXSegmentedPager alloc] initWithFrame:self.view.frame]; + _segmentedPager = [[MXSegmentedPager alloc] initWithFrame:(CGRect){ + .origin = CGPointZero, + .size = self.view.frame.size + }]; } return _segmentedPager; } diff --git a/Pod/Classes/MXSegmentedPager+ParallaxHeader.m b/Pod/Classes/MXSegmentedPager+ParallaxHeader.m index f8d0c3493..0bd213b75 100644 --- a/Pod/Classes/MXSegmentedPager+ParallaxHeader.m +++ b/Pod/Classes/MXSegmentedPager+ParallaxHeader.m @@ -86,7 +86,6 @@ - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ return NO; } } - return YES; } @@ -119,6 +118,8 @@ @interface MXSegmentedPager () @implementation MXSegmentedPager (ParallaxHeader) +static NSString* const kContaineFrameKeyPath = @"container.frame"; + - (void)setParallaxHeaderView:(UIView *)view mode:(VGParallaxHeaderMode)mode height:(CGFloat)height { self.scrollView = [[MXScrollView alloc] initWithFrame:(CGRect){ @@ -130,6 +131,9 @@ - (void)setParallaxHeaderView:(UIView *)view mode:(VGParallaxHeaderMode)mode hei self.scrollView.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height + height); [self.scrollView setParallaxHeaderView:view mode:mode height:height]; [self addSubview:self.scrollView]; + + //I'm not a big fan of KVO but its the only way I found to subtract minimum height to container frame. + [self addObserver:self forKeyPath:kContaineFrameKeyPath options:NSKeyValueObservingOptionNew context:nil]; } #pragma mark Properties @@ -162,4 +166,23 @@ - (void)setProgressBlock:(MXProgressBlock)progressBlock { self.scrollView.progressBlock = progressBlock; } +#pragma mark KVO + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { + if (object == self && [keyPath isEqualToString:kContaineFrameKeyPath]) { + [self removeObserver:self forKeyPath:kContaineFrameKeyPath]; + + self.container.frame = (CGRect){ + .origin = self.container.frame.origin, + .size.width = self.container.frame.size.width, + .size.height = self.container.frame.size.height - self.scrollView.minimumHeigth + }; + + [self addObserver:self forKeyPath:kContaineFrameKeyPath options:NSKeyValueObservingOptionNew context:nil]; + } + else { + [super observeValueForKeyPath:keyPath ofObject:object change:change context:nil]; + } +} + @end diff --git a/Pod/Classes/MXSegmentedPager.h b/Pod/Classes/MXSegmentedPager.h index 2109e657a..6e3356dd6 100644 --- a/Pod/Classes/MXSegmentedPager.h +++ b/Pod/Classes/MXSegmentedPager.h @@ -146,7 +146,7 @@ /** The pages container size. Use this property to set up your pages frame. */ -@property (nonatomic, readwrite) CGSize containerSize; +@property (nonatomic, assign) CGSize containerSize; /** Reloads everything from scratch. redisplays pages in container. diff --git a/Pod/Classes/MXSegmentedPager.m b/Pod/Classes/MXSegmentedPager.m index c2c23248d..84bc7e012 100644 --- a/Pod/Classes/MXSegmentedPager.m +++ b/Pod/Classes/MXSegmentedPager.m @@ -126,6 +126,13 @@ - (CGSize)containerSize { return self.container.frame.size; } +- (void)setContainerSize:(CGSize)containerSize { + self.container.frame = (CGRect){ + .origin = self.container.frame.origin, + .size = containerSize + }; +} + #pragma mark HMSegmentedControl target - (void)pageControlValueChanged:(HMSegmentedControl*)segmentedControl { @@ -196,10 +203,10 @@ - (void)layoutContainer { UIView* view = [self.pages objectAtIndex:index]; [self.container addSubview:view]; - CGRect frame = (CGRect) { - .origin.x = width, - .origin.y = view.frame.origin.y, - .size = view.frame.size + CGRect frame = (CGRect){ + .origin.x = width, + .origin.y = 0.f, + .size = self.containerSize }; view.frame = frame; width += self.frame.size.width; @@ -213,17 +220,17 @@ - (void)layoutContainer { - (void) layoutWithHeight:(CGFloat)height { CGRect subFrame = (CGRect) { - .origin = CGPointZero, - .size.width = self.frame.size.width, - .size.height = height + .origin = CGPointZero, + .size.width = self.frame.size.width, + .size.height = height }; self.segmentedControl.frame = subFrame; subFrame = (CGRect) { - .origin.x = 0.f, - .origin.y = height, - .size.width = self.frame.size.width, - .size.height = self.frame.size.height - height + .origin.x = 0.f, + .origin.y = height, + .size.width = self.frame.size.width, + .size.height = self.frame.size.height - height }; self.container.frame = subFrame; }