diff --git a/SIAlertView/SIAlertView.h b/SIAlertView/SIAlertView.h index d65b7e9..48731ed 100644 --- a/SIAlertView/SIAlertView.h +++ b/SIAlertView/SIAlertView.h @@ -40,10 +40,12 @@ typedef NS_ENUM(NSInteger, SIAlertViewTransitionStyle) { @class SIAlertView; typedef void(^SIAlertViewHandler)(SIAlertView *alertView); -@interface SIAlertView : UIView +@interface SIAlertView : UIView @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *message; +@property (nonatomic, strong) UITextField *textField; +@property (nonatomic, copy) void(^textFieldReturnBlock)(); @property (nonatomic, copy) NSAttributedString *attributedTitle; @property (nonatomic, copy) NSAttributedString *attributedMessage; @@ -58,7 +60,7 @@ typedef void(^SIAlertViewHandler)(SIAlertView *alertView); @property (nonatomic, copy) SIAlertViewHandler didDismissHandler; @property (nonatomic, readonly, getter = isVisible) BOOL visible; - +@property (nonatomic, assign) BOOL enableAutoRotation;//default NO, doesn't support autorotation or landscape. @property (nonatomic, assign) BOOL parallaxEffectEnabled; // theme @@ -79,7 +81,6 @@ typedef void(^SIAlertViewHandler)(SIAlertView *alertView); @property (nonatomic, strong) UIColor *cancelButtonBackgroundColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; @property (nonatomic, strong) UIColor *destructiveButtonBackgroundColor NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; - - (id)initWithTitle:(NSString *)title message:(NSString *)message; - (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButton:(NSString *)canelButton handler:(SIAlertViewHandler)handler; - (id)initWithAttributedTitle:(NSAttributedString *)attributedTitle attributedMessage:(NSAttributedString *)attributedMessage; diff --git a/SIAlertView/SIAlertView.m b/SIAlertView/SIAlertView.m index 14a392f..69f335e 100644 --- a/SIAlertView/SIAlertView.m +++ b/SIAlertView/SIAlertView.m @@ -166,6 +166,14 @@ - (void)viewDidLoad [self.alertView setup]; } +-(BOOL)shouldAutorotate{ + return self.alertView.enableAutoRotation; +} + +-(UIInterfaceOrientationMask)supportedInterfaceOrientations{ + return self.alertView.enableAutoRotation ? [super supportedInterfaceOrientations] : UIInterfaceOrientationMaskPortrait; +} + - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; @@ -178,9 +186,11 @@ - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrie #pragma mark - SIAlertView @implementation SIAlertView - @synthesize title = _title, message = _message; +@synthesize textField; +@synthesize textFieldReturnBlock; @synthesize attributedTitle = _attributedTitle, attributedMessage = _attributedMessage; +@synthesize enableAutoRotation; + (void)initialize { @@ -283,6 +293,9 @@ + (void)setAnimating:(BOOL)animating + (void)showBackground { + if (__si_alert_background_window != nil){ + [SIAlertView hideBackgroundAnimated:NO]; + } if (!__si_alert_background_window) { __si_alert_background_window = [[SIAlertBackgroundWindow alloc] initWithFrame:[UIScreen mainScreen].bounds andStyle:[SIAlertView currentAlertView].backgroundStyle]; @@ -301,13 +314,11 @@ + (void)showBackground + (void)hideBackgroundAnimated:(BOOL)animated { void (^completion)(void) = ^{ - [__si_alert_background_window removeFromSuperview]; - __si_alert_background_window = nil; - UIWindow *mainWindow = [UIApplication sharedApplication].windows[0]; mainWindow.tintAdjustmentMode = UIViewTintAdjustmentModeNormal; [mainWindow makeKeyWindow]; - mainWindow.hidden = NO; + __si_alert_background_window.hidden = YES; + __si_alert_background_window = nil; }; if (!animated) { @@ -548,8 +559,6 @@ - (void)dismissAnimated:(BOOL)animated cleanup:(BOOL)cleanup } void (^dismissComplete)(void) = ^{ - self.visible = NO; - [self teardown]; [SIAlertView setCurrentAlertView:nil]; @@ -825,6 +834,12 @@ - (void)validateLayout self.messageLabel.frame = CGRectMake(CONTENT_PADDING_LEFT, y, CONTAINER_WIDTH - CONTENT_PADDING_LEFT * 2, height); y += height + GAP; } + + if (self.textField) { + y += GAP; + self.textField.frame = CGRectMake(CONTENT_PADDING_LEFT, y, CONTAINER_WIDTH - CONTENT_PADDING_LEFT * 2, 30); + y += 30 + GAP; + } contentContainerViewHeight = y; if (self.items.count > 0) { @@ -907,6 +922,7 @@ - (void)setup [self setupViewHierarchy]; [self updateTitleLabel]; [self updateMessageLabel]; + [self updateTextField]; [self setupButtons]; [self setupLineLayer]; } @@ -917,8 +933,13 @@ - (void)teardown self.containerView = nil; self.titleLabel = nil; self.messageLabel = nil; + if (self.textField) { + [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil]; + self.textFieldReturnBlock = nil; + self.textField = nil; + } [self.buttons removeAllObjects]; - [self.alertWindow removeFromSuperview]; + self.alertWindow.hidden = YES; self.alertWindow = nil; self.layoutDirty = NO; } @@ -996,6 +1017,39 @@ - (void)updateMessageLabel [self invalidateLayout]; } +-(void)updateTextField{ + if (self.textField) { + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; + self.textField.delegate = self; + [self.contentContainerView addSubview:self.textField]; + } +} + +// Called when the UIKeyboardDidShowNotification is sent. +- (void)keyboardWasShown:(NSNotification*)aNotification +{ + NSDictionary* info = [aNotification userInfo]; + CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; + + CGRect containerViewFrame = self.containerView.frame; + containerViewFrame.origin.y = self.frame.size.height - kbSize.height - containerViewFrame.size.height - 6; + if (containerViewFrame.origin.y < self.containerView.frame.origin.y) { + self.containerView.frame = containerViewFrame; + } +} + +#pragma mark - UITextFieldDelegate +- (BOOL)textFieldShouldReturn:(UITextField *)aTextField{ + if (aTextField == self.textField) { + [self.textField resignFirstResponder]; + if (textFieldReturnBlock) { + textFieldReturnBlock(); + } + return YES; + } + return NO; +} + - (void)setupButtons { self.buttons = [[NSMutableArray alloc] initWithCapacity:self.items.count];