Chris D. Chris D. - 6 months ago 59
Swift Question

How do you set the height and width of a UIButton with image

This is driving me nuts. I have looked everywhere and cannot figure this out. Check it out...

let findMeButton = UIButton(type: UIButtonType.System)

findMeButton.translatesAutoresizingMaskIntoConstraints = false

findMeButton.setImage(UIImage(named: "locateMe"), forState: UIControlState.Normal)

findMeButton.addTarget(self, action: #selector(MapViewController.findUserLocation(_:)), forControlEvents: UIControlEvents.TouchUpInside)

view.addSubview(findMeButton)

// I added this line of code and it still doesn't work.
findMeButton.frame.size = CGSizeMake(50, 50)

findMeButton.bottomAnchor.constraintEqualToAnchor(bottomLayoutGuide.topAnchor, constant: -10).active = true

findMeButton.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor, constant: 5).active = true


I am still learning iOS. How do you set the Height and Width of this UIButton with an Image. Everything I have tried has given me an error or just didn't work. I am still trying to rap my head around what translatesAutoresizingMaskIntoConstraints does. I just simply want to have the Button where it is but change its size (Height & Width).

Thanks in advance

EDIT: I have changed the bit of code to this

// Locate user button
let locateButton = UIButton(type: UIButtonType.System) as UIButton

locateButton.frame = CGRectMake(0, 0, 50, 50)

locateButton.setBackgroundImage(UIImage(named: "locateMe"), forState: UIControlState.Normal)

locateButton.addTarget(self, action: #selector(MapViewController.findUserLocation(_:)), forControlEvents: UIControlEvents.TouchUpInside)

view.addSubview(locateButton)


I want to position the button to the windows bottom and right margin. I also want to set the image dimensions to height 50 x width 50. How would I accomplish this?

EDIT 2: I figure you have to use Auto Layout to do so but can someone show me how. Everything I have done hasn't worked.

Answer

so here i wrote a code to add the button on view.

      let button   = UIButton(type: UIButtonType.System) as UIButton
      // set the frame
      button.frame = CGRectMake(100, 100, 100, 50)
      // add image
      button.setBackgroundImage(UIImage(named:"SearchIcon" ), forState: UIControlState.Normal)
      // button title
      button.setTitle("Test Button", forState: UIControlState.Normal)
      // add action
      button.addTarget(self, action: #selector(RootViewController.updateView), forControlEvents: UIControlEvents.TouchUpInside)

      button.translatesAutoresizingMaskIntoConstraints = false
  // add button on view
       self.view.addSubview(button)
 // all constaints
    let widthContraints =  NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200)
    let heightContraints = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100)
       let xContraints = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
      let yContraints = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
        NSLayoutConstraint.activateConstraints([heightContraints,widthContraints,xContraints,yContraints])
Comments