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

相关文章
|
26天前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
83 3
|
3月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
122 2
|
3月前
|
存储 数据建模 数据库
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
38 0
|
2月前
|
监控 API Swift
用Swift开发iOS平台上的上网行为管理监控软件
在当今数字化时代,随着智能手机的普及,人们对于网络的依赖日益增加。然而,对于一些特定场景,如家庭、学校或者企业,对于iOS设备上的网络行为进行管理和监控显得尤为重要。为了满足这一需求,我们可以利用Swift语言开发一款iOS平台上的上网行为管理监控软件。
181 2
|
3月前
|
数据可视化 iOS开发
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
40 4
|
3月前
|
iOS开发
iOS开发解释 App 生命周期,包括各个阶段的调用顺序。
iOS开发解释 App 生命周期,包括各个阶段的调用顺序。
26 1
|
3月前
|
存储 安全 数据安全/隐私保护
IOS开发数据存储:解释一下 iOS 中的 Keychain,它的作用是什么?
IOS开发数据存储:解释一下 iOS 中的 Keychain,它的作用是什么?
83 4
|
3月前
|
API 定位技术 iOS开发
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
42 2
|
3月前
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
89 2
|
3月前
|
API 开发工具 iOS开发
iOS 开发高效率工具包:10 大必备工具
iOS 开发高效率工具包:10 大必备工具
42 1