iOS自定义提示弹出框实现类似UIAlertView的效果

yizhihongxing

下面我就来详细讲解一下“iOS自定义提示弹出框实现类似UIAlertView的效果”的完整攻略。

第一步:创建一个背景视图

我们首先需要创建一个背景视图,用于展示提示弹出框。可以使用UIView或者UIControl来实现,具体代码如下:

UIView *bgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
[[UIApplication sharedApplication].keyWindow addSubview:bgView];

在以上代码中,我们创建了一个UIView对象,设置了它的frame为屏幕大小,并设置了它的背景颜色为灰色半透明。最后将这个背景视图添加到主窗口上。

第二步:创建一个提示弹出框

接下来,我们需要在背景视图上创建一个提示弹出框。可以使用UIView来实现,具体代码如下:

UIView *alertView = [[UIView alloc] initWithFrame:CGRectMake(50, 200, [UIScreen mainScreen].bounds.size.width - 100, 200)];
alertView.backgroundColor = [UIColor whiteColor];
alertView.layer.cornerRadius = 10;
bgView.userInteractionEnabled = YES;
[bgView addSubview:alertView];

在以上代码中,我们创建了一个UIView对象,设置了它的frame为屏幕宽度-100,高度为200,居中显示。并将其背景颜色设置为白色,圆角设置为10,并且将其添加到背景视图上。

第三步:添加提示信息

接下来,我们需要向提示弹出框中添加提示信息,可以使用UILabel来实现,具体代码如下:

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, alertView.bounds.size.width - 20, 30)];
titleLabel.text = @"提示信息";
titleLabel.textAlignment = NSTextAlignmentCenter;
[alertView addSubview:titleLabel];

UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(titleLabel.frame) + 10, alertView.bounds.size.width - 20, 60)];
messageLabel.text = @"这是一个自定义提示弹出框的示例";
messageLabel.textAlignment = NSTextAlignmentCenter;
[alertView addSubview:messageLabel];

在以上代码中,我们分别创建了两个UILabel对象,用于展示标题和提示信息。并且将它们添加到提示弹出框中。

第四步:添加按钮

最后一步,我们需要向提示弹出框中添加按钮,用于用户的操作。可以使用UIButton来实现,具体代码如下:

UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(messageLabel.frame) + 10, alertView.bounds.size.width / 2 - 20, 40)];
[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
cancelButton.backgroundColor = [UIColor redColor];
[cancelButton addTarget:self action:@selector(cancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[alertView addSubview:cancelButton];

UIButton *okButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(cancelButton.frame) + 20, CGRectGetMaxY(messageLabel.frame) + 10, alertView.bounds.size.width / 2 - 20, 40)];
[okButton setTitle:@"确定" forState:UIControlStateNormal];
okButton.backgroundColor = [UIColor greenColor];
[okButton addTarget:self action:@selector(okButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[alertView addSubview:okButton];

在以上代码中,我们创建了两个UIButton对象,用于展示“取消”和“确定”按钮,并且将它们添加到提示弹出框中。同时我们也为按钮添加了点击事件,分别为cancelButtonClicked:和okButtonClicked:。

至此,我们的自定义提示弹出框已经实现了。可以根据实际需要进行调整和修改。

下面提供两个示例:

示例一:展示一个警告框,并打印用户的操作结果

UIView *bgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
[[UIApplication sharedApplication].keyWindow addSubview:bgView];

UIView *alertView = [[UIView alloc] initWithFrame:CGRectMake(50, 200, [UIScreen mainScreen].bounds.size.width - 100, 200)];
alertView.backgroundColor = [UIColor whiteColor];
alertView.layer.cornerRadius = 10;
bgView.userInteractionEnabled = YES;
[bgView addSubview:alertView];

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, alertView.bounds.size.width - 20, 30)];
titleLabel.text = @"警告";
titleLabel.textAlignment = NSTextAlignmentCenter;
[alertView addSubview:titleLabel];

UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(titleLabel.frame) + 10, alertView.bounds.size.width - 20, 60)];
messageLabel.text = @"确定删除该文件吗?";
messageLabel.textAlignment = NSTextAlignmentCenter;
[alertView addSubview:messageLabel];

UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(messageLabel.frame) + 10, alertView.bounds.size.width / 2 - 20, 40)];
[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
cancelButton.backgroundColor = [UIColor redColor];
[cancelButton addTarget:self action:@selector(cancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[alertView addSubview:cancelButton];

UIButton *okButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(cancelButton.frame) + 20, CGRectGetMaxY(messageLabel.frame) + 10, alertView.bounds.size.width / 2 - 20, 40)];
[okButton setTitle:@"确定" forState:UIControlStateNormal];
okButton.backgroundColor = [UIColor greenColor];
[okButton addTarget:self action:@selector(okButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[alertView addSubview:okButton];

- (void)cancelButtonClicked:(UIButton *)sender {
    [sender.superview.superview removeFromSuperview];
    NSLog(@"用户取消了删除操作");
}

- (void)okButtonClicked:(UIButton *)sender {
    [sender.superview.superview removeFromSuperview];
    NSLog(@"用户确定了删除操作");
}

示例二:展示一个输入框,并保存用户输入的内容

UIView *bgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
[[UIApplication sharedApplication].keyWindow addSubview:bgView];

UIView *alertView = [[UIView alloc] initWithFrame:CGRectMake(50, 200, [UIScreen mainScreen].bounds.size.width - 100, 200)];
alertView.backgroundColor = [UIColor whiteColor];
alertView.layer.cornerRadius = 10;
bgView.userInteractionEnabled = YES;
[bgView addSubview:alertView];

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, alertView.bounds.size.width - 20, 30)];
titleLabel.text = @"输入信息";
titleLabel.textAlignment = NSTextAlignmentCenter;
[alertView addSubview:titleLabel];

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(titleLabel.frame) + 10, alertView.bounds.size.width - 20, 40)];
textField.placeholder = @"请输入内容";
textField.borderStyle = UITextBorderStyleRoundedRect;
[alertView addSubview:textField];

UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(textField.frame) + 10, alertView.bounds.size.width / 2 - 20, 40)];
[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
cancelButton.backgroundColor = [UIColor redColor];
[cancelButton addTarget:self action:@selector(cancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[alertView addSubview:cancelButton];

UIButton *okButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(cancelButton.frame) + 20, CGRectGetMaxY(textField.frame) + 10, alertView.bounds.size.width / 2 - 20, 40)];
[okButton setTitle:@"确定" forState:UIControlStateNormal];
okButton.backgroundColor = [UIColor greenColor];
[okButton addTarget:self action:@selector(okButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[alertView addSubview:okButton];

- (void)cancelButtonClicked:(UIButton *)sender {
    [sender.superview.superview removeFromSuperview];
}

- (void)okButtonClicked:(UIButton *)sender {
    NSString *inputText = [(UITextField *)(sender.superview.subviews[1]) text];
    NSLog(@"用户输入的内容为:%@", inputText);
    [sender.superview.superview removeFromSuperview];
}

以上就是“iOS自定义提示弹出框实现类似UIAlertView的效果”的完整攻略,希望能对你有所帮助。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:iOS自定义提示弹出框实现类似UIAlertView的效果 - Python技术站

(0)
上一篇 2023年6月25日
下一篇 2023年6月25日

相关文章

  • 详解CSS布局中浮动问题的四种解决方案

    首先我们先来介绍一下CSS布局中浮动问题的背景。 在CSS布局中,为了让元素自由地流动,我们会经常使用浮动来实现布局。但是,浮动也带来了很多问题,比如容易造成父元素高度塌陷、子元素溢出等。 接下来,我们就来详解CSS布局中浮动问题的四种解决方案。 方案一:使用clearfix clearfix 是一个经典的解决浮动问题的方案,原理是清除元素的浮动影响,同时可…

    other 2023年6月26日
    00
  • VS2015找不到win32位的控制台怎么解决?

    解决VS2015找不到win32位的控制台问题攻略 如果在使用Visual Studio 2015时遇到了找不到win32位的控制台的问题,可以按照以下步骤进行解决。 步骤一:检查安装配置 首先,确保你已经正确安装了Visual Studio 2015,并且选择了包含win32位开发工具的安装选项。如果你没有选择这个选项,你需要重新运行安装程序并选择正确的配…

    other 2023年7月28日
    00
  • Serveral effective linux commands

    Linux命令攻略 Linux命令是Linux系统中最基本和最重要的工具之一。本攻略将介绍几个常用的Linux命令,包括ls、grep、find和chmod,并提供两个示例说明。 ls命令 ls命令用于列出目录中的文件和子目录。以下是ls命令的基本语法: ls [options] [file|dir] 其中,options参数是可选的命令选项,file|di…

    other 2023年5月6日
    00
  • sqlexec命令用法

    以下是sqlexec命令用法的完整攻略: 1. sqlexec命令简介 sqlexec是一个命令行工具,用于在命令行中执行SQL语句。它可以连接到各种数据库,包括MySQL、Oracle、SQL Server等,并执行SQL查询、插入、更新和删除等操作。 2. sqlexec命令语法 sqlexec命令的基本语法如下: sqlexec [options] […

    other 2023年5月8日
    00
  • 网页中右键功能的实现方法之contextMenu的使用

    为网站添加自定义的右键菜单是网页设计中的一个常见需求。在Web开发中,我们可以使用JavaScript中的contextMenu来实现自定义的右键菜单。以下是实现过程的步骤和示例。 步骤 1.定义右键菜单:使用HTML标记定义菜单,使用CSS为其设置样式。菜单通常使用<ul>和<li>标记定义,CSS可以定义它们的样式和位置。 2.设…

    other 2023年6月27日
    00
  • Android开发-之五大布局详解

    Android开发-之五大布局详解攻略 1. 线性布局(LinearLayout) 线性布局是Android开发中最常用的布局之一。它按照水平或垂直方向排列子视图。以下是一个示例: <LinearLayout android:layout_width=\"match_parent\" android:layout_height=\&…

    other 2023年8月24日
    00
  • JPA Specification常用查询+排序实例

    下面将详细讲解 JPA Specification 常用查询和排序的实现方法。 一、JPA Specification 查询实例 1. 前置条件 在使用 JPA Specification 进行查询前,需要先引入相关的依赖: <!– JPA规范,提供了一套标准API操作数据库 –> <dependency> <groupId…

    other 2023年6月27日
    00
  • 如何修改logback.xml配置文件在resource以外的位置

    要修改logback.xml配置文件在resource以外的位置,可以按以下步骤进行: 1.找到需要将logback.xml文件移动到哪个路径,例如:/data/logs/config/logback.xml。 2.在web应用程序的web.xml中添加logback的listener如下: <listener> <listener-cla…

    other 2023年6月25日
    00
合作推广
合作推广
分享本页
返回顶部