如何使用图像设置UIButton的高度和宽度

时间:2016-05-27 03:11:25

标签: swift uibutton

这让我疯了。我到处寻找,无法弄清楚这一点。看看......

    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:我认为您必须使用自动布局才能这样做但有人可以告诉我如何操作。我所做的一切都没有奏效。

5 个答案:

答案 0 :(得分:12)

所以在这里我写了一个代码来在视图上添加按钮。

斯威夫特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])

答案 1 :(得分:11)

Swift 3

findMeButton.frame.size = CGSize(width, height)

答案 2 :(得分:6)

只需使用

设置按钮大小
findMeButton.frame.size = CGSizeMake(width, height)

或者您可以使用

指定按钮位置和大小
findMeButton.frame = CGRectMake(x, y, width, height)

答案 3 :(得分:2)

也可以通过执行以下操作来设置按钮布局......

    // 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

答案 4 :(得分:0)

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