0

I am trying to create a button like this in interface builder.

enter image description here

I have used this SO question as reference, however I can't get the image to sit on top of text.

I can only get an off centered text and image.

enter image description here

Here are some of the settings:

enter image description here

enter image description here

Community
  • 1
  • 1
user-44651
  • 3,924
  • 6
  • 41
  • 87
  • In inspector, if you choose "edge" we have the option to choose the image and change the edge value use "left , right,Top,bottom" inset – HariKrishnan.P Aug 04 '16 at 05:26

2 Answers2

2

An alternative that I frequently use in situations like this is to create a UIView in Interface Builder and change its class to UIControl. You can then add as many UIImageViews, UILabels, etc as you'd like & position them all with AutoLayout vs more "hacky" content insets.

UIButton is a subclass of UIControl and all the functionality you're probably looking for is also likely a part of UIControl, specifically the -addTarget:action:forControlEvents method.

William LeGate
  • 540
  • 7
  • 18
1

You are trying to use both the Image and the Label on the same view (UIButton view) and position them, this is not the best way to perform this kind of task.

And you can't add subclasses on UIButton in interface builder.

I would recommend you to create a UIView instead, and add separately a UIImageView with the settings image, and a UILabel with the "settings":

enter image description here

After that you can add TapGesturRecognizer for informing you when tap has made to your view button:

func tapSetup() {
        let tapOnMyViewButton = UITapGestureRecognizer(target: self, action: #selector(ViewController.didTapOnMyViewButton))
        self.byViewBtn.addGestureRecognizer(tapOnMyViewButton)
    }

    func didTapOnMyViewButton() {
        //Your logic
    }
MCMatan
  • 8,623
  • 6
  • 46
  • 85