UIAlertController 高级应用加入图片等自定义控件

官方公开的属性,并不多,貌似可以定制的东西很有限,但是,我们不能忽略的是UIAlertController是继承与UIViewController的,我们很容易联想到UIViewController的一些特征和属性,最简单的一点就是UIViewController有一个View,我们可以给View加东西。 既然如此,那就去加东西。

实例代码如下

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    button.backgroundColor = [UIColor yellowColor];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    }

  • (void)buttonClick
    {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"没有标题的标题"
    message:@"学无止境,漫漫长路"
    preferredStyle:UIAlertControllerStyleAlert ];

    //添加取消到UIAlertController中
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancelAction];

    //添加确定到UIAlertController中
    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:OKAction];

    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 30, 30)];
    imageView2.image = [UIImage imageNamed:@"WechatIMG20"];
    [alertController.view addSubview:imageView2];

    [self presentViewController:alertController animated:YES completion:nil];

}
对你有帮助的话,点个赞吧👍

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容