下面我就来详细讲解一下“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技术站