自定义大小的 UIAlertView iOS 中的中心标题和按钮
我正在使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了解决标题问题,我从这篇文章中得到了灵感: 带背景的自定义 AlertView
首先,当应用程序呈现alertView时,我调用 NSLog 来获取:
_titleLabel
的宽度_titleLabel
的起始位置(MinX 和 MinY)然后我调整了
_titleLabel
框架的大小,仅此而已!textAlignment
默认情况下居中,所以我所要做的就是调整标签框架的大小。另一种可能的方法是使用Y 值为 0,因为我不希望它垂直移动。但所描述的方法对我来说更干净。
所以我添加到创建并呈现我的alertView的方法中是这样的:
注意:此代码必须在向alertView添加任何其他视图之前执行!
至于按钮,我在 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:
_titleLabel
's width_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 useY 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!
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 thedidSelectRow...inComponent
part of the pickerView code.This worked for me just fine, but I am open to new ideas!!
See ya!