ios ios7 UIAlertView自定义

针对iOS7中原生UIAlertView不支持自定义的问题,本文介绍了一种利用第三方库CustomIOS7AlertView实现自定义UIAlertView的方法。该库允许添加自定义视图、按钮点击事件等,并提供了两种响应方式:委托代理及Block。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前一个项目适配ios7之后,发现原先的UIAlertView无法正常显示。

后来发现ios7里面原生态的UIAlertView不支持自定义了。

然后就去github上找了下。发现了一个不错的第三方库。和我们之前的使用习惯差不多。

mark一下。


下面给个简单的示例。


1.导入文件。


将这两个文件加入我们的工程下。


2.添加头文件

在需要使用UIAlertView的地方,添加头文件。

#import "CustomIOS7AlertView.h"

并且添加协议。

<CustomIOS7AlertViewDelegate>

比如我的.h文件

#import <UIKit/UIKit.h>  
#import "CustomIOS7AlertView.h"  
  
@interface ViewController : UIViewController<CustomIOS7AlertViewDelegate>  
  
@end  

3.添加UIAlertView

先给出.m文件。

#import "ViewController.h"  
  
@interface ViewController ()  
  
@end  
  
@implementation ViewController  
  
- (void)viewDidLoad  
{  
    [super viewDidLoad];  
  
    // Just a subtle background color  
    [self.view setBackgroundColor:[UIColor colorWithRed:0.8f green:0.8f blue:0.8f alpha:1.0f]];  
  
    // A simple button to launch the demo  
    UIButton *launchDialog = [UIButton buttonWithType:UIButtonTypeCustom];  
    [launchDialog setFrame:CGRectMake(10, 30, self.view.bounds.size.width-20, 50)];  
    [launchDialog addTarget:self action:@selector(launchDialog:) forControlEvents:UIControlEventTouchDown];  
    [launchDialog setTitle:@"Launch Dialog" forState:UIControlStateNormal];  
    [launchDialog setBackgroundColor:[UIColor whiteColor]];  
    [launchDialog setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];  
    [launchDialog.layer setBorderWidth:0];  
    [launchDialog.layer setCornerRadius:5];  
    [self.view addSubview:launchDialog];  
}  
  
- (IBAction)launchDialog:(id)sender  
{  
    // Here we need to pass a full frame  
    CustomIOS7AlertView *alertView = [[CustomIOS7AlertView alloc] init];  
  
    // Add some custom content to the alert view  
    [alertView setContainerView:[self createDemoView]];  
  
    // Modify the parameters  
    [alertView setButtonTitles:[NSMutableArray arrayWithObjects:@"Close1", @"Close2", @"Close3", nil]];  
    [alertView setDelegate:self];  
      
    // You may use a Block, rather than a delegate.  
    [alertView setOnButtonTouchUpInside:^(CustomIOS7AlertView *alertView, int buttonIndex) {  
        NSLog(@"Block: Button at position %d is clicked on alertView %d.", buttonIndex, [alertView tag]);  
        [alertView close];  
    }];  
      
    [alertView setUseMotionEffects:true];  
  
    // And launch the dialog  
    [alertView show];  
}  
  
- (void)customIOS7dialogButtonTouchUpInside: (CustomIOS7AlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex  
{  
    NSLog(@"Delegate: Button at position %d is clicked on alertView %d.", buttonIndex, [alertView tag]);  
    [alertView close];  
}  
  
- (UIView *)createDemoView  
{  
    UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 290, 200)];  
  
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 270, 180)];  
    [imageView setImage:[UIImage imageNamed:@"demo"]];  
    [demoView addSubview:imageView];  
  
    return demoView;  
}  
  
@end 

这里,通过一个button绑定弹出UIAlertView。  当然,你也可以在其他条件触发launchDialog方法。

然后UIAlertView载入自己定义的视图(createDemoView)。

另外,块内的setOnButtonTouchUpInside 和 单独的delegate   customIOS7dialogButtonTouchUpInside方法都能响应我们的选择。

具体选择哪种方式因人而异。


下面给出例子程序下载链接:http://download.youkuaiyun.com/detail/hitwhylz/6857765


转载自:http://blog.youkuaiyun.com/hitwhylz/article/details/18402515

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值