From d15fd9c6e353ea0f3dc7f0247514429b204f1b1d Mon Sep 17 00:00:00 2001 From: "LuXo (Home iMac)" Date: Tue, 14 May 2013 18:00:56 +0200 Subject: [PATCH 1/4] Check for application orientation, not device (which doesn't take into account locked orientation). --- RDActionSheet/RDActionSheet.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/RDActionSheet/RDActionSheet.m b/RDActionSheet/RDActionSheet.m index 4da361a..aa35f16 100644 --- a/RDActionSheet/RDActionSheet.m +++ b/RDActionSheet/RDActionSheet.m @@ -180,7 +180,7 @@ - (void)setupButtons { for (UIButton *button in self.buttons) { CGFloat buttonWidth; - UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; + UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) { buttonWidth = kLandscapeButtonWidth; } @@ -205,7 +205,7 @@ - (void)setupButtons { - (void)setupTitle { CGFloat labelWidth; - UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; + UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) { labelWidth = kLandscapeButtonWidth; } From 6c8ece8eaf308b18028be224a21bcbf8fa454192 Mon Sep 17 00:00:00 2001 From: "LuXo (Home iMac)" Date: Fri, 27 Sep 2013 17:15:57 +0200 Subject: [PATCH 2/4] If we are using iOS7 use the default UIActionSheet. --- RDActionSheet/RDActionSheet.m | 125 +++++++++++++++++++++++++++++++++- 1 file changed, 124 insertions(+), 1 deletion(-) diff --git a/RDActionSheet/RDActionSheet.m b/RDActionSheet/RDActionSheet.m index aa35f16..11f3c69 100644 --- a/RDActionSheet/RDActionSheet.m +++ b/RDActionSheet/RDActionSheet.m @@ -10,8 +10,9 @@ #import -@interface RDActionSheet () +@interface RDActionSheet () +@property (nonatomic, weak) UIActionSheet *ios7actionSheet; @property (nonatomic, strong) UIView *blackOutView; @property (nonatomic, strong) UILabel *titleLabel; @@ -42,6 +43,9 @@ - (void)buttonWasPressed:(id)button; const CGFloat kActionSheetAnimationTime = 0.2; const CGFloat kBlackoutViewFadeInOpacity = 0.6; +const NSInteger kCancelButtonTag = 23; +const NSInteger kDestructiveButtonTag = 25; +const NSInteger kNormalButtonTag = 12; @implementation RDActionSheet @@ -50,6 +54,7 @@ @implementation RDActionSheet @synthesize buttons; @synthesize blackOutView; +@synthesize ios7actionSheet; #pragma mark - Initialization @@ -126,6 +131,10 @@ - (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButton - (void)layoutSubviews { + if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { + return; + } + [self setupBackground]; [self setupTitle]; [self setupButtons]; @@ -274,6 +283,7 @@ - (UIButton *)buildButtonWithTitle:(NSString *)title { button.titleLabel.layer.shadowRadius = 0.0; button.titleLabel.layer.shadowOffset = CGSizeMake(0.0, 1.0); button.titleLabel.layer.shadowOpacity = 0.5; + button.tag = kNormalButtonTag; return button; } @@ -289,6 +299,7 @@ - (UIButton *)buildCancelButtonWithTitle:(NSString *)title { [button setBackgroundImage:touchBackgroundImage forState:UIControlStateHighlighted]; button.titleLabel.layer.shadowOpacity = 0.3; + button.tag = kCancelButtonTag; return button; } @@ -306,6 +317,7 @@ - (UIButton *)buildPrimaryButtonWithTitle:(NSString *)title { button.titleLabel.layer.shadowColor = [UIColor blackColor].CGColor; button.titleLabel.layer.shadowOffset = CGSizeMake(0.0, -1.0); + button.tag = kNormalButtonTag; return button; } @@ -323,6 +335,7 @@ - (UIButton *)buildDestroyButtonWithTitle:(NSString *)title { button.titleLabel.layer.shadowColor = [UIColor blackColor].CGColor; button.titleLabel.layer.shadowOffset = CGSizeMake(0.0, -1.0); + button.tag = kDestructiveButtonTag; return button; } @@ -379,13 +392,56 @@ - (void)hideActionSheetWithButtonIndex:(NSInteger)buttonIndex { } -(void)cancelActionSheet { + if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { + [self.ios7actionSheet dismissWithClickedButtonIndex:-1 animated:YES]; + return; + } [self hideActionSheetWithButtonIndex:-1]; } #pragma mark - Present action sheet - (void)showFrom:(UIView *)view { + + if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { + NSInteger destructiveIndex = NSNotFound; + NSInteger cancelIndex = NSNotFound; + + UIActionSheet *actionSheet = [[UIActionSheet alloc] init]; + actionSheet.title = self.titleLabel.text; + + NSInteger index = 0; + for(UIButton *button in [self.buttons reverseObjectEnumerator]) { + switch (button.tag) { + case kDestructiveButtonTag: + destructiveIndex = index; + break; + case kCancelButtonTag: + cancelIndex = index; + break; + default: + break; + } + [actionSheet addButtonWithTitle: [button titleForState:UIControlStateNormal]]; + index++; + } + + if(destructiveIndex != NSNotFound) + actionSheet.destructiveButtonIndex = destructiveIndex; + + if(cancelIndex != NSNotFound) + actionSheet.cancelButtonIndex = cancelIndex; + + actionSheet.delegate = self; + [actionSheet showInView: view]; + + [view addSubview: self]; + + self.ios7actionSheet = actionSheet; + return; + } + CGFloat startPosition = view.bounds.origin.y + view.bounds.size.height; self.frame = CGRectMake(0, startPosition, view.bounds.size.width, [self calculateSheetHeight]); [view addSubview:self]; @@ -426,4 +482,71 @@ - (CGFloat)calculateSheetHeight { return floorf((kButtonHeight * self.buttons.count) + (self.buttons.count * kButtonPadding) + kButtonHeight/2) + self.titleLabel.bounds.size.height + 4; } +#pragma mark - UIActionSheetDelegate methods for iOS7 + +// Called when a button is clicked. The view will be automatically dismissed after this call returns +- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { + + NSInteger rdActionSheetIndex = [self.buttons count]-1-buttonIndex; + + if (self.callbackBlock) { + self.callbackBlock(RDActionSheetCallbackTypeClickedButtonAtIndex, rdActionSheetIndex, [[[self.buttons objectAtIndex:rdActionSheetIndex] titleLabel] text]); + } + else { + if (self.delegate && [self.delegate respondsToSelector:@selector(actionSheet:clickedButtonAtIndex:)]) { + [self.delegate actionSheet:self clickedButtonAtIndex:rdActionSheetIndex]; + } + } +} + +- (void)willPresentActionSheet:(UIActionSheet *)actionSheet { + if (self.callbackBlock) { + self.callbackBlock(RDActionSheetCallbackTypeWillPresentActionSheet, -1, nil); + } + else { + if (self.delegate && [self.delegate respondsToSelector:@selector(willPresentActionSheet:)]) { + [self.delegate willPresentActionSheet:self]; + } + } +} + +- (void)didPresentActionSheet:(UIActionSheet *)actionSheet{ + if (self.callbackBlock) { + self.callbackBlock(RDActionSheetCallbackTypeDidPresentActionSheet, -1, nil); + } + else { + if (self.delegate && [self.delegate respondsToSelector:@selector(didPresentActionSheet:)]) { + [self.delegate didPresentActionSheet:self]; + } + } +} + +- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex { + NSInteger rdActionSheetIndex = [self.buttons count]-1-buttonIndex; + if (self.callbackBlock) { + self.callbackBlock(RDActionSheetCallbackTypeWillDismissWithButtonIndex, rdActionSheetIndex, [[[self.buttons objectAtIndex:rdActionSheetIndex] titleLabel] text]); + } + else { + if (self.delegate && [self.delegate respondsToSelector:@selector(actionSheet:willDismissWithButtonIndex:)]) { + [self.delegate actionSheet:self willDismissWithButtonIndex:rdActionSheetIndex]; + } + } +} + +- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ + NSInteger rdActionSheetIndex = [self.buttons count]-1-buttonIndex; + if (self.callbackBlock) { + self.callbackBlock(RDActionSheetCallbackTypeDidDismissWithButtonIndex, rdActionSheetIndex, [[[self.buttons objectAtIndex:rdActionSheetIndex] titleLabel] text]); + } + else { + if (self.delegate && [self.delegate respondsToSelector:@selector(actionSheet:didDismissWithButtonIndex:)]) { + [self.delegate actionSheet:self didDismissWithButtonIndex:rdActionSheetIndex]; + } + } + + if(self.superview != nil) { + [self removeFromSuperview]; + } +} + @end From 0f6b97c6889d7ff0741b468d3eb88d93457f6a13 Mon Sep 17 00:00:00 2001 From: "LuXo (Home iMac)" Date: Tue, 1 Oct 2013 04:17:37 +0200 Subject: [PATCH 3/4] Don't perform actions if index is -1 --- RDActionSheet/RDActionSheet.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/RDActionSheet/RDActionSheet.m b/RDActionSheet/RDActionSheet.m index 11f3c69..ef2ae09 100644 --- a/RDActionSheet/RDActionSheet.m +++ b/RDActionSheet/RDActionSheet.m @@ -522,6 +522,8 @@ - (void)didPresentActionSheet:(UIActionSheet *)actionSheet{ } - (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex { + if(buttonIndex < 0) + return; NSInteger rdActionSheetIndex = [self.buttons count]-1-buttonIndex; if (self.callbackBlock) { self.callbackBlock(RDActionSheetCallbackTypeWillDismissWithButtonIndex, rdActionSheetIndex, [[[self.buttons objectAtIndex:rdActionSheetIndex] titleLabel] text]); @@ -534,6 +536,8 @@ - (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSI } - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ + if(buttonIndex < 0) + return; NSInteger rdActionSheetIndex = [self.buttons count]-1-buttonIndex; if (self.callbackBlock) { self.callbackBlock(RDActionSheetCallbackTypeDidDismissWithButtonIndex, rdActionSheetIndex, [[[self.buttons objectAtIndex:rdActionSheetIndex] titleLabel] text]); From 8d9b4766c070cb33cddae4aa4febdd1e695096fb Mon Sep 17 00:00:00 2001 From: "LuXo (Home iMac)" Date: Tue, 19 Nov 2013 10:08:16 +0100 Subject: [PATCH 4/4] Use always default iOS actionsheets (but maintain block based declaration) --- RDActionSheet/RDActionSheet.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/RDActionSheet/RDActionSheet.m b/RDActionSheet/RDActionSheet.m index ef2ae09..798041f 100644 --- a/RDActionSheet/RDActionSheet.m +++ b/RDActionSheet/RDActionSheet.m @@ -131,7 +131,7 @@ - (id)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButton - (void)layoutSubviews { - if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { + if(YES || floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { return; } @@ -392,7 +392,7 @@ - (void)hideActionSheetWithButtonIndex:(NSInteger)buttonIndex { } -(void)cancelActionSheet { - if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { + if(YES || floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { [self.ios7actionSheet dismissWithClickedButtonIndex:-1 animated:YES]; return; } @@ -403,7 +403,7 @@ -(void)cancelActionSheet { - (void)showFrom:(UIView *)view { - if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { + if(YES || floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { NSInteger destructiveIndex = NSNotFound; NSInteger cancelIndex = NSNotFound;