UIAlertController中的自定义视图 [英] Custom View in UIAlertController

查看:1900
本文介绍了UIAlertController中的自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在UIAlertController中放入自定义视图.自定义视图的大小会遇到一些奇怪的问题.

I am trying to put a custom view inside a UIAlertController. I'm running into some odd issues with the sizing of the custom view.

我希望自定义视图跨越UIAlertController的宽度,无论它是什么.我正在使用CGRectGetWidth(alertController.view.bounds)来获取警报控制器的宽度.但是,这似乎是在返回整个视图的宽度.使用view.frame并没有什么不同.

I want the custom view to span the width of the UIAlertController, whatever that might be. I'm using CGRectGetWidth(alertController.view.bounds) to get the width of the alert controller. However, this seems instead to be returning the width of the entire view. using view.frame does not make a difference.

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"My Title" message:nil preferredStyle:UIAlertControllerStyleAlert];

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(alertController.view.bounds), 50)];
view.backgroundColor = [UIColor blueColor];

[alertController.view addSubview:view];
[self presentViewController:alertController animated:YES completion:nil];

这将产生以下结果.我在尝试获取UIAlertController的X,Y宽度和高度属性时遇到相同的问题.有谁知道我如何在不使用硬编码数字的情况下将该视图放置在警报控制器的中间?

This yields the results below. I have the same issue trying to get X, Y width and height properties of the UIAlertController. Does anyone know how I can position this view in the middle of the alert controller without using hard-coded numbers?

推荐答案

您不应该这样做.引用文档:

You're not supposed to do that. To quote the docs:

UIAlertController类旨在按原样使用,不支持子类化.

The UIAlertController class is intended to be used as-is and does not support subclassing.

此类的视图层次结构是私有的,不能修改.

The view hierarchy for this class is private and must not be modified.

如果您反对苹果这样的明确声明,那么所有赌注都将被取消,即使您可以在当前的OS版本上使用它,也可能会与任何将来的版本兼容.

If you go against an explicit statement like that from Apple all bets are off, and even if you can get it to work on the current OS version, it could break with any future version.

这篇关于UIAlertController中的自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆