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 ( copy ) void (^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,如需转载请自行联系原作者