【IOS】自定义UIAlertView样式,实现可替换背景和按钮

【原创作品, 欢迎转载,转载请在明显处注明! 谢谢。

原文地址:http://blog.csdn.net/toss156/article/details/7552075


UIAlertView 是一个十分常用的控件,网上也有好多类似的自定义AlertView的方法。但是感觉效果都不是很好,它们有的是在系统自带的上面添加文本框,也有的是完全自己用UIView来实现,还有的就是继承了UIAlertView 。

今天给大家带来的这个UIAlertView ,它也是继承了UIAlertView,然后屏蔽了系统的背景图片,和 按钮,然后自己添加,事件响应,从而完成了样式的自定义,这样做的好处是保留了 UIAlertView的模态窗口。

最终的效果图:

// // JKCustomAlert.m // AlertTest // // Created by on 12-5-9. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import <UIKit/UIKit.h> @protocol JKCustomAlertDelegate <NSObject> @optional - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; @end @interface JKCustomAlert : UIAlertView { id JKdelegate; UIImage *backgroundImage; UIImage *contentImage; NSMutableArray *_buttonArrays; } @property(readwrite, retain) UIImage *backgroundImage; @property(readwrite, retain) UIImage *contentImage; @property(nonatomic, assign) id JKdelegate; - (id)initWithImage:(UIImage *)image contentImage:(UIImage *)content; -(void) addButtonWithUIButton:(UIButton *) btn; @end


// // // JKCustomAlert.m // AlertTest // // Created by on 12-5-9. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import "JKCustomAlert.h" @interface JKCustomAlert () @property(nonatomic, retain) NSMutableArray *_buttonArrays; @end @implementation JKCustomAlert @synthesize backgroundImage,contentImage,_buttonArrays,JKdelegate; - (id)initWithImage:(UIImage *)image contentImage:(UIImage *)content{ if (self == [super init]) { self.backgroundImage = image; self.contentImage = content; self._buttonArrays = [NSMutableArray arrayWithCapacity:4]; } return self; } -(void) addButtonWithUIButton:(UIButton *) btn { [_buttonArrays addObject:btn]; } - (void)drawRect:(CGRect)rect { CGSize imageSize = self.backgroundImage.size; [self.backgroundImage drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)]; } - (void) layoutSubviews { //屏蔽系统的ImageView 和 UIButton for (UIView *v in [self subviews]) { if ([v class] == [UIImageView class]){ [v setHidden:YES]; } if ([v isKindOfClass:[UIButton class]] || [v isKindOfClass:NSClassFromString(@"UIThreePartButton")]) { [v setHidden:YES]; } } for (int i=0;i<[_buttonArrays count]; i++) { UIButton *btn = [_buttonArrays objectAtIndex:i]; btn.tag = i; [self addSubview:btn]; [btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; } if (contentImage) { UIImageView *contentview = [[UIImageView alloc] initWithImage:self.contentImage]; contentview.frame = CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height); [self addSubview:contentview]; } } -(void) buttonClicked:(id)sender { UIButton *btn = (UIButton *) sender; if (JKdelegate) { if ([JKdelegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]) { [JKdelegate alertView:self clickedButtonAtIndex:btn.tag]; } } [self dismissWithClickedButtonIndex:0 animated:YES]; } - (void) show { [super show]; CGSize imageSize = self.backgroundImage.size; self.bounds = CGRectMake(0, 0, imageSize.width, imageSize.height); } - (void)dealloc { [_buttonArrays removeAllObjects]; [backgroundImage release]; if (contentImage) { [contentImage release]; contentImage = nil; } [super dealloc]; } @end


Demo下载地址:http://download.csdn.net/detail/toss156/4289966

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值