自定义大小的 UIAlertView iOS 中的中心标题和按钮

发布于 2024-11-27 11:07:50 字数 254 浏览 1 评论 0原文

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

谢谢各位!

I am working with alertViews to load to them different objects, such as textFields and others.
With textFields there is no problem. I have successfully added a UIPickerView as a subview of my alertView and I had resized the alertView.frame to hold the pickerView properly, but then the title and the button in the alertView are not centered.
I tried with many of the options [alertView …function…] but none seems to work with this issue. This looks bad in a custom sized alertView. Any suggestions?

Thanks folks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窗影残 2024-12-04 11:07:50

为了解决标题问题,我从这篇文章中得到了灵感: ​​带背景的自定义 AlertView

首先,当应用程序呈现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 文件中发现按钮无法自定义...因此,如果有人对这个问题有任何提示,我们将不胜感激。

希望这对其他人有帮助!

更新:正如我所提到的,苹果表示alertViews中的按钮无法自定义,因此有两种选择:创建自定义按钮并将其添加为子视图或根本没有按钮。就我而言,我这样做了,并在 pickerView 代码的 didSelectRow...inComponent 部分中调用了 [myAlertView dismissWithClickedButtonIndex:-1animated:YES]
这对我来说效果很好,但我对新想法持开放态度!

再见!

to solve the Title issue, I got inspiration from this post: Custom AlertView With Background

First of all, when the App presents the alertView, I called a NSLog to get:

  • the pickerView's (my subview) width
  • the _titleLabel's width
  • the _titleLabel's starting position (MinX and MinY)

then I resized the _titleLabel frame and that was all!
textAlignment is Center by default so all I had to do was resize the label frame. other possible method is to use

[theTitle CGAffineTransformMakeTranslation(newXValue, 0]; 

Y value is 0 because I didn't want it to move vertically. But the described method is cleaner to me.

so what I added to the method that creates&presents my alertView was this:

NOTE: this code HAS to go BEFORE the addition of any additional view to the 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)

as for the button, i found in the UIAlertView.h file that buttons cannot be customized...so if someone has any tip on this issue, any help will be appreciated.

hope this helps someone else!

UPDATE: As I mentioned, Apple said buttons in alertViews cannot be customized, so there are two alternatives: create a custom button and add it as subview or have no button at all. In my case I did so, and called [myAlertView dismissWithClickedButtonIndex:-1 animated:YES] in the didSelectRow...inComponent part of the pickerView code.
This worked for me just fine, but I am open to new ideas!!

See ya!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文