3

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.

Dima Deplov
  • 3,688
  • 7
  • 45
  • 77

3 Answers3

2

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.

Two Buttons Touching

3) Or...

You can cheat and have simply one button and put some space between your image and your button text, like this:

One Button Yes, that's a whole bunch of spaces (which I've highlighted in green) before the word "Button".

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
0

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.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

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.

Dima Deplov
  • 3,688
  • 7
  • 45
  • 77