开发者

Add a second image to a custom UIButton

开发者 https://www.devze.com 2023-01-12 06:35 出处:网络
I\'ve got an Custom UIButton. It\'s got a \"static\" background image a variable text (the Title) which gets set in the code

I've got an Custom UIButton. It's got

  • a "static" background image
  • a variable text (the Title) which gets set in the code

Now I would like to add an icon (UIImage/UIImageView) inside the button on the left of the text. (I use the indent to move the text slightly to the right). Is there an easy way of adding that icon (and referencing it from code, so I can change it) or would you recommend creating a completely new bu开发者_Python百科tton e.g. based on a UIView? (e.g. a view, that responds to touches)?

I'm just trying to get a feel for what the best approach would be for this. Any experience?


Two ways:

  1. I prefer doing this by subclassing a UIView and (as you mention) implement the UITouch-responder methods. You're also able to override drawRect: what I really like because it makes your app soooooo much faster!
  2. Since UIButton is a subclass of UIView, you can add a UIImageView to your button by using addSubview:. If you save it in your main-class (@property (...) UIButton *button) you can always access it by calling [self button]. You can also subclass your UIButton and add this @property. That's up to you.

It's totally up to you. However I prefer the first way!

0

精彩评论

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