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

Feature change for iPad/iPhone orientation #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion YRDropdownExample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ - (void)viewDidUnload

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
return YES;
}

- (IBAction)showInView:(id)sender {
Expand Down
7 changes: 7 additions & 0 deletions YRDropdownExample/YRDropdownView/YRDropdownView.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
SEL onTouch;
NSDate *showStarted;
BOOL shouldAnimate;
BOOL isWindow;
BOOL isView;

}

@property (copy) NSString *titleText;
Expand All @@ -31,6 +34,9 @@
@property (nonatomic, assign) UIImage *backgroundImage;
@property (nonatomic, assign) SEL onTouch;
@property (assign) BOOL shouldAnimate;
@property (assign) BOOL isWindow;
@property (assign) BOOL isView;


#pragma mark - View methods

Expand Down Expand Up @@ -63,6 +69,7 @@
+ (BOOL)hideDropdownInView:(UIView *)view animated:(BOOL)animated;

#pragma mark -
- (void)flipViewAccordingToStatusBarOrientation:(NSNotification *)notification;
- (void)show:(BOOL)animated;
- (void)hide:(BOOL)animated;

Expand Down
86 changes: 84 additions & 2 deletions YRDropdownExample/YRDropdownView/YRDropdownView.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ @implementation YRDropdownView
@synthesize accessoryImage;
@synthesize onTouch;
@synthesize shouldAnimate;
@synthesize isView;
@synthesize isWindow;

//Using this prevents two alerts to ever appear on the screen at the same time
//TODO: Queue alerts, if multiple
Expand Down Expand Up @@ -139,6 +141,8 @@ - (id)initWithFrame:(CGRect)frame
self.opaque = YES;

onTouch = @selector(hide:);
currentDropdown.isWindow = NO;
currentDropdown.isView = NO;
}
return self;
}
Expand Down Expand Up @@ -200,14 +204,21 @@ + (YRDropdownView *)showDropdownInView:(UIView *)view
dropdown.shouldAnimate = animated;

if ([view isKindOfClass:[UIWindow class]]) {
currentDropdown.isWindow = YES;
CGRect dropdownFrame = dropdown.frame;
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
dropdownFrame.origin.y = appFrame.origin.y;
dropdown.frame = dropdownFrame;
}else{
currentDropdown.isView = YES;
}

[view addSubview:dropdown];
[dropdown show:animated];

[[NSNotificationCenter defaultCenter] addObserver:dropdown selector:@selector(flipViewAccordingToStatusBarOrientation:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
[dropdown flipViewAccordingToStatusBarOrientation:nil];

if (delay != 0.0) {
[dropdown performSelector:@selector(hideUsingAnimation:) withObject:[NSNumber numberWithBool:animated] afterDelay:delay+ANIMATION_DURATION];
}
Expand All @@ -229,11 +240,15 @@ + (void)removeView

+ (BOOL)hideDropdownInView:(UIView *)view
{
[[NSNotificationCenter defaultCenter] removeObserver:self];

return [YRDropdownView hideDropdownInView:view animated:YES];
}

+ (BOOL)hideDropdownInView:(UIView *)view animated:(BOOL)animated
{
currentDropdown.isWindow = NO;
currentDropdown.isView = NO;
if (currentDropdown) {
[currentDropdown hideUsingAnimation:[NSNumber numberWithBool:animated]];
return YES;
Expand Down Expand Up @@ -390,17 +405,84 @@ - (void)layoutSubviews {
}

CGFloat dropdownHeight = 44.0f;
CGFloat startCoord = 0.0f;

if (self.detailText) {
dropdownHeight = MAX(CGRectGetMaxY(self.bounds), CGRectGetMaxY(detailLabel.frame));
dropdownHeight += VERTICAL_PADDING;
}

[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, dropdownHeight)];
if(currentDropdown.isWindow){
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
switch (orientation){
case UIInterfaceOrientationPortrait:
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width,dropdownHeight)];
break;
case UIInterfaceOrientationPortraitUpsideDown:
startCoord = self.frame.origin.y - dropdownHeight;
[self setFrame:CGRectMake(self.frame.origin.x, startCoord, self.frame.size.width,dropdownHeight)];
break;
case UIInterfaceOrientationLandscapeLeft:
// startCoord = self.frame.origin.x - dropdownHeight;
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, dropdownHeight,self.frame.size.height)];
break;
case UIInterfaceOrientationLandscapeRight:
startCoord = self.frame.origin.x - dropdownHeight;
[self setFrame:CGRectMake(startCoord, self.frame.origin.y, dropdownHeight,self.frame.size.height)];
break;
}


}
if(currentDropdown.isView)
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width,dropdownHeight)];

[backgroundImageView setFrame:self.bounds];

}

- (void)flipViewAccordingToStatusBarOrientation:(NSNotification *)notification {

if (currentDropdown.isWindow) {

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

CGFloat angle = 0.0;
CGRect newFrame = self.window.bounds;
CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;

CGFloat dropdownHeight = 44.0f;

switch (orientation) {
case UIInterfaceOrientationPortraitUpsideDown:
angle = M_PI;
newFrame.origin.y = newFrame.size.height - statusBarSize.height;
newFrame.size.height = dropdownHeight;
break;
case UIInterfaceOrientationLandscapeLeft:
angle = - M_PI / 2.0f;
newFrame.origin.x = statusBarSize.width;
newFrame.size.width = dropdownHeight;
break;
case UIInterfaceOrientationLandscapeRight:
angle = M_PI / 2.0f;
newFrame.origin.x = newFrame.size.width - statusBarSize.width;
newFrame.size.width = dropdownHeight;
break;
default: // as UIInterfaceOrientationPortrait
angle = 0.0;
newFrame.origin.y += statusBarSize.height;
newFrame.size.height = dropdownHeight;
newFrame.size.width = statusBarSize.width;
break;
}

self.transform = CGAffineTransformMakeRotation(angle);
self.frame = newFrame;
}
}


@end