如何设置带有图像的 UIButton 的高度和宽度
Posted
技术标签:
【中文标题】如何设置带有图像的 UIButton 的高度和宽度【英文标题】:How do you set the height and width of a UIButton with image 【发布时间】:2016-05-27 03:11:25 【问题描述】:这让我发疯了。我到处寻找,无法弄清楚。看看吧……
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
我还在学习 ios。如何使用图像设置此 UIButton 的高度和宽度。我尝试过的一切都给了我一个错误或只是没有工作。我仍在试图围绕 translatesAutoresizingMaskIntoConstraints 所做的事情进行思考。我只是想让按钮在它所在的位置,但改变它的大小(高度和宽度)。
提前致谢
编辑:我已经把这段代码改成了这个
// 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)
我想将按钮定位到窗口底部和右边距。我还想将图像尺寸设置为高 50 x 宽 50。我将如何做到这一点?
编辑 2: 我认为您必须使用自动布局才能这样做,但有人可以告诉我如何操作。我所做的一切都没有奏效。
【问题讨论】:
在背景图像中设置图像开始正常工作 你这是什么意思? 您必须将该图像设置为按钮背景图像。 很抱歉你能给我一个代码示例。我尝试的是给我一个错误。谢谢 我使用了 findMeButton.setBackgroundImage(UIImage(named: "locateMe"), forState: UIControlState.Normal) 但它仍然不允许我更改大小。 【参考方案1】:所以我在这里写了一个代码来添加视图上的按钮。
斯威夫特 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])
斯威夫特 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])
【讨论】:
是的,我知道该怎么做,但我想使用自动布局将其锚定到底部和右侧边距,但是当我这样做时,它不会让我设置图像的大小。 好吧,所以你需要为底部、水平居中、宽度和高度添加自动布局 你能用代码告诉我吗?我也试过了,它给了我错误。抱歉,我是这方面的新手。 也请注明 是的,我试过了,但它说我需要 15 声望才能这样做。一旦我得到15,我会回来这样做。对不起【参考方案2】:斯威夫特 3
findMeButton.frame.size = CGSize(width, height)
【讨论】:
【参考方案3】:只需设置按钮大小
findMeButton.frame.size = CGSizeMake(width, height)
或者你可以用
指定按钮的位置和大小findMeButton.frame = CGRectMake(x, y, width, height)
【讨论】:
我刚试了,还是不行。我已编辑以显示我的更改。 CGSizeMake for Swift 3: findMeButton.frame.size = CGSize(width: 50, height: 50)【参考方案4】:也可以通过以下方式设置按钮布局...
// Create button with half the size of and centered in parent view
parentView.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
button.widthAnchor.constraint(equalTo: parentView.widthAnchor, multiplier: 0.5).isActive = true
button.heightAnchor.constraint(equalTo: parentView.heightAnchor, multiplier: 0.5).isActive = true
button.centerXAnchor.constraint(equalTo: parentView.centerXAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: parentView.centerYAnchor).isActive = true
【讨论】:
【参考方案5】:Swift 5:我使用 NSLayoutConstraints 来设置 UIButton 的宽度和高度。无需设置button.frame
。
let playImage = UIImage(named: "Play")
playButton = UIButton(type: UIButton.ButtonType.custom) as UIButton
playButton.setBackgroundImage(playImage, for: .normal)
playButton.addTarget(self, action: #selector(playBtnTapped), for: .touchUpInside)
playButton.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(playButton)
NSLayoutConstraint.activate([
playButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
playButton.centerYAnchor.constraint(equalTo: view.centerYAnchor),
playButton.widthAnchor.constraint(equalToConstant: 120),
playButton.heightAnchor.constraint(equalTo: playButton.widthAnchor)
])
【讨论】:
以上是关于如何设置带有图像的 UIButton 的高度和宽度的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 AutoLayout 配置视图宽度固定宽度和可变高度