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

fix 2 bugs #114

Open
wants to merge 4 commits into
base: ios7
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
7 changes: 4 additions & 3 deletions SIAlertView/SIAlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ typedef NS_ENUM(NSInteger, SIAlertViewTransitionStyle) {
@class SIAlertView;
typedef void(^SIAlertViewHandler)(SIAlertView *alertView);

@interface SIAlertView : UIView
@interface SIAlertView : UIView<UITextFieldDelegate>

@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;
Expand All @@ -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
Expand All @@ -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;
Expand Down
70 changes: 62 additions & 8 deletions SIAlertView/SIAlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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
{
Expand Down Expand Up @@ -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];
Expand All @@ -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) {
Expand Down Expand Up @@ -548,8 +559,6 @@ - (void)dismissAnimated:(BOOL)animated cleanup:(BOOL)cleanup
}

void (^dismissComplete)(void) = ^{
self.visible = NO;

[self teardown];

[SIAlertView setCurrentAlertView:nil];
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -907,6 +922,7 @@ - (void)setup
[self setupViewHierarchy];
[self updateTitleLabel];
[self updateMessageLabel];
[self updateTextField];
[self setupButtons];
[self setupLineLayer];
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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];
Expand Down