ios 实现自定义alertview
时间: 2023-05-03 19:00:30 浏览: 81
要在iOS实现自定义alertview,可以通过以下步骤:
1.创建一个UIView或者自定义的视图作为alertview的容器;
2.在容器上添加需要的UI控件,例如UILabel、UIButton等;
3.为容器添加动画效果,例如弹出或者淡入等;
4.监听容器内的事件,例如按钮点击等;
5.手动移除容器视图,并添加消失动画效果。
相关问题
自定义AlertView的方法和改变Alert的弹出位置以及其宽度
自定义UIAlertView的方法已经在iOS13之后被废弃,推荐使用UIAlertController来代替。下面介绍如何自定义UIAlertController的弹出位置以及宽度。
1. 自定义弹出位置
可以使用UIAlertController的popoverPresentationController属性来设置弹出位置。具体步骤如下:
```
// 创建UIAlertController对象
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
// 创建弹出位置的UIView对象
UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
// 设置弹出位置
alertController.popoverPresentationController.sourceView = popoverView;
alertController.popoverPresentationController.sourceRect = popoverView.bounds;
// 弹出UIAlertController
[self presentViewController:alertController animated:YES completion:nil];
```
2. 自定义宽度
可以通过设置UIAlertController的preferredContentSize属性来改变其宽度。具体步骤如下:
```
// 创建UIAlertController对象
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
// 设置宽度
alertController.preferredContentSize = CGSizeMake(280, 200);
// 弹出UIAlertController
[self presentViewController:alertController animated:YES completion:nil];
```
需要注意的是,此方法只适用于UIAlertControllerStyleAlert风格的UIAlertController。如果使用UIAlertControllerStyleActionSheet风格的UIAlertController,设置preferredContentSize属性将不会生效。