小编Chr*_*ris的帖子

如何使用图像设置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
Run Code Online (Sandbox Code Playgroud)

我还在学习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)
Run Code Online (Sandbox Code Playgroud)

我想将按钮定位到窗口底部和右边距.我还想将图像尺寸设置为高度50 x宽度50.我将如何实现这一目标?

编辑2:我认为你必须使用自动布局这样做,但有人可以告诉我如何.我所做的一切都没有奏效.

uibutton swift

15
推荐指数
3
解决办法
4万
查看次数

标签 统计

swift ×1

uibutton ×1