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

相关文章
|
7天前
|
前端开发 Android开发 iOS开发
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
【4月更文挑战第30天】Flutter 框架实现跨平台移动应用,通过一致的 UI 渲染(Skia 引擎)、热重载功能和响应式框架提高开发效率和用户体验。然而,Android 和 iOS 的系统差异、渲染机制及编译过程影响性能。性能对比显示,iOS 可能因硬件优化提供更流畅体验,而 Android 更具灵活性和广泛硬件支持。开发者可采用代码、资源优化和特定平台优化策略,利用性能分析工具提升应用性能。
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
|
8天前
|
存储 Swift iOS开发
使用Swift开发一个简单的iOS应用的详细步骤。
使用Swift开发iOS应用的步骤包括:创建Xcode项目,设计界面(Storyboard或代码),定义数据模型,实现业务逻辑,连接界面和逻辑,处理数据存储(如Core Data),添加网络请求(必要时),调试与测试,根据测试结果优化改进,最后提交至App Store或其它平台发布。
21 0
|
8天前
|
安全 Swift iOS开发
【Swift 开发专栏】Swift 与 UIKit:构建 iOS 应用界面
【4月更文挑战第30天】本文探讨了Swift和UIKit在构建iOS应用界面的关键技术和实践方法。Swift的简洁语法、类型安全和高效编程模型,加上与UIKit的紧密集成,使开发者能便捷地创建用户界面。UIKit提供视图、控制器、布局、动画和事件处理等功能,支持灵活的界面设计。实践中,遵循设计原则,合理组织视图层次,运用布局和动画,以及实现响应式设计,能提升界面质量和用户体验。文章通过登录、列表和详情界面的实际案例展示了Swift与UIKit的结合应用。
|
8天前
|
存储 安全 Swift
【Swift 开发专栏】使用 Swift 开发一个简单的 iOS 应用
【4月更文挑战第30天】本文介绍了使用 Swift 开发简单 iOS 待办事项应用的步骤。首先,阐述了 iOS 开发的吸引力及 Swift 语言的优势。接着,详细说明了应用的需求和设计,包括添加、查看和删除待办事项的功能。开发步骤包括创建项目、界面搭建、数据存储、功能实现,并提供了相关代码示例。最后,强调了实际开发中需注意的细节和优化,旨在帮助初学者掌握 Swift 和 iOS 开发基础。
|
16天前
|
iOS开发 开发者 UED
利用SwiftUI构建动态列表:iOS开发的新范式
【4月更文挑战第22天】在本文中,我们将深入探讨如何使用SwiftUI来创建动态列表。SwiftUI是苹果最新推出的用户界面工具集,它允许开发者以声明式的方式描述用户界面,从而简化了代码的复杂性。我们将通过具体的代码实例,展示如何利用SwiftUI的List和ForEach视图来创建动态列表,并讨论其在实际开发中的应用。
17 2
|
20天前
|
API 定位技术 iOS开发
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
【4月更文挑战第18天】**Cocoa Touch** 是iOS和Mac OS X应用的核心框架,包含面向对象库、运行时系统和触摸优化工具。它提供Mac验证的开发模式,强调触控接口和性能,涵盖3D图形、音频、网络及设备访问API,如相机和GPS。是构建高效iOS应用的基础,对开发者至关重要。
19 0
|
28天前
|
搜索推荐 iOS开发 开发者
利用SwiftUI构建动态用户界面:iOS开发新篇章
【4月更文挑战第10天】在移动应用的世界中,流畅的用户体验和引人注目的界面设计是至关重要的。随着SwiftUI的推出,iOS开发者被赋予了创造高度动态且响应式界面的能力。本文将深入探讨如何利用SwiftUI的强大特性来实现一个动态用户界面,包括其声明性语法、状态绑定以及视图更新机制。我们将通过一个天气应用案例,了解如何有效地运用这些工具来提升应用的交互性和视觉吸引力。
|
Android开发 iOS开发 UED
iOS 自定义收款键盘
在iOS8之前,iOS系统的输入法只能使用苹果官方提供的输入法。 对于中文来说,官方的输入法并不好用,或者说不够好用,词库,联想,云输入等都没有或者和搜狗输入法,百度输入法等有中国特色的输入法相比有一定的差距。
170 0
|
编解码 iOS开发
iOS自定义的emoji表情键盘
iOS自定义的emoji表情键盘
297 0
iOS自定义的emoji表情键盘