UIToolbar 上可更新的自定义视图
我想在 UIToolbar 中间创建一个小区域来显示一些信息,并且想知道执行此操作的最佳方法是什么。
我需要显示一些文本和图形,当新信息到达时,两者都需要更新(大约每 3 秒)。该图形与 iPhone 信号强度指示器类似,因此可以自定义绘制或从 3 个图形(低、中、高强度)之一中选择。
我可能会使用 initWithCustomView:
创建一个 UIBarButtonItem
,尽管我希望视图可单击(以允许用户更改显示的信息)。
实现该视图的最佳方式是什么?我可以使用 NIB,还是需要在视图中进行自定义绘图?更新按钮的最佳方法是什么?我假设我每次都必须重新制作 toolbarItems
数组,并在信息更改时设置它。有没有更干净的方法来做到这一点?谢谢。
I want to make a small area to present some information in the middle of a UIToolbar
and was wondering what the best way to do this is.
I need to show some text and a graphic, both of which need to be updated (around every 3 seconds) as new information arrives. The graphic is similar to the iPhone signal strength indicator, so it can be either custom drawn or selected from one of 3 graphics (low, medium, high strength).
I'll probably use initWithCustomView:
to create a UIBarButtonItem
, although I would like the view to be clickable (to allow the user to change the information shown).
What's the best way to make that view? Can I use a NIB, or do I need to do custom drawing in the view? What's the best way to update the buttons? I'm assuming that I'll have to remake the toolbarItems
array each time and set it when the information changes. Is there a cleaner way to do this? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
initWithCustomView:
听起来是一个不错的方法。您可以以任何您想要的方式创建自定义视图:使用 NIB、通过绘制、甚至使用图像。它也可以有自己的子视图。它可以是任何继承自 UIView 的对象。 (因此,如果您愿意,您甚至可以通过使用 UIButton、自定义 UIControl 或附加手势识别器的自定义 UIView 使其可操作。)您不必重新制作工具栏项目(或者就此而言,执行任何操作)如果您只保留指向自定义视图 UIBarButtonItem 的指针,则在添加所有按钮项后使用它。它可以是一个实例变量。然后,要更新自定义视图,您可以像访问任何其他视图一样访问它。我从来没有真正尝试过这个,但我看不出这样做有什么问题。
听起来你已经基本弄清楚了。希望这有帮助。
Using
initWithCustomView:
sounds like a good way to go. You can create your custom view any way you want: with a NIB, by drawing it, even using images. It can also have its own subviews. It can be any object that inherits from UIView. (So, if you wanted, you could even make it actionable by using a UIButton, a custom UIControl, or a custom UIView with a gesture recognizer attached.)You shouldn't have to remake toolbarItems (or, for that matter, do anything with it after you've added all your button items) if you just keep a pointer to your custom view UIBarButtonItem. It could be an instance variable. Then, to update the custom view, you could access it as you would any other view. I've never actually tried this, but I can't see any problem with doing it.
You sound like you had it mostly figured out. Hope this is helpful.
我需要相同的解决方案,并希望您提供一些代码示例。所以我最终在 IB 中完成了这一切,步骤如下:
希望这对某人有帮助,因为这已经困扰我一段时间了。
I needed the same solution and was hoping for some code examples from you. So I ended up doing it all in IB and the steps are here as follows:
Hope this helps someone as this has been eluding me for a while.