Skip to content

Commit

Permalink
Fixed status bar issue and improved control hiding and showing in iOS 7.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwaterfall committed Oct 3, 2013
1 parent 6f1e070 commit a597fa7
Showing 1 changed file with 67 additions and 21 deletions.
88 changes: 67 additions & 21 deletions MWPhotoBrowser/Classes/MWPhotoBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ - (CGRect)frameForPagingScrollView {
CGRect frame = self.view.bounds;// [[UIScreen mainScreen] bounds];
frame.origin.x -= PADDING;
frame.size.width += (2 * PADDING);
return frame;
return CGRectIntegral(frame);
}

- (CGRect)frameForPageAtIndex:(NSUInteger)index {
Expand All @@ -874,7 +874,7 @@ - (CGRect)frameForPageAtIndex:(NSUInteger)index {
CGRect pageFrame = bounds;
pageFrame.size.width -= (2 * PADDING);
pageFrame.origin.x = (bounds.size.width * index) + PADDING;
return pageFrame;
return CGRectIntegral(pageFrame);
}

- (CGSize)contentSizeForPagingScrollView {
Expand All @@ -893,15 +893,17 @@ - (CGRect)frameForToolbarAtOrientation:(UIInterfaceOrientation)orientation {
CGFloat height = 44;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone &&
UIInterfaceOrientationIsLandscape(orientation)) height = 32;
return CGRectMake(0, self.view.bounds.size.height - height, self.view.bounds.size.width, height);
return CGRectIntegral(CGRectMake(0, self.view.bounds.size.height - height, self.view.bounds.size.width, height));
}

- (CGRect)frameForCaptionView:(MWCaptionView *)captionView atIndex:(NSUInteger)index {
CGRect pageFrame = [self frameForPageAtIndex:index];
captionView.frame = CGRectMake(0, 0, pageFrame.size.width, 44); // set initial frame
CGSize captionSize = [captionView sizeThatFits:CGSizeMake(pageFrame.size.width, 0)];
CGRect captionFrame = CGRectMake(pageFrame.origin.x, pageFrame.size.height - captionSize.height - (_toolbar.superview?_toolbar.frame.size.height:0), pageFrame.size.width, captionSize.height);
return captionFrame;
CGRect captionFrame = CGRectMake(pageFrame.origin.x,
pageFrame.size.height - captionSize.height - (_toolbar.superview?_toolbar.frame.size.height:0),
pageFrame.size.width,
captionSize.height);
return CGRectIntegral(captionFrame);
}

#pragma mark - UIScrollView Delegate
Expand Down Expand Up @@ -975,6 +977,7 @@ - (void)gotoNextPage { [self jumpToPageAtIndex:_currentPageIndex+1]; }
#pragma mark - Control Hiding / Showing

// If permanent then we don't set timers to hide again
// Fades all controls on iOS 5 & 6, and iOS 7 controls slide and fade
- (void)setControlsHidden:(BOOL)hidden animated:(BOOL)animated permanent:(BOOL)permanent {

// Force visible if no photos
Expand All @@ -983,13 +986,20 @@ - (void)setControlsHidden:(BOOL)hidden animated:(BOOL)animated permanent:(BOOL)p
// Cancel any timers
[self cancelControlHiding];

// Animations & positions
BOOL slideAndFade = SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7");
CGFloat animatonOffset = 20;
CGFloat animationDuration = (animated ? 0.35 : 0);

// Status bar
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {

// Hide status bar
// Not working because of https://devforums.apple.com/thread/207799
_statusBarShouldBeHidden = hidden;
[self setNeedsStatusBarAppearanceUpdate];

[UIView animateWithDuration:animationDuration animations:^(void) {
[self setNeedsStatusBarAppearanceUpdate];
} completion:^(BOOL finished) {}];

} else {

// Status bar and nav bar positioning
Expand Down Expand Up @@ -1020,18 +1030,54 @@ - (void)setControlsHidden:(BOOL)hidden animated:(BOOL)animated permanent:(BOOL)p

}

// Captions
NSMutableSet *captionViews = [[NSMutableSet alloc] initWithCapacity:_visiblePages.count];
for (MWZoomingScrollView *page in _visiblePages) {
if (page.captionView) [captionViews addObject:page.captionView];
// Toolbar, nav bar and captions
// Pre-appear animation positions for iOS 7 sliding
if (slideAndFade && [self areControlsHidden] && !hidden && animated) {

// Toolbar
_toolbar.frame = CGRectOffset([self frameForToolbarAtOrientation:self.interfaceOrientation], 0, animatonOffset);

// Captions
for (MWZoomingScrollView *page in _visiblePages) {
if (page.captionView) {
MWCaptionView *v = page.captionView;
// Pass any index, all we're interested in is the Y
CGRect captionFrame = [self frameForCaptionView:v atIndex:0];
captionFrame.origin.x = v.frame.origin.x; // Reset X
v.frame = CGRectOffset(captionFrame, 0, animatonOffset);
}
}

}

// Hide/show bars
[UIView animateWithDuration:(animated ? 0.35 : 0) animations:^(void) {
[UIView animateWithDuration:animationDuration animations:^(void) {

CGFloat alpha = hidden ? 0 : 1;

// Nav bar slides up on it's own on iOS 7
[self.navigationController.navigationBar setAlpha:alpha];
[_toolbar setAlpha:alpha];
for (UIView *v in captionViews) v.alpha = alpha;

// Toolbar
if (slideAndFade) {
_toolbar.frame = [self frameForToolbarAtOrientation:self.interfaceOrientation];
if (hidden) _toolbar.frame = CGRectOffset(_toolbar.frame, 0, animatonOffset);
}
_toolbar.alpha = alpha;

// Captions
for (MWZoomingScrollView *page in _visiblePages) {
if (page.captionView) {
MWCaptionView *v = page.captionView;
if (slideAndFade) {
// Pass any index, all we're interested in is the Y
CGRect captionFrame = [self frameForCaptionView:v atIndex:0];
captionFrame.origin.x = v.frame.origin.x; // Reset X
if (hidden) captionFrame = CGRectOffset(captionFrame, 0, animatonOffset);
v.frame = captionFrame;
}
v.alpha = alpha;
}
}

} completion:^(BOOL finished) {}];

// Control hiding timer
Expand All @@ -1046,7 +1092,7 @@ - (BOOL)prefersStatusBarHidden {
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
return UIStatusBarAnimationFade;
return UIStatusBarAnimationSlide;
}

- (void)cancelControlHiding {
Expand All @@ -1065,7 +1111,7 @@ - (void)hideControlsAfterDelay {
}
}

- (BOOL)areControlsHidden { return (_toolbar.alpha == 0); /* [UIApplication sharedApplication].isStatusBarHidden; */ }
- (BOOL)areControlsHidden { return (_toolbar.alpha == 0); }
- (void)hideControls { [self setControlsHidden:YES animated:YES permanent:NO]; }
- (void)toggleControls { [self setControlsHidden:![self areControlsHidden] animated:YES permanent:NO]; }

Expand Down Expand Up @@ -1158,8 +1204,8 @@ - (void)actionButtonPressed:(id)sender {
// Show
__weak typeof(self) weakSelf = self;
[self.activityViewController setCompletionHandler:^(NSString *activityType, BOOL completed) {
[weakSelf hideControlsAfterDelay];
weakSelf.activityViewController = nil;
[weakSelf hideControlsAfterDelay];
[weakSelf hideProgressHUD:YES];
}];
[self presentViewController:self.activityViewController animated:YES completion:nil];
Expand Down

0 comments on commit a597fa7

Please sign in to comment.