开发者

Updatable, custom view on a UIToolbar

开发者 https://www.devze.com 2023-02-28 03:20 出处:网络
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.开发者_JAVA技巧

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.开发者_JAVA技巧

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.


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.


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:

  1. Create UItoolbar in IB with no Items. (A Bar Button Item will be added again once you add the UIView)
  2. Add UIView as subview of UIToolbar
  3. Add UILabels to subview of UIView that is already a subview of the UIToolbar.
  4. Create IBOutlets from UIToolbar, UIView and each UILabel and then you can reference the labels in your app.
  5. I did set the backgrounds to clearColor so the text appears on top of UIToolbar without any box or borders.
  6. And the text updates dynamically which was the desired outcome.

Hope this helps someone as this has been eluding me for a while.

Updatable, custom view on a UIToolbar

0

精彩评论

暂无评论...
验证码 换一张
取 消