2017-04-16 32 views
1

所以我看了看堆栈溢出的所有项目在UIButton加入边插图为UIImages但他们没有工作。当我构建项目时,图像只是通过按钮的边界设置 - 它没有设置为插入,我不知道为什么。这里是代码:的UIButton UIEdgeInsets不起作用

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{ 

    } 
+0

unbutton?还是UIButton? –

+0

其UIButton? – john

+0

我没有在您的代码中看到您实际将图像设置为按钮的位置。 imageEdgeInsets只适用于按钮的图像。 – DisableR

回答

0

imageEdgeInsets是图像不是背景图像的定位!

使用setImage而不是setBackgroundImage

0

如果AnimatableButton是一个UIButton为什么你想添加按钮里面的按钮?我真的不知道什么是你的目的,但你可以尝试 Settingsbutton.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)