简体   繁体   English

UIButton UIEdgeInsets不起作用

[英]UIButton UIEdgeInsets doesn't work

So I've looked at all projects on Stack Overflow for adding edge insets for UIImages in a UIButton but none of them worked. 因此,我查看了Stack Overflow上的所有项目,这些项目用于在UIButtonUIImages添加边缘插图,但没有一个起作用。 When I build the project, the image is just set through the bounds of the button - it's not set to the insets and I don't know why. 当我构建项目时,图像只是通过按钮的边界设置的-它没有设置为插图,我也不知道为什么。 Here is the code: 这是代码:

let Settingsbutton = AnimatableButton(frame: CGRect(x: 23, y: 30, width: 40, height: 40))
    Settingsbutton.layer.cornerRadius = Settingsbutton.frame.size.height / 2
    Settingsbutton.layer.backgroundColor = UIColor.white
    let roundedButton3 = UIButton(type: .custom)
    roundedButton3.frame = Settingsbutton.frame
    let _border3 = CAShapeLayer()
    _border3.path = UIBezierPath(roundedRect: roundedButton3.bounds, cornerRadius:roundedButton3.frame.size.width/2).cgPath
    _border3.frame = roundedButton3.bounds
    _border3.strokeColor = UIColor.white.cgColor
    _border3.fillColor = UIColor.clear.cgColor
    _border3.lineWidth = 3.0

    Settingsbutton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)
    Settingsbutton.contentVerticalAlignment = .fill
    Settingsbutton.contentHorizontalAlignment = .fill
    Settingsbutton.layer.addSublayer(_border3)
    self.view.addSubview(Settingsbutton)
    self.view.bringSubview(toFront: Settingsbutton)

    if PFUser.current() == nil{
        Settingsbutton.setBackgroundImage(UIImage(named: "padlock"), for: .normal)

    }else{

    }

imageEdgeInsets are for positioning the image NOT the background image! imageEdgeInsets用于定位图像,而不是背景图像!

Use setImage instead of setBackgroundImage 使用setImage代替setBackgroundImage

If AnimatableButton is a UIButton why you want to add button inside button? 如果AnimatableButton是UIButton,为什么要在button中添加button? I don't know really what's your purpose but you might try Settingsbutton.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10) 我确实不知道您的目的是什么,但是您可以尝试Settingsbutton.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM