Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed nav bar offset calculation when status bar is hidden #92

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ REMenuItem *customViewItem = [[REMenuItem alloc] initWithCustomView:customView a
You can customize the following properties of `REMenu`:

``` objective-c
@property (assign, readwrite, nonatomic) UIEdgeInsetsMake contentInset; //padding for the menu
@property (assign, readwrite, nonatomic) CGFloat cornerRadius;
@property (strong, readwrite, nonatomic) UIColor *shadowColor;
@property (assign, readwrite, nonatomic) CGSize shadowOffset;
Expand Down
Empty file modified REMenu/RECommonFunctions.m
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion REMenu/REMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#import <QuartzCore/QuartzCore.h>
#import "RECommonFunctions.h"
#import "REMenuItem.h"
#import "REMenuContainerView.h"

@class REMenu;
@class REMenuItem;
Expand Down Expand Up @@ -67,6 +66,7 @@ typedef NS_ENUM(NSInteger, REMenuLiveBackgroundStyle) {

// Style
//
@property (assign, readwrite, nonatomic) UIEdgeInsets contentInset;
@property (assign, readwrite, nonatomic) CGFloat cornerRadius;
@property (strong, readwrite, nonatomic) UIColor *shadowColor;
@property (assign, readwrite, nonatomic) CGSize shadowOffset;
Expand Down
70 changes: 46 additions & 24 deletions REMenu/REMenu.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ @interface REMenuItem ()

@interface REMenu ()

@property (strong, readwrite, nonatomic) UIView *menuView;
@property (strong, readwrite, nonatomic) UIScrollView *menuView;
@property (strong, readwrite, nonatomic) UIView *menuWrapperView;
@property (strong, readwrite, nonatomic) REMenuContainerView *containerView;
@property (strong, readwrite, nonatomic) UIView *containerView;
@property (strong, readwrite, nonatomic) UIButton *backgroundButton;
@property (assign, readwrite, nonatomic) BOOL isOpen;
@property (assign, readwrite, nonatomic) BOOL isAnimating;
@property (strong, readwrite, nonatomic) NSMutableArray *itemViews;
@property (weak, readwrite, nonatomic) UINavigationBar *navigationBar;
@property (weak, readwrite, nonatomic) UIView *viewToShowFrom;
@property (strong, readwrite, nonatomic) UIToolbar *toolbar;

@end
Expand All @@ -59,6 +60,7 @@ - (id)init
_separatorHeight = 2.0;
_separatorOffset = CGSizeMake(0.0, 0.0);
_waitUntilAnimationIsComplete = YES;
_contentInset = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);

_textOffset = CGSizeMake(0, 0);
_subtitleTextOffset = CGSizeMake(0, 0);
Expand Down Expand Up @@ -116,11 +118,13 @@ - (void)showFromRect:(CGRect)rect inView:(UIView *)view

self.isOpen = YES;
self.isAnimating = YES;


self.viewToShowFrom = view;

// Create views
//
self.containerView = ({
REMenuContainerView *view = [[REMenuContainerView alloc] init];
UIView *view = [[UIView alloc] init];
view.clipsToBounds = YES;
view.autoresizingMask = UIViewAutoresizingFlexibleWidth;

Expand All @@ -130,9 +134,9 @@ - (void)showFromRect:(CGRect)rect inView:(UIView *)view
}
view;
});

self.menuView = ({
UIView *view = [[UIView alloc] init];
UIScrollView *view = [[UIScrollView alloc] init];
if (!self.liveBlur || !REUIKitIsFlatMode()) {
view.backgroundColor = self.backgroundColor;
}
Expand All @@ -143,6 +147,7 @@ - (void)showFromRect:(CGRect)rect inView:(UIView *)view
view.layer.shouldRasterize = YES;
view.layer.rasterizationScale = [UIScreen mainScreen].scale;
view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
view.contentInset = self.contentInset;
view;
});

Expand Down Expand Up @@ -184,8 +189,6 @@ - (void)showFromRect:(CGRect)rect inView:(UIView *)view
button;
});

CGFloat navigationBarOffset = self.appearsBehindNavigationBar && self.navigationBar ? 64 : 0;

// Append new item views to REMenuView
//
for (REMenuItem *item in self.items) {
Expand All @@ -196,15 +199,15 @@ - (void)showFromRect:(CGRect)rect inView:(UIView *)view
itemHeight += self.cornerRadius;

UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake(self.separatorOffset.width,
index * self.itemHeight + index * self.separatorHeight + 40.0 + navigationBarOffset + self.separatorOffset.height,
index * self.itemHeight + index * self.separatorHeight + self.separatorOffset.height,
rect.size.width - self.separatorOffset.width,
self.separatorHeight)];
separatorView.backgroundColor = self.separatorColor;
separatorView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.menuView addSubview:separatorView];

REMenuItemView *itemView = [[REMenuItemView alloc] initWithFrame:CGRectMake(0,
index * self.itemHeight + (index + 1.0) * self.separatorHeight + 40.0 + navigationBarOffset,
index * self.itemHeight + (index + 1.0) * self.separatorHeight,
rect.size.width,
itemHeight)
menu:self item:item
Expand All @@ -219,12 +222,15 @@ - (void)showFromRect:(CGRect)rect inView:(UIView *)view
}
[self.menuView addSubview:itemView];
}

// Set up frames
//
self.menuWrapperView.frame = CGRectMake(0, -self.combinedHeight - navigationBarOffset, rect.size.width, self.combinedHeight + navigationBarOffset);
self.menuView.frame = self.menuWrapperView.bounds;
if (REUIKitIsFlatMode() && self.liveBlur) {
self.menuWrapperView.frame = CGRectMake(0, -self.combinedHeight, self.viewToShowFrom.bounds.size.width, self.combinedHeight);
CGFloat height = MIN(self.viewToShowFrom.bounds.size.height - self.navigationBarOffset, self.combinedHeight);
self.menuView.frame = CGRectMake(0, 0, self.viewToShowFrom.bounds.size.width, height);
self.menuView.contentSize = CGSizeMake(self.viewToShowFrom.bounds.size.width, self.combinedHeight);

if (REUIKitIsFlatMode() && self.liveBlur) {
self.toolbar.frame = self.menuWrapperView.bounds;
}
self.containerView.frame = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
Expand Down Expand Up @@ -257,7 +263,7 @@ - (void)showFromRect:(CGRect)rect inView:(UIView *)view
animations:^{
self.backgroundView.alpha = self.backgroundAlpha;
CGRect frame = self.menuView.frame;
frame.origin.y = -40.0 - self.separatorHeight;
frame.origin.y = self.navigationBarOffset;
self.menuWrapperView.frame = frame;
} completion:^(BOOL finished) {
self.isAnimating = NO;
Expand All @@ -272,7 +278,7 @@ - (void)showFromRect:(CGRect)rect inView:(UIView *)view
animations:^{
self.backgroundView.alpha = self.backgroundAlpha;
CGRect frame = self.menuView.frame;
frame.origin.y = -40.0 - self.separatorHeight;
frame.origin.y = self.navigationBarOffset;
self.menuWrapperView.frame = frame;
} completion:^(BOOL finished) {
self.isAnimating = NO;
Expand All @@ -289,7 +295,7 @@ - (void)showFromRect:(CGRect)rect inView:(UIView *)view
animations:^{
self.backgroundView.alpha = self.backgroundAlpha;
CGRect frame = self.menuView.frame;
frame.origin.y = -40.0 - self.separatorHeight;
frame.origin.y = self.navigationBarOffset;
self.menuWrapperView.frame = frame;
} completion:^(BOOL finished) {
self.isAnimating = NO;
Expand All @@ -298,6 +304,25 @@ - (void)showFromRect:(CGRect)rect inView:(UIView *)view
}
}];
}

[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(updateViewOnRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}

-(CGFloat) navigationBarOffset {
CGFloat statusBarHeight = MIN([[UIApplication sharedApplication] statusBarFrame].size.width, [[UIApplication sharedApplication] statusBarFrame].size.height);

return self.navigationBar ? self.navigationBar.frame.size.height + statusBarHeight : 0;
}

-(void) updateViewOnRotate:(NSNotification*) notification {
CGFloat height = MIN(self.viewToShowFrom.bounds.size.height - self.navigationBarOffset, self.combinedHeight);
self.menuView.frame = CGRectMake(0, 0, self.viewToShowFrom.bounds.size.width, height);
self.menuView.contentSize = CGSizeMake(self.viewToShowFrom.bounds.size.width, self.combinedHeight);
self.menuWrapperView.frame = CGRectMake(0, self.navigationBarOffset, self.viewToShowFrom.bounds.size.width, height);
}

- (void)showInView:(UIView *)view
Expand All @@ -313,8 +338,6 @@ - (void)showFromNavigationController:(UINavigationController *)navigationControl

self.navigationBar = navigationController.navigationBar;
[self showFromRect:CGRectMake(0, 0, navigationController.navigationBar.frame.size.width, navigationController.view.frame.size.height) inView:navigationController.view];
self.containerView.appearsBehindNavigationBar = self.appearsBehindNavigationBar;
self.containerView.navigationBar = navigationController.navigationBar;
if (self.appearsBehindNavigationBar) {
[navigationController.view bringSubviewToFront:navigationController.navigationBar];
}
Expand All @@ -325,16 +348,15 @@ - (void)closeWithCompletion:(void (^)(void))completion
if (self.isAnimating) return;

self.isAnimating = YES;

CGFloat navigationBarOffset = self.appearsBehindNavigationBar && self.navigationBar ? 64 : 0;

[[NSNotificationCenter defaultCenter] removeObserver:self];

void (^closeMenu)(void) = ^{
[UIView animateWithDuration:self.closeAnimationDuration
delay:0.0
options:UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseInOut
animations:^ {
CGRect frame = self.menuView.frame;
frame.origin.y = - self.combinedHeight - navigationBarOffset;
frame.origin.y = - self.combinedHeight - self.navigationBarOffset;
self.menuWrapperView.frame = frame;
self.backgroundView.alpha = 0;
} completion:^(BOOL finished) {
Expand Down Expand Up @@ -388,7 +410,7 @@ - (void)close

- (CGFloat)combinedHeight
{
return self.items.count * self.itemHeight + self.items.count * self.separatorHeight + 40.0 + self.cornerRadius;
return self.items.count * self.itemHeight + self.items.count * self.separatorHeight + self.cornerRadius;
}

- (void)setNeedsLayout
Expand Down
33 changes: 0 additions & 33 deletions REMenu/REMenuContainerView.h

This file was deleted.

51 changes: 0 additions & 51 deletions REMenu/REMenuContainerView.m

This file was deleted.