簡體   English   中英

UIButton UIEdgeInsets不起作用

[英]UIButton UIEdgeInsets doesn't work

因此,我查看了Stack Overflow上的所有項目,這些項目用於在UIButtonUIImages添加邊緣插圖,但沒有一個起作用。 當我構建項目時,圖像只是通過按鈕的邊界設置的-它沒有設置為插圖,我也不知道為什么。 這是代碼:

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用於定位圖像,而不是背景圖像!

使用setImage代替setBackgroundImage

如果AnimatableButton是UIButton,為什么要在button中添加button? 我確實不知道您的目的是什么,但是您可以嘗試Settingsbutton.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM