I have an element that needs to behave like UIButton but it has several (3) text labels visible at once and multiple UIImages in the same bounding box. It's really a view with a bunch of different other UIViews and labels that needs to look and act like a button but with more custom placement of these elements than the standard UIButton.
Is it better to inherit from UIButton to accomplish this or is UIControl the one to inherit from?
When the element is tapped, I do want to mimic all of the highlighting effects (if it's UILabel, show the highlight text color, etc) as well.
As long as it doesn't matter which part of your button content gets tapped to activate it's functions you could compose a UIButton along with all the UILabel, UIImageViews you need.
You could easily inherit from UIView, place all of the above inside and place your transparent UIButton on top of everything to get the events you need (setting yourself as the target for that button and implementing some delegate to notify about selection).
Related
I am not a huge fan of interface builder but I am trying to create a UIButton that would highlight when touched. I was thinking the whole button to reverse. I see this property reverses on highlight but what it does is to reverse the text. I want the whole button to reverse color.
This is how I have it now:
This is a green button with rounded corners. Is there a way to make the whole button to reverse from interface builder?
This is an iOS 7 only project.
If you want to keep things inside interface builder, I believe your only option is to configure different background images for each button state. I had the same problem and couldn't find a way to use different UIColors for different button states. If your button style is really minimal, you could potentially use 1x1 pngs and apply rounded corners using Quartz directly in code.
In my case, I ended up creating a custom subclass of UIButton with a setter to configure different colors/text colors and shadows for each state (I used an enum to map different button "themes").
i often do not split view and controller related things correctly.
should i always subclass a uiview if i want custom uibuttons and backgrounds in my app and add style related stuff in my view or should i handle this in my controller? or probably subclass a uibutton?
what about alignings? when should i subclass a uiview, add buttons to it and align then or handle this in my controller?
thanks for your hints!
please leave some comments if something is unclear.
Should i always subclass a uiview if I want custom uibuttons and
backgrounds in my app and add style related stuff in my view
If your need are basic, you coan just insert wanted elements as subviews of your view without having to subclass anything. Buttons and views have enough properties to handle those simple needs. But everything depends on what are those needs.
Should i handle this in my controller? or probably subclass a
uibutton?
Subclassing UIButton for UIButton behaviour is not a bad idea :-) But as said before, what do you need as special behaviour ? A special image : there is a property for that. A special reaction on events, manage it into the controller on IBActions. A UIButton with UFO behaviours, ok, let's subclass it.
What about alignings? when should i subclass a uiview, add buttons to
it and align then or handle this in my controller?
Hmmm... I guess never. This can (should) be done in the controller. The controller controls the whole UIItems. So if you want to align one item regarding another, do it into the controller. If you want to align special graphics or text that is displayed into the button, sublass it and manage that in the drawRect method.
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.
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:
Create UItoolbar in IB with no Items. (A Bar Button Item will be added again once you add the UIView)
Add UIView as subview of UIToolbar
Add UILabels to subview of UIView that is already a subview of the UIToolbar.
Create IBOutlets from UIToolbar, UIView and each UILabel and then you can reference the labels in your app.
I did set the backgrounds to clearColor so the text appears on top of UIToolbar without any box or borders.
And the text updates dynamically which was the desired outcome.
Hope this helps someone as this has been eluding me for a while.
I have several custom uibuttons on my view. I want to create a toggle button which when pressed, it will loop through all the UIButtons and enable a background image for them.
What I have done is to use an image as a background, then created clickable parts of it using blank custom buttons. I want this toggle function to then show the buttons.
My plan is to create a subclassed UIButton for the "hidden" buttons. When the toggle button is pressed, the code should then set the background image for each of these buttons to a "reddot.png". That stays on the screen until the toggle button is pressed again - this then disables each sub classed uibuttons background image.
What's the best way to do this?
I would advise you to not subclass UIButton for two reasons. First, UIButton is actually a class-cluster, which makes subclassing rather difficult. Secondly, I don't think it is needed in your case.
Simply create all the buttons as custom buttons. You can customize their appearance using methods like [button setHidden:] and [button setBackgroundImage:forState:]. The toggle button could then simply by linked to an IBAction, which would apply the appropriate customizations to the other buttons.
I'm trying to figure out a best way to implement the picture-editing capability shown in the native address book app on iPhone.
On the built-in address book, this is how the picture looks like before editing:
qkpic.com/2f7d4 http://qkpic.com/2f7d4
And after clicking edit, notice how "Edit" overlay is added and the image becomes clickable:
qkpic.com/fb2f4 http://qkpic.com/fb2f4
What would be the best way to implement something like this? Should I make the image a button from the beginning and have tapping disabled at first? If so, what steps are required to add an overlay/label to the image (in above example, gray border + the text "Edit" is added)
The easiest way is to use Interface Builder, create a container view, then add a UIImageView and UILabel as subviews to it. You would position and style the text and the image but set the UILabel to hidden. Make the whole container view respond to touches. It's easy to do since UIView is derived from UIResponder so all you have to do is override touchesEnded. Whenever you want to change the text label, just set the UILabel to hidden=NO.
There's more to it, however. Notice how the image has rounded corners? You'll want to override the UIImageView's drawRect method to implement a custom drawing routine to do that. There's lots of sample code around and it wasn't part of your original question so I'll stop here.