Dynamically set view top margin and height using autolayout - swift3










0















I am working on a UI where I have 3 labels. All are arranged vertically-



Label 1



Label 2



Label 3



  1. List item At a time any two Label will be shown to user.

  2. For example if Label 1 and Label 3 are shown then I want to shift Label3 up below the label1 and make Label2 height to 0.

    1. If Label2 and Label3 are shown we have to shift both label up and set label1 height to 0.
      So everytime topmst label should have same top margin from superview. It can be either Label1, Label2 or Label3


Should I use stack view to achieve this requirement?



What is the best way to do it using auto layout?










share|improve this question
























  • You should definitely use UIStackView for this. Whichever label you need to eliminate, you just have to make that label hidden. That's it. Every other label will adjust their positioning accordingly.

    – nayem
    Nov 15 '18 at 15:46











  • Yes. It Helped!

    – user10316062
    Nov 15 '18 at 16:40















0















I am working on a UI where I have 3 labels. All are arranged vertically-



Label 1



Label 2



Label 3



  1. List item At a time any two Label will be shown to user.

  2. For example if Label 1 and Label 3 are shown then I want to shift Label3 up below the label1 and make Label2 height to 0.

    1. If Label2 and Label3 are shown we have to shift both label up and set label1 height to 0.
      So everytime topmst label should have same top margin from superview. It can be either Label1, Label2 or Label3


Should I use stack view to achieve this requirement?



What is the best way to do it using auto layout?










share|improve this question
























  • You should definitely use UIStackView for this. Whichever label you need to eliminate, you just have to make that label hidden. That's it. Every other label will adjust their positioning accordingly.

    – nayem
    Nov 15 '18 at 15:46











  • Yes. It Helped!

    – user10316062
    Nov 15 '18 at 16:40













0












0








0








I am working on a UI where I have 3 labels. All are arranged vertically-



Label 1



Label 2



Label 3



  1. List item At a time any two Label will be shown to user.

  2. For example if Label 1 and Label 3 are shown then I want to shift Label3 up below the label1 and make Label2 height to 0.

    1. If Label2 and Label3 are shown we have to shift both label up and set label1 height to 0.
      So everytime topmst label should have same top margin from superview. It can be either Label1, Label2 or Label3


Should I use stack view to achieve this requirement?



What is the best way to do it using auto layout?










share|improve this question
















I am working on a UI where I have 3 labels. All are arranged vertically-



Label 1



Label 2



Label 3



  1. List item At a time any two Label will be shown to user.

  2. For example if Label 1 and Label 3 are shown then I want to shift Label3 up below the label1 and make Label2 height to 0.

    1. If Label2 and Label3 are shown we have to shift both label up and set label1 height to 0.
      So everytime topmst label should have same top margin from superview. It can be either Label1, Label2 or Label3


Should I use stack view to achieve this requirement?



What is the best way to do it using auto layout?







ios autolayout xcode9 swift3.2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 14:50







user10316062

















asked Nov 14 '18 at 21:20









user10316062user10316062

14




14












  • You should definitely use UIStackView for this. Whichever label you need to eliminate, you just have to make that label hidden. That's it. Every other label will adjust their positioning accordingly.

    – nayem
    Nov 15 '18 at 15:46











  • Yes. It Helped!

    – user10316062
    Nov 15 '18 at 16:40

















  • You should definitely use UIStackView for this. Whichever label you need to eliminate, you just have to make that label hidden. That's it. Every other label will adjust their positioning accordingly.

    – nayem
    Nov 15 '18 at 15:46











  • Yes. It Helped!

    – user10316062
    Nov 15 '18 at 16:40
















You should definitely use UIStackView for this. Whichever label you need to eliminate, you just have to make that label hidden. That's it. Every other label will adjust their positioning accordingly.

– nayem
Nov 15 '18 at 15:46





You should definitely use UIStackView for this. Whichever label you need to eliminate, you just have to make that label hidden. That's it. Every other label will adjust their positioning accordingly.

– nayem
Nov 15 '18 at 15:46













Yes. It Helped!

– user10316062
Nov 15 '18 at 16:40





Yes. It Helped!

– user10316062
Nov 15 '18 at 16:40












1 Answer
1






active

oldest

votes


















0














You can group your different scenarios constraints in a IBOutletCollection(NSLayoutConstraint) property (one per scenario/ group of constraints) like:



@property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *scenarioNConstraints;


And then based on your conditions you can Activate or Deactivate the desire constraints by doing something like



for (NSLayoutConstraint *constraint in scenarioNConstraints) 
[constraint setActive:YES]; //or [constraint setActive:NO];






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308895%2fdynamically-set-view-top-margin-and-height-using-autolayout-swift3%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can group your different scenarios constraints in a IBOutletCollection(NSLayoutConstraint) property (one per scenario/ group of constraints) like:



    @property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *scenarioNConstraints;


    And then based on your conditions you can Activate or Deactivate the desire constraints by doing something like



    for (NSLayoutConstraint *constraint in scenarioNConstraints) 
    [constraint setActive:YES]; //or [constraint setActive:NO];






    share|improve this answer



























      0














      You can group your different scenarios constraints in a IBOutletCollection(NSLayoutConstraint) property (one per scenario/ group of constraints) like:



      @property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *scenarioNConstraints;


      And then based on your conditions you can Activate or Deactivate the desire constraints by doing something like



      for (NSLayoutConstraint *constraint in scenarioNConstraints) 
      [constraint setActive:YES]; //or [constraint setActive:NO];






      share|improve this answer

























        0












        0








        0







        You can group your different scenarios constraints in a IBOutletCollection(NSLayoutConstraint) property (one per scenario/ group of constraints) like:



        @property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *scenarioNConstraints;


        And then based on your conditions you can Activate or Deactivate the desire constraints by doing something like



        for (NSLayoutConstraint *constraint in scenarioNConstraints) 
        [constraint setActive:YES]; //or [constraint setActive:NO];






        share|improve this answer













        You can group your different scenarios constraints in a IBOutletCollection(NSLayoutConstraint) property (one per scenario/ group of constraints) like:



        @property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *scenarioNConstraints;


        And then based on your conditions you can Activate or Deactivate the desire constraints by doing something like



        for (NSLayoutConstraint *constraint in scenarioNConstraints) 
        [constraint setActive:YES]; //or [constraint setActive:NO];







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 15:54









        Fede HenzeFede Henze

        535615




        535615





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308895%2fdynamically-set-view-top-margin-and-height-using-autolayout-swift3%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党