2017-04-16 83 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)