为了解决标题问题,我从这篇文章中得到了灵感:Custom AlertView With Background
首先,当App呈现alertView时,我调用了一个NSLog来获取:
- pickerView 的(我的子视图)宽度
-
_titleLabel 的宽度
-
_titleLabel 的起始位置(MinX 和 MinY)
然后我调整了_titleLabel 框架的大小,仅此而已!
textAlignment 默认为中心,所以我所要做的就是调整标签框的大小。其他可能的方法是使用
[theTitle CGAffineTransformMakeTranslation(newXValue, 0];
Y 值为 0,因为我不希望它垂直移动。但所描述的方法对我来说更干净。
所以我添加到创建和呈现我的 alertView 的方法中是这样的:
注意:此代码必须在向 alertView 添加任何其他视图之前执行!
//... alertView creation and display
//create a title object to edit titleLable properties
UILabel *theTitle = [pickers valueForKey:@"_titleLabel"];
// set frame for titleLabel to begin where it currently begins in both X and Y, set its width to the width of th e picker +/- the difference in X in case they don't have the same MinX value, and set the height it already has
theTitle.frame = CGRectMake(CGRectGetMinX(theTitle.frame), CGRectGetMinY(theTitle.frame), CGRectGetWidth(pkPais.frame)+6, CGRectGetHeight(theTitle.frame));
//...add subviews and set its frames (in my case, a pickerView)
至于按钮,我在 UIAlertView.h 文件中发现按钮无法自定义...所以如果有人对此问题有任何提示,我们将不胜感激。
希望这对其他人有帮助!
更新:正如我所提到的,Apple 表示 alertViews 中的按钮无法自定义,因此有两种选择:创建自定义按钮并将其添加为子视图或根本没有按钮。就我而言,我这样做了,并在pickerView 代码的didSelectRow...inComponent 部分中调用了[myAlertView dismissWithClickedButtonIndex:-1 animated:YES]。
这对我来说很好,但我对新想法持开放态度!
再见!