Skip to content

Commit

Permalink
Fix container height while using minimum height with parallax header.
Browse files Browse the repository at this point in the history
Fix example.
  • Loading branch information
maxep committed Mar 20, 2015
1 parent 79cef06 commit d06f506
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 19 deletions.
12 changes: 7 additions & 5 deletions Example/MXSegmentedPager/Base.lproj/Main_iPhone.storyboard
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6254" systemVersion="14C109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="o9c-rq-WX6">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6751" systemVersion="14C1510" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="o9c-rq-WX6">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6247"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6736"/>
</dependencies>
<scenes>
<!--Root View Controller-->
<scene sceneID="xN4-dL-yXS">
<objects>
<tableViewController id="FJE-cX-rWX" customClass="MXMenuViewController" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="pjH-Pf-vM2">
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<rect key="frame" x="0.0" y="64" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<connections>
Expand All @@ -27,10 +27,12 @@
<!--Navigation Controller-->
<scene sceneID="J4n-66-rW8">
<objects>
<navigationController id="o9c-rq-WX6" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" id="l1S-Xk-xhx">
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="o9c-rq-WX6" sceneMemberID="viewController">
<navigationBar key="navigationBar" opaque="NO" contentMode="scaleToFill" id="l1S-Xk-xhx">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<color key="tintColor" cocoaTouchSystemColor="darkTextColor"/>
</navigationBar>
<connections>
<segue destination="FJE-cX-rWX" kind="relationship" relationship="rootViewController" id="Rb0-mr-dtc"/>
Expand Down
10 changes: 9 additions & 1 deletion Example/MXSegmentedPager/MXParallaxViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
25 changes: 24 additions & 1 deletion Pod/Classes/MXSegmentedPager+ParallaxHeader.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return NO;
}
}

return YES;
}

Expand Down Expand Up @@ -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){
Expand All @@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion Pod/Classes/MXSegmentedPager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 18 additions & 11 deletions Pod/Classes/MXSegmentedPager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down

0 comments on commit d06f506

Please sign in to comment.