Forum Xamarin.iOS

How to add bottom constrain should be constamnt

AmejackAmejack USMember ✭✭✭
How to add bottom constrain and itb should be constant.. At the bottom of the view
Tagged:

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    If you want to add it on the storyboard try this approach:
    firstly, select the control and then click the top right button to add a bottom constraint

    If you want to add constraints using code behind, here it is:

    UILabel label = new UILabel();
    label.Text = "Test";
    label.TranslatesAutoresizingMaskIntoConstraints = false;
    View.AddSubview(label);
    
    // Bottom constraint
    label.BottomAnchor.ConstraintEqualTo(View.BottomAnchor).Active = true;
    
  • AmejackAmejack USMember ✭✭✭

    (this.View.BottomAnchor, constant: -100)

    this constant is changes iphone se ..

  • LandLuLandLu Member, Xamarin Team Xamurai

    @akk Could you please make it clear?

  • AmejackAmejack USMember ✭✭✭

    bottom spacing should should be 100..

  • LandLuLandLu Member, Xamarin Team Xamurai

    If you use code to set the constraint, we need to set the constant of the bottom constraint to a negative value:

     label.BottomAnchor.ConstraintEqualTo(View.BottomAnchor, -100).Active = true;
    

    Because the value of the constant is got by label's bottom minus view's bottom.
    This equals to:

    View.BottomAnchor.ConstraintEqualTo(label.BottomAnchor, 100).Active = true;
    
  • AmejackAmejack USMember ✭✭✭

    View.BottomAnchor.ConstraintEqualTo(label.BottomAnchor, 100).Active = true;

    i did this above code ..but iphoneSE simulator its changing its position

  • LandLuLandLu Member, Xamarin Team Xamurai

    What do you mean "but iphoneSE simulator its changing its position"?
    We need to add other constraints to make it in the right position.
    For example:

    UILabel label = new UILabel();
    label.Text = "Test";
    label.TranslatesAutoresizingMaskIntoConstraints = false;
    View.AddSubview(label);
    
    // Bottom constraint
    label.BottomAnchor.ConstraintEqualTo(View.BottomAnchor, -100).Active = true;
    // Center constraint
    label.CenterXAnchor.ConstraintEqualTo(View.CenterXAnchor).Active = true;
    

    This label will be displayed at the center of the X axis and its size will depends on its content.

Sign In or Register to comment.