How to set 2 images in a UIButton? - Swift - ios

I would like to set 2 images in a UIButton, one at the left and one at the right. I did a SetImage, so I have my image on the left, but how can I do with the second image? Thanks

You simply can't. UIButton wasn't designed to support this. Instead, create a normal UIView, add the images to that view then use an UITapGestureRecognizer for it to act like a tappable button & do some work when tapped.
In order to achieve a Highlight effect, check this answer out How to highlight a UIView like Apple does.

Related

Custom UISlider with pips ios

Wondering if the above can be created using UISlider? If not, what other ways can this be accomplished?
You can set components of a UISlider, such as the currentThumbImage ( see: "Appearance of Sliders"1).
However, it is almost certainly easier to just re-implement a slider for this much customization. Simply use background UIImageView with the scale image, and then add a separate UIView (or UIImageView) for the arrow. Finally, attach a UIPanGestureRecognizer to the arrow view to allow a user translate the view vertically.
You can change a lot in the appearance of a UISlider like setting the thumb to a red arrow. You can also replace the background image with the inches ruler and with different rulers for the different device types and display sizes.
The one thing that I don't see is that you turn the slider to work vertically. I know them only working left to right.
If I'm right, your only chance is to have a ruler as background image and a view that contains the arrow and a label with the actual value. That whole view can be pawned and tapped using Gesture Listener.

Make a small UIImageView easy to be tapped

I have a UITableView with rows.
Each row has a small UIImageView aligned to the right (a "bookmark" icon)
The UIimageView has a UITapGestureRecognizer associated.
cell.favoritedImageView.userInteractionEnabled = true
cell.favoritedImageView.addGestureRecognizer(gestureRecognizer)
The problem is that to actually tap it with the finger (in a real device), you have to use the tip of the finger and be very accurate, because the image is small.
If you miss tapping the imageView, the cell is tapped (didSelectRowAtIndexPath) and you end up executing a show-segue to another view, so you have to go back and try again (not cool)
Question: what is the best way to solve this? I want it to be easy to be tapped.
I have some ideas:
Create a larger image with transparent surrounding (ie: crop out with transparent background) -- downside is that I also use this image in other views, in which is not tappable, so I'd have to create two versions of the image
Put the image inside a UIView and make the UIView big and tappable instead of the UIImageView
Add padding to the UIImageView (will this work? or the padding is not recognized in the UITapGestureRecognizer?)
Per your own suggestion, you should create a transparent view that is much larger and attach the UITapGestureRecognizer to the view and then nest your smaller image within the view. That way appearances are the same, but you handle a much larger area for the tap to be recognized with selecting the cell.

iOS: Combine two UIButtons to one UIButton

On the storyboard I have two UIButtons. In my UIViewController subclass I have one IBAction method for this buttons. One of this buttons has an image and hasn't got a title, another has the title but hasn't got the image. So, it's simply image that behaves as a button and ordinary button.
Both of this buttons call a method. The problem is that if I push one button, another doesn't highlight. Another problem that this buttons have padding and for me will be better if this padding will be touchable (I mean space between image button and title button to be clickable).
I know that I can subclass UIButton and make my buttons as I want, but maybe there is a way to make what I want without subclassing?
Thanks.
1)
Assign both buttons to properties.
When an IBAction fires, you can set one or both buttons to highlighted via those controls' "highlighted" properties, or via setHighlighted:.
2)
As for making space between the buttons touchable, set the alignment for the button graphic to left aligned or right aligned (the latter is what I've done in my example below) and these two separate buttons have edges that are touching.
3) Or...
You can cheat and have simply one button and put some space between your image and your button text, like this:
Yes, that's a whole bunch of spaces (which I've highlighted in green) before the word "Button".
Subclassing is a good thing. If you need a button with both an image and a title, then by all means create a subclass of UIButton that does what you want. You do that once, and then use it anywhere that you want. The alternative is to muck around with stacked duplicate buttons everywhere you want this look.
I found the most suitable variant for me and I think for all, who encountered the same problem. If you have one UIButton for image and another UIButton for text(title label), you can combine this two UIButtons to one Custom type UIButton. Then you can set image and title label text to this custom button. After that, use Insets to layout your image and title label.
For example, to set title label under image you must set Inset Left parameter as negative value and Inset Left parameter as positive value that is greater than image height. With Insets you can set proper image size too.
Using this way you don't need to handle highlight property changing by yourself.

Make a UIButton background image larger than frame

I'm trying to make a UIButton with a background image where the image is larger than the button frame.
The problem is, the UIButton automatically scales the image to fit and haven't found a way to change the behavior.
Am I missing something obvious or is it time to make a custom button?
Subclass UIButton and override the
- (CGRect)backgroundRectForBounds:(CGRect)bounds;
method so it returns the size you want.
TIP: Use this method to center your background as well.
Looks like the answer to this one is. Not possible through existing interface on UIButton.
The simplest way to implement seems to be using a custom sub class of UIButton and creating a UIImageView with the same frame as the UIButton, turn off clipping and scaling and then just capture all the setBackground methods and capture state changes to handle "normal" button interaction changing the background images.

Advice on Twitter like UIView

I have a question on some UIView stuff. I want to create a Twitter-like UIView cf image. So, how to create this type of view? Is it composed of several UIViews like 1 for the topbar and a second for the content (white one).
If yes, how to create this shadow between these views?
How to create the white arrow on top left (just under the image).
Any code sample will be appreciated. Thanks all!
http://i.stack.imgur.com/MM8Qt.png
It's simple UIViewController with white background, containing UITableView with one cell.

Resources