2017-04-16 8 views
1

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の

使用setImage?またはUIButton? –

+0

そのUIButton? – john

+0

実際に画像をボタンに設定したコードでは表示されません。 imageEdgeInsetsはボタンの画像にのみ適用されます。 – DisableR

答えて

0

imageEdgeInsetsは画像を配置するためのものであり、背景画像ではない!代わりにsetBackgroundImage

0

AnimatableButtonがなぜボタンの内側にボタンを追加したいのですか?私はあなたの目的が何であるかわからないが、試してみるかもしれない。 Settingsbutton.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)

関連する問題