兼容iOs7的自定义alertView
转载请注明出处。
升级到ios7后,旧项目中使用的继承UIAlertView的自定义alertview无法正常显示了,无奈只好换思路去实现,改成从当前keywindow下创建要显示的alertview,并模仿了系统alertview
.h文件
- #import <UIKit/UIKit.h>
- typedef enum
- {
- CustomAlertViewType_Msg_TwoBtn=,//含有title,提示内容以及两个button.
- CustomAlertViewType_Msg_OneBtn,//含有title,提示内容以及一个button.
- CustomAlertViewType_ActivityIndiAndMsg_OneBtn, //含有title,UIActivityIndicatorView控件,提示内容以及一个button.
- CustomAlertViewType_Msg_TextField_TwoBtn,
- CustomAlertViewType_JalBreakBuy_Login,
- CustomAlertViewType_RemindTime,
- }CustomAlertViewType;
- @protocol CustomAlertViewDelegate;
- @interface CustomAlertView : UIView<UITextFieldDelegate>
- {
- CustomAlertViewType _alertViewType;
- id<CustomAlertViewDelegate> _customDelegate;
- UILabel* titleLabel;
- UILabel* contentLabel;
- UIButton* leftBtn;
- UIButton* rightBtn;
- UIButton* centerBtn;
- UIActivityIndicatorView *indicatorView;
- UITextField* textField;
- UIView* _alertView;
- UIView* _bgView;
- }
- @property (nonatomic,assign) id<CustomAlertViewDelegate> customDelegate;
- @property (nonatomic,retain) UILabel* contentLabel;
- @property (nonatomic,assign) UITextField* textField;
- //含有title,提示内容以及两个button.
- - (id)initWithTitle:(NSString*)title msg:(NSString*)msg rightBtnTitle:(NSString*)rightTitle leftBtnTitle:(NSString*)leftTitle delegate:(id<CustomAlertViewDelegate>) _delegate;
- - (id)initWithTitle:(NSString*)title msg:(NSString*)msg rightBtnTitle:(NSString*)rightTitle leftBtnTitle:(NSString*)leftTitle delegate:(id<CustomAlertViewDelegate>) _delegate msgFontSize:(CGFloat)fontSize;
- //含有title,提示内容以及一个button.
- - (id)initWithTitle:(NSString*)title msg:(NSString*)msg centerBtnTitle:(NSString*)centerTitle;
- //含有title,UIActivityIndicatorView控件,提示内容以及一个button.
- - (id)initProgressAlertViewWithTitle:(NSString*)title msg:(NSString*)msg centerBtnTitle:(NSString*)centerTitle delegate:(id<CustomAlertViewDelegate>) _delegate;
- //含有title,textfield,提示内容以及两个button.
- - (id)initTextFieldWithTitle:(NSString*)title msg:(NSString*)msg rightBtnTitle:(NSString*)rightTitle leftBtnTitle:(NSString*)leftTitle delegate:(id<CustomAlertViewDelegate>) _delegate;
- //含title,两个button,密码输入textfield,用户名等提示信息
- -(id)initLoginWithDelegate:(id<CustomAlertViewDelegate>)delegate userId:(NSString*)userid title:(NSString*)strTitle rightBtnTitle:(NSString*)strRbt;
- - (id)initRemindAlert;
- -(void) show;
- - (void) hideAlertView;
- -(void) setTitle:(NSString*) title;
- @end
- @protocol CustomAlertViewDelegate <NSObject>
- @optional
- - (void) leftBtnPressedWithinalertView:(CustomAlertView*)alert;
- - (void) rightBtnPressedWithinalertView:(CustomAlertView*)alert;
- - (void) centerBtnPressedWithinalertView:(CustomAlertView*)alert;
- @end
.m文件
- #import "CustomAlertView.h"
- #import "UIScreen+Frame.h"
- #import "CustomAlertView.h"
- #define MAX_CATEGORY_NAME_LENGTH 9
- #define kTagViewTextFieldJalBreakPassW (1001)
- @implementation CustomAlertView
- @synthesize customDelegate = _customDelegate;
- @synthesize contentLabel;
- @synthesize textField;
- //含有title,提示内容以及两个button.
- - (id)initWithTitle:(NSString*)title msg:(NSString*)msg rightBtnTitle:(NSString*)rightTitle leftBtnTitle:(NSString*)leftTitle delegate:(id<CustomAlertViewDelegate>) _delegate
- {
- if ((self = [super initWithFrame:[[UIScreen mainScreen] LandscapeBounds]]))
- {
- // Initialization code
- _alertViewType=CustomAlertViewType_Msg_TwoBtn;
- self.customDelegate=_delegate;
- [self setBackgroundColor:[UIColor clearColor]];
- _bgView = [[UIView alloc] initWithFrame:self.frame];
- [_bgView setBackgroundColor:[UIColor blackColor]];
- [self addSubview:_bgView];
- [_bgView release];
- CGRect alertRect = [self getAlertBounds];
- _alertView = [[UIView alloc] initWithFrame:alertRect];
- UIImageView *alertBg = [[UIImageView alloc] initWithFrame:CGRectMake(, , alertRect.size.width, alertRect.size.height)];
- alertBg.image = [UIImage imageNamed:@"AlertView_background.png"];
- [_alertView addSubview:alertBg];
- [alertBg release];
- titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(, , , )];
- titleLabel.textColor = [UIColor whiteColor];
- titleLabel.backgroundColor = [UIColor clearColor];
- titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
- titleLabel.text =title;
- titleLabel.textAlignment=UITextAlignmentCenter;
- [_alertView addSubview:titleLabel];
- [titleLabel release];
- contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
- contentLabel.textColor = [UIColor whiteColor];
- contentLabel.backgroundColor = [UIColor clearColor];
- contentLabel.font = [UIFont systemFontOfSize:15.0];
- contentLabel.text =msg;
- contentLabel.textAlignment=UITextAlignmentCenter;
- contentLabel.lineBreakMode = UILineBreakModeWordWrap;
- contentLabel.numberOfLines = ;
- [_alertView addSubview:contentLabel];
- [contentLabel release];
- UIImage* unselectedImg=[UIImage imageNamed:@"button_unselected.png"];
- UIImage* selectedImg=[UIImage imageNamed:@"button_selected.png"];
- rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
- [rightBtn setBackgroundImage:selectedImg forState:UIControlStateNormal];
- [rightBtn setTitle:rightTitle forState:UIControlStateNormal];
- rightBtn.frame=CGRectMake(, , selectedImg.size.width, selectedImg.size.height);
- [_alertView addSubview:rightBtn];
- [rightBtn addTarget:self action:@selector(rightBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
- leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
- [leftBtn setBackgroundImage:unselectedImg forState:UIControlStateNormal];
- [leftBtn setTitle:leftTitle forState:UIControlStateNormal];
- [leftBtn addTarget:self action:@selector(leftBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
- leftBtn.frame=CGRectMake(, , unselectedImg.size.width, unselectedImg.size.height);
- [_alertView addSubview:leftBtn];
- [self addSubview:_alertView];
- [_alertView release];
- [self showBackground];
- [self showAlertAnmation];
- }
- return self;
- }
- //可修改字体
- - (id)initWithTitle:(NSString*)title
- msg:(NSString*)msg
- rightBtnTitle:(NSString*)rightTitle
- leftBtnTitle:(NSString*)leftTitle
- delegate:(id<CustomAlertViewDelegate>) _delegate
- msgFontSize:(CGFloat)fontSize
- {
- if ((self = [super initWithFrame:[[UIScreen mainScreen] LandscapeBounds]]))
- {
- // Initialization code
- _alertViewType=CustomAlertViewType_Msg_TwoBtn;
- self.customDelegate=_delegate;
- [self setBackgroundColor:[UIColor clearColor]];
- _bgView = [[UIView alloc] initWithFrame:self.frame];
- [_bgView setBackgroundColor:[UIColor blackColor]];
- [self addSubview:_bgView];
- [_bgView release];
- CGRect alertRect = [self getAlertBounds];
- _alertView = [[UIView alloc] initWithFrame:alertRect];
- UIImageView *alertBg = [[UIImageView alloc] initWithFrame:CGRectMake(, , alertRect.size.width, alertRect.size.height)];
- alertBg.image = [UIImage imageNamed:@"AlertView_background.png"];
- [_alertView addSubview:alertBg];
- [alertBg release];
- titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(, , , )];
- titleLabel.textColor = [UIColor whiteColor];
- titleLabel.backgroundColor = [UIColor clearColor];
- titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
- titleLabel.text =title;
- titleLabel.textAlignment=UITextAlignmentCenter;
- [_alertView addSubview:titleLabel];
- [titleLabel release];
- contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
- contentLabel.textColor = [UIColor whiteColor];
- contentLabel.backgroundColor = [UIColor clearColor];
- contentLabel.font = [UIFont systemFontOfSize:fontSize];
- contentLabel.text =msg;
- contentLabel.textAlignment=UITextAlignmentCenter;
- contentLabel.lineBreakMode = UILineBreakModeWordWrap;
- contentLabel.numberOfLines = ;
- [_alertView addSubview:contentLabel];
- [contentLabel release];
- UIImage* unselectedImg=[UIImage imageNamed:@"button_unselected.png"];
- UIImage* selectedImg=[UIImage imageNamed:@"button_selected.png"];
- rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
- [rightBtn setBackgroundImage:selectedImg forState:UIControlStateNormal];
- [rightBtn setTitle:rightTitle forState:UIControlStateNormal];
- rightBtn.frame=CGRectMake(, , selectedImg.size.width, selectedImg.size.height);
- [_alertView addSubview:rightBtn];
- [rightBtn addTarget:self action:@selector(rightBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
- leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
- [leftBtn setBackgroundImage:unselectedImg forState:UIControlStateNormal];
- [leftBtn setTitle:leftTitle forState:UIControlStateNormal];
- [leftBtn addTarget:self action:@selector(leftBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
- leftBtn.frame=CGRectMake(, , unselectedImg.size.width, unselectedImg.size.height);
- [_alertView addSubview:leftBtn];
- [self addSubview:_alertView];
- [_alertView release];
- [self showBackground];
- [self showAlertAnmation];
- }
- return self;
- }
- - (id)initWithTitle:(NSString*)title msg:(NSString*)msg centerBtnTitle:(NSString*)centerTitle
- {
- self = [super initWithFrame:[[UIScreen mainScreen] LandscapeBounds]];
- if(self)
- {
- _alertViewType=CustomAlertViewType_Msg_OneBtn;
- [self setBackgroundColor:[UIColor clearColor]];
- _bgView = [[UIView alloc] initWithFrame:self.frame];
- [_bgView setBackgroundColor:[UIColor blackColor]];
- [self addSubview:_bgView];
- [_bgView release];
- CGRect alertRect = [self getAlertBounds];
- _alertView = [[UIView alloc] initWithFrame:alertRect];
- UIImageView *alertBg = [[UIImageView alloc] initWithFrame:CGRectMake(, , alertRect.size.width, alertRect.size.height)];
- alertBg.image = [UIImage imageNamed:@"AlertView_background.png"];
- [_alertView addSubview:alertBg];
- [alertBg release];
- titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(, , , )];
- titleLabel.textColor = [UIColor whiteColor];
- titleLabel.backgroundColor = [UIColor clearColor];
- titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
- titleLabel.text =title;
- titleLabel.textAlignment=UITextAlignmentCenter;
- [_alertView addSubview:titleLabel];
- [titleLabel release];
- contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
- contentLabel.textColor = [UIColor whiteColor];
- contentLabel.backgroundColor = [UIColor clearColor];
- contentLabel.font = [UIFont systemFontOfSize:15.0];
- contentLabel.text =msg;
- contentLabel.textAlignment=UITextAlignmentCenter;
- [_alertView addSubview:contentLabel];
- [contentLabel release];
- UIImage* selectedImg=[UIImage imageNamed:@"bigbuttonbkimg.png"];
- centerBtn=[UIButton buttonWithType:UIButtonTypeCustom];
- [centerBtn setBackgroundImage:selectedImg forState:UIControlStateNormal];
- [centerBtn setTitle:centerTitle forState:UIControlStateNormal];
- centerBtn.frame=CGRectMake(, , , );
- [_alertView addSubview:centerBtn];
- [centerBtn addTarget:self action:@selector(centerBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:_alertView];
- [_alertView release];
- [self showBackground];
- [self showAlertAnmation];
- }
- return self;
- }
- //含有title,UIActivityIndicatorView控件,提示内容以及一个button.
- - (id)initProgressAlertViewWithTitle:(NSString*)title msg:(NSString*)msg centerBtnTitle:(NSString*)centerTitle delegate:(id<CustomAlertViewDelegate>) _delegate
- {
- if ((self = [super initWithFrame:[[UIScreen mainScreen] LandscapeBounds]]))
- {
- // Initialization code
- _alertViewType=CustomAlertViewType_ActivityIndiAndMsg_OneBtn;
- self.customDelegate=_delegate;
- [self setBackgroundColor:[UIColor clearColor]];
- _bgView = [[UIView alloc] initWithFrame:self.frame];
- [_bgView setBackgroundColor:[UIColor blackColor]];
- [self addSubview:_bgView];
- [_bgView release];
- CGRect alertRect = [self getAlertBounds];
- _alertView = [[UIView alloc] initWithFrame:alertRect];
- UIImageView *alertBg = [[UIImageView alloc] initWithFrame:CGRectMake(, , alertRect.size.width, alertRect.size.height)];
- alertBg.image = [UIImage imageNamed:@"AlertView_background.png"];
- [_alertView addSubview:alertBg];
- [alertBg release];
- titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(, , , )];
- titleLabel.textColor = [UIColor whiteColor];
- titleLabel.backgroundColor = [UIColor clearColor];
- titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
- titleLabel.text =title;
- titleLabel.textAlignment=UITextAlignmentCenter;
- [_alertView addSubview:titleLabel];
- [titleLabel release];
- indicatorView= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(80.0, 45.0, 30.0, 30.0)];
- indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
- indicatorView.hidesWhenStopped=NO;
- [_alertView addSubview:indicatorView];
- [indicatorView release];
- [indicatorView startAnimating];
- contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(120.0, 50.0, 150.0, 20.0)];
- contentLabel.textColor = [UIColor whiteColor];
- contentLabel.backgroundColor = [UIColor clearColor];
- contentLabel.font = [UIFont boldSystemFontOfSize:15.0];
- contentLabel.text =msg;
- contentLabel.textAlignment=UITextAlignmentLeft;
- [_alertView addSubview:contentLabel];
- [contentLabel release];
- UIImage* selectedImg=[UIImage imageNamed:@"button_selected.png"];
- centerBtn=[UIButton buttonWithType:UIButtonTypeCustom];
- [centerBtn setBackgroundImage:selectedImg forState:UIControlStateNormal];
- [centerBtn setTitle:centerTitle forState:UIControlStateNormal];
- centerBtn.frame=CGRectMake(, , , );
- [_alertView addSubview:centerBtn];
- [centerBtn addTarget:self action:@selector(centerBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:_alertView];
- [_alertView release];
- [self showBackground];
- [self showAlertAnmation];
- }
- return self;
- }
- //含有title,textfield,提示内容以及两个button.
- - (id)initTextFieldWithTitle:(NSString*)title msg:(NSString*)msg rightBtnTitle:(NSString*)rightTitle leftBtnTitle:(NSString*)leftTitle delegate:(id<CustomAlertViewDelegate>) _delegate
- {
- if ((self = [super initWithFrame:[[UIScreen mainScreen] LandscapeBounds]]))
- {
- // Initialization code
- _alertViewType=CustomAlertViewType_Msg_TextField_TwoBtn;
- self.customDelegate=_delegate;
- [self setBackgroundColor:[UIColor clearColor]];
- _bgView = [[UIView alloc] initWithFrame:self.frame];
- [_bgView setBackgroundColor:[UIColor blackColor]];
- [self addSubview:_bgView];
- [_bgView release];
- CGRect alertRect = [self getAlertBounds];
- _alertView = [[UIView alloc] initWithFrame:alertRect];
- UIImageView *alertBg = [[UIImageView alloc] initWithFrame:CGRectMake(, , alertRect.size.width, alertRect.size.height)];
- alertBg.image = [UIImage imageNamed:@"AlertView_background.png"];
- [_alertView addSubview:alertBg];
- [alertBg release];
- titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(, , , )];
- titleLabel.textColor = [UIColor whiteColor];
- titleLabel.backgroundColor = [UIColor clearColor];
- titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
- titleLabel.text =title;
- titleLabel.textAlignment=UITextAlignmentCenter;
- [_alertView addSubview:titleLabel];
- [titleLabel release];
- contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 33.0, 300.0, 12.0)];
- contentLabel.textColor = [UIColor clearColor];
- contentLabel.backgroundColor = [UIColor clearColor];
- contentLabel.font = [UIFont boldSystemFontOfSize:8.0];
- contentLabel.textAlignment=UITextAlignmentCenter;
- [_alertView addSubview:contentLabel];
- [contentLabel release];
- textField = [[[UITextField alloc] initWithFrame:CGRectMake(, , , )] autorelease];
- textField.borderStyle = UITextBorderStyleRoundedRect;
- textField.clearButtonMode = UITextFieldViewModeWhileEditing;
- textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
- textField.placeholder = msg;
- [textField addTarget:self action:@selector(textFieldChanged) forControlEvents:UIControlEventEditingChanged];
- [_alertView addSubview:textField];
- [textField release];
- UIImage* unselectedImg=[UIImage imageNamed:@"button_unselected.png"];
- UIImage* selectedImg=[UIImage imageNamed:@"button_selected.png"];
- rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
- [rightBtn setBackgroundImage:selectedImg forState:UIControlStateNormal];
- [rightBtn setTitle:rightTitle forState:UIControlStateNormal];
- rightBtn.frame=CGRectMake(, , selectedImg.size.width, selectedImg.size.height);
- [_alertView addSubview:rightBtn];
- [rightBtn addTarget:self action:@selector(rightBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
- leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
- [leftBtn setBackgroundImage:unselectedImg forState:UIControlStateNormal];
- [leftBtn setTitle:leftTitle forState:UIControlStateNormal];
- [leftBtn addTarget:self action:@selector(leftBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
- leftBtn.frame=CGRectMake(, , unselectedImg.size.width, unselectedImg.size.height);
- [_alertView addSubview:leftBtn];
- [self addSubview:_alertView];
- [_alertView release];
- [self showBackground];
- [self showAlertAnmation];
- }
- return self;
- }
- -(id)initLoginWithDelegate:(id<CustomAlertViewDelegate>)delegate userId:(NSString*)userid title:(NSString*)strTitle rightBtnTitle:(NSString*)strRbt
- {
- if ((self = [super initWithFrame:[[UIScreen mainScreen] LandscapeBounds]]))
- {
- _alertViewType = CustomAlertViewType_JalBreakBuy_Login;
- self.customDelegate = delegate;
- [self setBackgroundColor:[UIColor clearColor]];
- _bgView = [[UIView alloc] initWithFrame:self.frame];
- [_bgView setBackgroundColor:[UIColor blackColor]];
- [self addSubview:_bgView];
- [_bgView release];
- CGRect alertRect = [self getAlertBounds];
- _alertView = [[UIView alloc] initWithFrame:alertRect];
- UIImageView *alertBg = [[UIImageView alloc] initWithFrame:CGRectMake(, , alertRect.size.width, alertRect.size.height)];
- alertBg.image = [UIImage imageNamed:@"AlertView_background.png"];
- [_alertView addSubview:alertBg];
- [alertBg release];
- titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(, , , )];
- titleLabel.textColor = [UIColor whiteColor];
- titleLabel.backgroundColor = [UIColor clearColor];
- titleLabel.font = [UIFont boldSystemFontOfSize:17.0];
- titleLabel.text = strTitle;
- titleLabel.textAlignment=UITextAlignmentCenter;
- [_alertView addSubview:titleLabel];
- [titleLabel release];
- CGFloat xLabel1 = ;
- CGFloat xLabel2 = ;
- CGFloat yLevel1 = ;
- CGFloat yLevel2 = ;
- UILabel* label = nil;
- label = [[UILabel alloc]initWithFrame:CGRectMake(xLabel1, yLevel1, , )];
- label.backgroundColor = [UIColor clearColor];
- label.text = @"账号:";
- label.textColor = [UIColor whiteColor];
- label.font = [UIFont boldSystemFontOfSize:17.0];
- label.textAlignment = UITextAlignmentCenter;
- [_alertView addSubview:label];
- [label release];
- label = [[UILabel alloc]initWithFrame:CGRectMake(xLabel2, yLevel1,, )];
- label.backgroundColor = [UIColor clearColor];
- label.text = userid;
- label.textColor = [UIColor whiteColor];
- label.font = [UIFont boldSystemFontOfSize:17.0];
- label.textAlignment = UITextAlignmentLeft;
- [_alertView addSubview:label];
- [label release];
- label = [[UILabel alloc]initWithFrame:CGRectMake(xLabel1, yLevel2, , )];
- label.backgroundColor = [UIColor clearColor];
- label.text = @"密码:";
- label.textColor = [UIColor whiteColor];
- label.font = [UIFont boldSystemFontOfSize:17.0];
- label.textAlignment = UITextAlignmentCenter;
- [_alertView addSubview:label];
- [label release];
- textField = [[[UITextField alloc]initWithFrame:CGRectMake(xLabel2, yLevel2, , )] autorelease];
- textField.delegate = self;
- textField.textColor = kColorLoginInput;
- textField.tag= kTagViewTextFieldJalBreakPassW;
- textField.borderStyle = UITextBorderStyleRoundedRect;
- textField.secureTextEntry = YES;
- textField.returnKeyType = UIReturnKeyDone;
- textField.autocorrectionType = UITextAutocorrectionTypeNo;
- textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
- textField.font = [UIFont systemFontOfSize:];
- textField.clearButtonMode = UITextFieldViewModeWhileEditing;
- textField.leftViewMode = UITextFieldViewModeAlways;
- textField.keyboardType = UIKeyboardTypeASCIICapable ;
- [_alertView addSubview:textField];
- [textField release];
- UIImage* unselectedImg=[UIImage imageNamed:@"button_unselected.png"];
- UIImage* selectedImg=[UIImage imageNamed:@"button_selected.png"];
- rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
- [rightBtn setBackgroundImage:selectedImg forState:UIControlStateNormal];
- [rightBtn setTitle:strRbt forState:UIControlStateNormal];
- rightBtn.frame=CGRectMake(, , selectedImg.size.width, selectedImg.size.height);
- [_alertView addSubview:rightBtn];
- [rightBtn addTarget:self action:@selector(rightBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
- leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
- [leftBtn setBackgroundImage:unselectedImg forState:UIControlStateNormal];
- [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
- [leftBtn addTarget:self action:@selector(leftBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
- leftBtn.frame=CGRectMake(, , unselectedImg.size.width, unselectedImg.size.height);
- [_alertView addSubview:leftBtn];
- [self addSubview:_alertView];
- [_alertView release];
- [self showBackground];
- [self showAlertAnmation];
- }
- return self;
- }
- -(void) show
- {
- UIWindow* window = [[UIApplication sharedApplication] keyWindow];
- NSArray* windowViews = [window subviews];
- if(windowViews && [windowViews count]>){
- UIView* subView = [windowViews objectAtIndex:[windowViews count]-];
- for(UIView* aSubView in subView.subviews)
- {
- [aSubView.layer removeAllAnimations];
- }
- [subView addSubview:self];
- }
- }
- - (void)showBackground
- {
- _bgView.alpha = ;
- [UIView beginAnimations:@"fadeIn" context:nil];
- [UIView setAnimationDuration:0.35];
- _bgView.alpha = 0.6;
- [UIView commitAnimations];
- }
- -(void) showAlertAnmation
- {
- CAKeyframeAnimation * animation;
- animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
- animation.duration = 0.30;
- animation.removedOnCompletion = YES;
- animation.fillMode = kCAFillModeForwards;
- NSMutableArray *values = [NSMutableArray array];
- [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
- [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)]];
- [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
- animation.values = values;
- [_alertView.layer addAnimation:animation forKey:nil];
- }
- -(void) hideAlertAnmation
- {
- [UIView beginAnimations:@"fadeIn" context:nil];
- [UIView setAnimationDuration:0.35];
- _bgView.alpha = 0.0;
- [UIView commitAnimations];
- }
- -(CGRect)getAlertBounds
- {
- CGRect retRect;
- if (_alertViewType == CustomAlertViewType_JalBreakBuy_Login)
- {
- retRect= CGRectMake((self.frame.size.width-)/, (self.frame.size.height-)/, , );
- }
- else
- {
- UIImage* image=[UIImage imageNamed:@"AlertView_background.png"];
- CGSize imageSize = image.size;
- retRect= CGRectMake((self.frame.size.width-imageSize.width)/, (self.frame.size.height-imageSize.height)/, imageSize.width, imageSize.height);
- }
- return retRect;
- }
- - (void) hideAlertView
- {
- _alertView.hidden = YES;
- [self hideAlertAnmation];
- [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.2];
- }
- -(void) removeFromSuperview
- {
- [super removeFromSuperview];
- }
- - (void) leftBtnPressed:(id)sender
- {
- if (_customDelegate && [_customDelegate respondsToSelector:@selector(leftBtnPressedWithinalertView:)])
- {
- [_customDelegate leftBtnPressedWithinalertView:self];
- }
- else
- {
- [self hideAlertView];
- }
- }
- - (void) rightBtnPressed:(id)sender
- {
- if (_customDelegate && [_customDelegate respondsToSelector:@selector(rightBtnPressedWithinalertView:)])
- {
- [_customDelegate rightBtnPressedWithinalertView:self];
- }
- else
- {
- [self hideAlertView];
- }
- }
- - (void) centerBtnPressed:(id)sender
- {
- if (_customDelegate && [_customDelegate respondsToSelector:@selector(centerBtnPressedWithinalertView:)])
- {
- [_customDelegate centerBtnPressedWithinalertView:self];
- }
- else
- {
- [self hideAlertView];
- }
- }
- -(void) setTitle:(NSString*) title
- {
- titleLabel.text = title;
- }
- -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- {
- [self endEditing:YES];
- }
- -(void) textFieldChanged
- {
- if ([textField.text length] > MAX_CATEGORY_NAME_LENGTH)
- {
- textField.text = [textField.text substringToIndex:MAX_CATEGORY_NAME_LENGTH];
- }
- }
- #pragma mark - DelegateTextField
- - (BOOL)textFieldShouldReturn:(UITextField *)_textField
- {
- if (_textField.tag == kTagViewTextFieldJalBreakPassW)
- {
- [self rightBtnPressed:nil];
- return NO;
- }
- return YES;
- }
- - (BOOL)textField:(UITextField *)textField_ shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
- {
- if (textField_.tag == kTagViewTextFieldJalBreakPassW)
- {
- if (string && [string length] && [textField_.text length]>)
- {
- return NO;
- }
- }
- return YES;
- }
- @end
调用:
- CustomAlertView* alert = [[CustomAlertView alloc]initWithTitle:nil
- msg:@"测试alertview"
- centerBtnTitle:@"确定"];
- [alert show];
- [alert release];
兼容iOs7的自定义alertView的更多相关文章
- iOS 读取相册二维码,兼容ios7(使用CIDetector 和 ZXingObjC)
ios从相册读取二维码,在ios8以上,苹果提供了自带的识别图片二维码的功能,这种方式效率最好,也是最推荐的,但是如果你的系统需要向下兼容ios7,就必须用其他方式. 这里我选择的是 ZXingObj ...
- iOS 第三方自定义Alertview项目MBProcessHud中的重要代码分析
做ios,弹出一个自定义的alertview挺常见的.ios7以前,我们可以对系统的UIAlertView进行一点操作,实现一点简单的定制,但是ios7不再允许我们这样做了.因此,我们需要自己创建一个 ...
- Swift3.0生成二维码、扫描二维码、相册读取二维码,兼容iOS7(结合ZXingObjC)
二维码生成 //MARK: 传进去字符串,生成二维码图片(>=iOS7) text:要生成的二维码内容 WH:二维码高宽 private func creatQRCodeImage(text: ...
- 自定义AlertView的方法和改变Alert的弹出位置以及其宽度
此方法在IOS7中不适合 一.自定义AlertView 1.首先新建一个OC类继承与AlertView. 2.然后再.m中添加方法 - (void)layoutSubviews 可以再这个方法里边改变 ...
- Swift自定义AlertView
今天项目加新需求,添加积分过期提醒功能: 第一反应就用系统的UIAlertViewController,但是message中积分是需要红色显示. // let str = "尊敬的顾客,您有 ...
- [iOS开发]Xcode8兼容iOS7以及低版本Xcode调试高版本iOS系统
现在的项目一般都要兼容iOS7系统,同时也要兼容iOS10,在Xcode8上面,默认情况下无法调试iOS7,因为缺乏调试iOS7需要的配置文件.同时在低版本的Xcode上面(8以下),也无法调试iOS ...
- Xcode8兼容iOS7以及低版本Xcode调试高版本iOS系统
我们使用Xcode8新建的工程,默认支持的最低系统是iOS8,我们可以手动更改版本到7.0,但是不支持真机调试. 现在的项目一般都要兼容iOS7系统,同时也要兼容iOS10,在Xcode8上面,默认情 ...
- iOS开发——自定义AlertView
自定义的AlertView,可以选择出现的动画方式,正文信息高度自动变化,特意做了几个可以对比.没啥难点,直接上代码,一看就懂. 1.在YYTAlertView.h文件中 // // YYTAler ...
- iOS7新特性-完美解决iOS7关于自定义导航条UIBarButtonItem偏移的问题
前言: 本文由DevDiv社区@Vincent 原创,转载请注明出处! http://www.devdiv.com/iOS_iPhone-ios_ios_uibarbuttonitem_-thread ...
随机推荐
- JVM 内存调优 与 实际案例
堆内存设置 原理 JVM堆内存分为2块:Permanent Space 和 Heap Space. Permanent 即 持久代(Permanent Generation),主要存放的是Java类定 ...
- Python学习札记(十九) 高级特性5 迭代器
参考:迭代器 Note 1.可用于for循环的对象有两类:(1)集合数据类型:list tuple dict str set (2)Generator:生成器和含yield语句的函数.这些可以直接作用 ...
- gTest&gMock learning
在C++中,编写服务后的一种测试方式是使用google的gTest和gMock结合 之前写py,测试方式是将服务挂起,使用工具模拟请求发包,check resp,这样的缺点在于不方便,即使存下了所有的 ...
- [PostgreSql]PostgreSql创建函数及函数的执行
1.准备好创建函数的脚本 -- FUNCTION: public.dynamic_placelist_select(integer, timestamp without time zone) -- D ...
- Les13 性能管理
目标 使用Oracle Enterprise Manager监视性能 使用自动内存管理(AMM) 使用内存指导调整内存缓冲区的大小 查看与性能相关的动态视图 排除无效和不可用对象产生的故障 性能监视 ...
- 《OpenCL编程指南》之 与Direct3D互操作
介绍OpenCL与D3D 10之间的互操作. 1.初始化OpenCL上下文实现Direct3D互操作 OpenCL共享由pragma cl_khr_d3d10_sharing启用: #pragma O ...
- 【zznu-2060】 Minsum Plus(最小正子段和)
题目描述 题意简单到令人发指! 序列A由N个整数组成,从中选出一个连续的子序列,使得这个子序列的和为正数,且和为所有和大于零的子序列中的最小值. 将这个值输出,若无解,输出no solution. 输 ...
- hdu1151
题解: 二分图边覆盖 n-最大匹配 代码: #include<cstdio> #include<cmath> #include<algorithm> #includ ...
- jqeury 基础
jquery 选择器: 基本选择器:#id ..class.*(匹配所有) 层次选择器: $(div span) 选取<div>里的所有的<span>元素. $(div> ...
- Hadoop学习资料整理
1.hadoop相关 hadoop 0.18文档(详细介绍Hadoop,MapReduce,FS Shell,Streaming等) hadoop资料汇总 2.实习的时候用的是streaming,非j ...