Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 285
RoboChris Avatar asked May 27 '16 03:05

RoboChris


People also ask

How do I put the image on the right side of the text in a UIButton?

To make an UIButton's image align to the right side of the text, we force content flipping behavior to the one use it right-to-left language. 1 By adding this semanticContentAttribute = .

How do I resize a button image in Swift?

Using storyboard, select the button, then in the size inspect click the dropdown on size just above Content inset. There is a list of sizes to select from, and this will adjust your image size(if you use system image). The default size is already set when you added the button on the storyboard to the View Controller.

How do I change the size of an image in Swiftui?

Resizable. To make an image scales to fit the current view, we use the resizable() modifier, which resizes an image to fit available space. We can see the whole image now, but it got stretched out and losing its aspect ratio, which is usually not the behavior we want. The image view resize to fit available space.

What is UIButton Swift?

A control that executes your custom code in response to user interactions.


1 Answers

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

Swift 3:

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]) 

Swift 4:

let button   = UIButton(type: UIButtonType.system) as UIButton // set the frame button.frame = CGRect(x: 100, y: 100, width: 100, height: 50) // add image button.setBackgroundImage(UIImage(named:"SearchIcon"), for: .normal) // button title button.setTitle("Test Button", for: .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.activate([heightContraints,widthContraints,xContraints,yContraints]) 

Swift 4.2:

let button   = UIButton(type: UIButton.ButtonType.system) as UIButton         // set the frame button.frame = CGRect(x: 100, y: 100, width: 100, height: 50)  // add image button.setBackgroundImage(UIImage(named:"SearchIcon"), for: .normal)  // button title button.setTitle("Test Button", for: .normal)  // add action button.addTarget(self, action: #selector(didTapOnTakePhotoButton), for: UIControl.Event.touchUpInside)  button.translatesAutoresizingMaskIntoConstraints = false  // add button on view self.view.addSubview(button)  // all constaints let widthContraints =  NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 200) let heightContraints = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100) let xContraints = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.centerX, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.centerX, multiplier: 1, constant: 0) let yContraints = NSLayoutConstraint(item: button, attribute: NSLayoutConstraint.Attribute.centerY, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.centerY, multiplier: 1, constant: 0) NSLayoutConstraint.activate([heightContraints,widthContraints,xContraints,yContraints]) 
like image 104
Chaudhary Ankit Deshwal Avatar answered Oct 21 '22 06:10

Chaudhary Ankit Deshwal