diff --git a/YRDropdownExample/ViewController.m b/YRDropdownExample/ViewController.m index e6d0a60..ae61ffa 100644 --- a/YRDropdownExample/ViewController.m +++ b/YRDropdownExample/ViewController.m @@ -31,7 +31,7 @@ - (void)viewDidUnload - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { - return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); + return YES; } - (IBAction)showInView:(id)sender { diff --git a/YRDropdownExample/YRDropdownView/YRDropdownView.h b/YRDropdownExample/YRDropdownView/YRDropdownView.h index 8683615..914c5b6 100644 --- a/YRDropdownExample/YRDropdownView/YRDropdownView.h +++ b/YRDropdownExample/YRDropdownView/YRDropdownView.h @@ -22,6 +22,9 @@ SEL onTouch; NSDate *showStarted; BOOL shouldAnimate; + BOOL isWindow; + BOOL isView; + } @property (copy) NSString *titleText; @@ -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 @@ -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; diff --git a/YRDropdownExample/YRDropdownView/YRDropdownView.m b/YRDropdownExample/YRDropdownView/YRDropdownView.m index ccbd655..afd5b5d 100644 --- a/YRDropdownExample/YRDropdownView/YRDropdownView.m +++ b/YRDropdownExample/YRDropdownView/YRDropdownView.m @@ -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 @@ -139,6 +141,8 @@ - (id)initWithFrame:(CGRect)frame self.opaque = YES; onTouch = @selector(hide:); + currentDropdown.isWindow = NO; + currentDropdown.isView = NO; } return self; } @@ -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]; } @@ -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; @@ -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