如何删除UIButton设置的默认约束?

时间:2016-09-29 04:43:51

标签: c# ios xamarin autolayout

我目前正在尝试使用自动布局约束来将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']>

2 个答案:

答案 0 :(得分:1)

这是一个扩展名(Swift3),它将垂直放置在imageView下面的UIButton的titleLabel。

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()
    }

}

example

答案 1 :(得分:1)

面对同样的问题,没有找到答案,但是对我来说有效的解决方案是在我的子类中重写此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
}

也许会帮助某人