I have the following code that seems to work fine but suffers from constraints problems:
UIView container = something;
UIView bottomAnchorView = previewViewInSameContainer;
var itemAsAWhole = new UIStackView { TranslatesAutoresizingMaskIntoConstraints = false, Axis = UILayoutConstraintAxis.Horizontal, Distribution = UIStackViewDistribution.Fill, Alignment = UIStackViewAlignment.Fill };
var itemTitle = new UILabel { TranslatesAutoresizingMaskIntoConstraints = false, Text = title, Font = FontHelpers.GenerateUIFont("Ubuntu-Light", UIFontTextStyle.Body) };
itemTitle.Tag = itemTitle.GenerateViewTag();
var scoreHStack = new UIStackView { TranslatesAutoresizingMaskIntoConstraints = false, Axis = UILayoutConstraintAxis.Horizontal, Distribution = UIStackViewDistribution.Fill, Alignment = UIStackViewAlignment.Fill };
var itemScore = new UILabel { TranslatesAutoresizingMaskIntoConstraints = false, Text = score == -1 ? "-" : score.ToString(), MinimumFontSize = 8, TextColor = Utilities.GetNoteColorFromScoreValue(score), Font = FontHelpers.GenerateUIFont("Ubuntu-Medium", UIFontTextStyle.Title2) };
itemScore.Tag = itemScore.GenerateViewTag();
var maxScore = new UILabel { TranslatesAutoresizingMaskIntoConstraints = false, Text = "/10", Font = FontHelpers.GenerateUIFont("Ubuntu-Light", UIFontTextStyle.Body) };
scoreHStack.AddArrangedSubview(itemScore);
scoreHStack.AddArrangedSubview(maxScore);
itemAsAWhole.AddArrangedSubview(itemTitle);
itemAsAWhole.AddArrangedSubview(scoreHStack);
container.AddSubview(itemAsAWhole);
itemAsAWhole.TopAnchor.ConstraintEqualTo(bottomAnchorView.BottomAnchor).Active = true;
itemAsAWhole.LeadingAnchor.ConstraintEqualTo(container.LeadingAnchor).Active = true;
itemAsAWhole.TrailingAnchor.ConstraintEqualTo(container.TrailingAnchor).Active = true;
At runtime:
- The itemTitle UILabel has ambiguous Width.
- The scoreHStack UIStackView has ambiguous Width and horizontal position.
- The itemScore UILabel has ambiguous Width.
- the maxScore UILabel has ambiguous Width and horizontal position.
What Am I missing in the code to make those warnings disappear?
EDIT : Here is a screenshot of what it does:
EDIT 2 : Problem solved with the help of another answer. Thank you @DonMag ;)
var itemScore = new UILabel { TranslatesAutoresizingMaskIntoConstraints = false, Text = score == -1 ? "-" : score.ToString(), MinimumFontSize = 8, TextColor = Utilities.GetNoteColorFromScoreValue(score), Font = FontHelpers.GenerateUIFont("Ubuntu-Medium", UIFontTextStyle.Title2) };
itemScore.SetContentHuggingPriority(1000f, UILayoutConstraintAxis.Horizontal);
itemScore.SetContentCompressionResistancePriority(1000f, UILayoutConstraintAxis.Horizontal);
itemScore.Tag = itemScore.GenerateViewTag();
var maxScore = new UILabel { TranslatesAutoresizingMaskIntoConstraints = false, Text = "/10", Font = FontHelpers.GenerateUIFont("Ubuntu-Light", UIFontTextStyle.Body) };
maxScore.SetContentHuggingPriority(1000f, UILayoutConstraintAxis.Horizontal);
maxScore.SetContentCompressionResistancePriority(1000f, UILayoutConstraintAxis.Horizontal);
scoreHStack.AddArrangedSubview(itemScore);
scoreHStack.AddArrangedSubview(maxScore);
itemAsAWhole.AddArrangedSubview(itemTitle);