【问题标题】:center title and button in a custom sized UIAlertView iOS自定义大小的 UIAlertView iOS 中的中心标题和按钮
【发布时间】:2011-08-02 20:12:15
【问题描述】:

我正在使用 alertViews 向它们加载不同的对象,例如 textFields 和其他对象。 使用 textFields 没有问题。我已经成功地将 UIPickerView 添加为我的 alertView 的子视图,并且我已经调整了 alertView.frame 的大小以正确保存pickerView,但是 alertView 中的标题和按钮没有居中。 我尝试了许多选项 [alertView ...function...],但似乎没有一个可以解决这个问题。这在自定义大小的 alertView 中看起来很糟糕。有什么建议吗?

谢谢各位!

【问题讨论】:

    标签: ios ipad uialertview


    【解决方案1】:

    为了解决标题问题,我从这篇文章中得到了灵感: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]。 这对我来说很好,但我对新想法持开放态度!

    再见!

    【讨论】:

      猜你喜欢
      • 2018-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      相关资源
      最近更新 更多