本文主要是介绍IOS】自定义UIAlertView样式,实现可替换背景和按钮 此博文包含图片此博文包含视频 (2012-10-24 10:23:25),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
UIAlertView 是一个十分常用的控件,网上也有好多类似的自定义AlertView的方法。但是感觉效果都不是很好,它们有的是在系统自带的上面添加文本框,也有的是完全自己用UIView来实现,还有的就是继承了UIAlertView 。
最终的效果图:
- //
- //
JKCustomAlert.m - //
AlertTest - //
- //
Created by on 12-5-9. - //
Copyright (c) 2012年 __MyCompanyName__. All rights reserved. - //
-
- #import
- @protocol
JKCustomAlertDelegate - @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]== class]){[UIImageView -
[v setHidden:YES]; -
} -
-
-
if ([v class]]isKindOfClass:[UIButton || -
[v isKindOfClass:NSClassFromString(@"UIThreePartButton")]) { -
[v setHidden:YES]; -
} -
} -
-
for ( inti=0;i<[_buttonArrays count]; i++) { -
UIButton *btn = [_buttonArrays objectAtIndex:i]; -
btn.tag = i; -
[self addSubview:btn]; -
[btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpIns ide]; -
} -
-
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 dismissWithClickedButton Index: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
这篇关于IOS】自定义UIAlertView样式,实现可替换背景和按钮 此博文包含图片此博文包含视频 (2012-10-24 10:23:25)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!