簡體   English   中英

如何刪除由UIButton設置的默認約束?

[英]How to remove default constraints set by UIButton?

我目前正在嘗試使用自動布局約束將UIButton的TitleLabel垂直放置在其ImageView下方。 但是,我似乎無法弄清楚如何從UIButton中刪除默認約束。 因此,最后一組約束是模棱兩可的。 如何刪除默認約束並使約束生效? 此外,我一直看到人們使用插圖來完成此布局。 為什么是這樣? 下面是我的代碼:

 public class DrawerButton : UIButton
{
    public override UIButtonType ButtonType { get; } = UIButtonType.Custom;

    public override void UpdateConstraints()
    {
        this.AddConstraints(
        ImageView.WithSameCenterX(this),

        TitleLabel.Below(ImageView).Plus(10),
        TitleLabel.WithSameCenterX(this)

        );

        base.UpdateConstraints();
    }

    public override void LayoutSubviews()
    {
        this.TranslatesAutoresizingMaskIntoConstraints = false;
        this.SubviewsDoNotTranslateAutoresizingMaskIntoConstraints();
        base.LayoutSubviews();
    }
}

產生的錯誤:

2016-09-28 22:31:08.163 XXXXXXXXXXX[80361:7012489] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x79292ca0 V:[UIImageView:0x78efb2c0]-(10)-[UIButtonLabel:0x78e00dc0'TEST']>",
    "<NSLayoutConstraint:0x792bb250 UIImageView:0x78efb2c0.centerY == XXXXXXXXX_iOS_Widgets_DrawerButton:0x793ddec0'TEST'.centerY>",
    "<NSLayoutConstraint:0x792c9310 UIButtonLabel:0x78e00dc0'TEST'.centerY == XXXXXXXXX_iOS_Widgets_DrawerButton:0x793ddec0'TEST'.centerY>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x79292ca0 V:[UIImageView:0x78efb2c0]-(10)-[UIButtonLabel:0x78e00dc0'TEST']>

這是一個擴展(Swift3),可將UIButton的titleLabel垂直放置在imageView下方。

extension UIButton {
    func centerButtonAndImageVertically(verticalSpacing: CGFloat = 0) {

        self.contentVerticalAlignment = .center
        self.contentHorizontalAlignment = .center

        let imageWidth = self.imageView?.frame.size.width ?? 0
        let imageHeight = self.imageView?.frame.size.height ?? 0
        let titleHeight = self.titleLabel?.frame.size.height ?? 0
        let titleWidth = self.titleLabel?.frame.size.width ?? 0

        self.imageEdgeInsets = UIEdgeInsets(top: 0, left: titleWidth, bottom: titleHeight+verticalSpacing*0.5, right: 0)
        self.titleEdgeInsets = UIEdgeInsets(top: imageHeight+verticalSpacing*0.5, left: 0, bottom: 0, right: imageWidth)
    }
}

舉例來說,我在以下ViewController代碼中使用擴展名來獲取圖像中的結果:

class ViewController: UIViewController {

    @IBOutlet weak var smallButton: UIButton!
    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var bigButton: UIButton!
    @IBOutlet weak var imageOnlyButton: UIButton!
    @IBOutlet weak var titleOnlyButton: UIButton!

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        smallButton.centerButtonAndImageVertically()
        button.centerButtonAndImageVertically()
        bigButton.centerButtonAndImageVertically(verticalSpacing: 10)
        imageOnlyButton.centerButtonAndImageVertically()
        titleOnlyButton.centerButtonAndImageVertically()
    }

}

例

面對相同的問題,沒有找到答案,但是對我來說有效的解決方案是在我的子類中重寫此UIButton的方法:

override func imageRect(forContentRect contentRect: CGRect) -> CGRect {

    var imageFrame: CGRect = super.imageRect(forContentRect: contentRect)

    imageFrame.origin = CGPoint(x: bounds.midX - imageFrame.width / 2, y: bounds.midY - imageFrame.height)

    return imageFrame
}

override func titleRect(forContentRect contentRect: CGRect) -> CGRect {

    var titleFrame: CGRect = super.titleRect(forContentRect: contentRect)

    titleFrame.origin = CGPoint(x: bounds.midX - titleFrame.width / 2, y: bounds.midY)

    return titleFrame
}

也許會幫助某人

暫無
暫無

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

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