开发者社区> 文艺小青年> 正文

iOS开发-自定义UIAlterView(iOS 7)

简介:
+关注继续查看

App中不可能少了弹框,弹框是交互的必要形式,使用起来也非常简单,不过最近需要自定义一个弹框,虽然iOS本身的弹框已经能满足大部分的需求,但是不可避免还是需要做一些自定义的工作。iOS7之前是可以自定义AlterView的,就是继承一下UIAlterView,然后初始化的时候通过addSubview添加自定义的View,但是iOS7之后这样做就不行了,不过还好有开源项目可以解决这个问题。

iOS默认弹框

viewDidLoad中添加两个按钮,代码如下:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
UIButton  *orignalBtn=[[UIButton alloc]initWithFrame:CGRectMake(100, 40, 100, 50)];
[orignalBtn setBackgroundColor:[UIColor greenColor]];
[orignalBtn setTitle:@"iOS弹框" forState:UIControlStateNormal];
[orignalBtn addTarget:self action:@selector(orignalShow) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:orignalBtn];
 
 
 
UIButton  *customlBtn=[[UIButton alloc]initWithFrame:CGRectMake(100, 140, 100, 50)];
[customlBtn setBackgroundColor:[UIColor redColor]];
[customlBtn setTitle:@"自定义弹框" forState:UIControlStateNormal];
[customlBtn addTarget:self action:@selector(customShow) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:customlBtn];

 

 响应默认弹框事件:

1
2
3
4
-(void)orignalShow{
    UIAlertView *alterView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"博客园-Fly_Elephant" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定"nil];
    [alterView show];
}

  效果如下:

 

自定义弹框

主要解决iOS7之后的系统无法自定义弹框的问题,使用开源项目,项目地址:https://github.com/wimagguc/ios-custom-alertview,其实就是自定义了一个类:

CustomIOSAlertView.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#import <UIKit/UIKit.h>
 
@protocol CustomIOSAlertViewDelegate
 
- (void)customIOS7dialogButtonTouchUpInside:(id)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
 
@end
 
@interface CustomIOSAlertView : UIView<CustomIOSAlertViewDelegate>
 
@property (nonatomic, retain) UIView *parentView;    // The parent view this 'dialog' is attached to
@property (nonatomic, retain) UIView *dialogView;    // Dialog's container view
@property (nonatomic, retain) UIView *containerView; // Container within the dialog (place your ui elements here)
 
@property (nonatomic, assign) id<CustomIOSAlertViewDelegate> delegate;
@property (nonatomic, retain) NSArray *buttonTitles;
@property (nonatomic, assign) BOOL useMotionEffects;
 
@property (copyvoid (^onButtonTouchUpInside)(CustomIOSAlertView *alertView, int buttonIndex) ;
 
- (id)init;
 
/*!
 DEPRECATED: Use the [CustomIOSAlertView init] method without passing a parent view.
 */
- (id)initWithParentView: (UIView *)_parentView __attribute__ ((deprecated));
 
- (void)show;
- (void)close;
 
- (IBAction)customIOS7dialogButtonTouchUpInside:(id)sender;
- (void)setOnButtonTouchUpInside:(void (^)(CustomIOSAlertView *alertView, int buttonIndex))onButtonTouchUpInside;
 
- (void)deviceOrientationDidChange: (NSNotification *)notification;
- (void)dealloc;
 
@end

CustomIOSAlertView.m

调用代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
-(void)customShow{
    CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
     
    [alertView setContainerView:[self customView]];
     
    [alertView setButtonTitles:[NSMutableArray arrayWithObjects:@"取消", @"确定"nil]];
    [alertView setDelegate:self];
     
    [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
        NSString *result=alertView.buttonTitles[buttonIndex];
        NSLog(@"点击了%@按钮",result);
        [alertView close];
    }];
     
    [alertView setUseMotionEffects:true];
    [alertView show];
 
}
 
- (UIView *)customView
{
    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 240, 160)];
     
    UILabel *tip=[[UILabel alloc]initWithFrame:CGRectMake(100, 10, 50, 30)];
    [tip setText:@"提示"];
    [customView addSubview:tip];
     
     
    UILabel *content=[[UILabel alloc]initWithFrame:CGRectMake(10, 60, 210, 30)];
    [content setText:@"http://www.cnblogs.com/xiaofeixiang"];
    [content setFont:[UIFont systemFontOfSize:12]];
    [customView addSubview:content];
    return customView;
}

 效果如下:

 本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4453819.html,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
WIN11自定义版本ios镜像下载教程
WIN11自定义版本ios镜像下载教程
75 0
iOS UISlider自定义渐变色滑杆
iOS UISlider自定义渐变色滑杆
13 0
iOS UILabel自定义位置
iOS UILabel自定义位置
27 0
weex-自定义module,实现weex在iOS的本地化,js之间互相跳转,交互,传值(iOS接入weex的最佳方式)
weex-自定义module,实现weex在iOS的本地化,js之间互相跳转,交互,传值(iOS接入weex的最佳方式)
68 0
iOS开发-banner滚动图自定义
iOS开发-banner滚动图自定义
56 0
iOS使用xib自定义uiview
iOS使用xib自定义uiview
83 0
iOS 端自定义开发(二)| 学习笔记
快速学习 iOS 端自定义开发。
86 0
iOS 端自定义开发(一)| 学习笔记
快速学习 iOS 端自定义开发。
81 0
iOS 端自定义开发(二)|学习笔记
快速学习iOS 端自定义开发(二)
80 0
iOS 端自定义开发(一)|学习笔记
快速学习iOS 端自定义开发(一)
56 0
+关注
文艺小青年
文章
问答
视频
文章排行榜
最热
最新
相关电子书
更多
Facebook iOS App技术演化十年之路
立即下载
From Java_Android to Swift iOS
立即下载
深入剖析 iOS 性能优化
立即下载