I'm currently trying to use Auto-Layout constraints to vertically position a UIButton's TitleLabel below its ImageView. However, I can't seem to figure out how to remove the default constraints from the UIButton. Because of this, the final set of constraints is ambiguous. How can I remove the default constraints and make my constraints work? Also, I keep seeing people using insets to accomplish this layout. Why is this? Below is my code:
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();
}
}
Generated error:
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']>