0

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:

enter image description here

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);
  • Could you provide a screen shot of the effect that you want to achieve ? – Lucas Zhang Nov 10 '20 at 13:20
  • @LucasZhang-MSFT done. The result is OK, I just have those warnings in the application output. –  Nov 10 '20 at 13:51
  • I tested your code in my project but it works without any warning . So could you share your sample so that I can test it on my side . – Lucas Zhang Nov 10 '20 at 14:29
  • I'm sorry the warnings are visible only in the RevealApp debugger. –  Nov 10 '20 at 14:55
  • @OlivierMATROT - possibly due to how you're adding `itemAsAWhole` to your view (and how you're constraining it)? – DonMag Nov 10 '20 at 17:39
  • @DonMag itemAsAWhole is indeed added to a container view and constrained to the leading and trailing of this container. The TopAnchor is constrained to the bottom of the previous view in this container. –  Nov 20 '20 at 09:56

0 Answers0