HardBirch

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

时间:12-05-09 栏目:iOS游戏开发技术 作者:张飞不张,文采横飞 评论:6 点击: 4,203 次

 

      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

【IOS】自定义UIAlertView样式,实现可替换背景和按钮:目前有6 条留言

  1. 6楼
    dongge_111:

    丁丁定的

    2012-05-10 10:01 [回复]
  2. 5楼
    ComeOnTom:

    mark >….

    2012-05-10 15:04 [回复]
  3. 4楼
    yaoohfox:

    好文章。

    2012-05-12 00:11 [回复]
  4. 地板
    weiqubo:

    2012-07-02 21:48 [回复]
  5. 板凳
    BIAOBIAOqi:

    多谢分享!
    博主很用心。建议博主开源代码到github上~

    2012-07-30 19:17 [回复]
  6. 沙发
    yjh4866:

    呼呼,全是积分下载啊

    2012-08-24 11:21 [回复]

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐