Dynamically set view top margin and height using autolayout - swift3
I am working on a UI where I have 3 labels. All are arranged vertically-
Label 1
Label 2
Label 3
- List item At a time any two Label will be shown to user.
- 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.
- 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
- If Label2 and Label3 are shown we have to shift both label up and set label1 height to 0.
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
add a comment |
I am working on a UI where I have 3 labels. All are arranged vertically-
Label 1
Label 2
Label 3
- List item At a time any two Label will be shown to user.
- 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.
- 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
- If Label2 and Label3 are shown we have to shift both label up and set label1 height to 0.
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
You should definitely useUIStackView
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
add a comment |
I am working on a UI where I have 3 labels. All are arranged vertically-
Label 1
Label 2
Label 3
- List item At a time any two Label will be shown to user.
- 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.
- 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
- If Label2 and Label3 are shown we have to shift both label up and set label1 height to 0.
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
I am working on a UI where I have 3 labels. All are arranged vertically-
Label 1
Label 2
Label 3
- List item At a time any two Label will be shown to user.
- 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.
- 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
- If Label2 and Label3 are shown we have to shift both label up and set label1 height to 0.
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
ios autolayout xcode9 swift3.2
edited Nov 15 '18 at 14:50
user10316062
asked Nov 14 '18 at 21:20
user10316062user10316062
14
14
You should definitely useUIStackView
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
add a comment |
You should definitely useUIStackView
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
add a comment |
1 Answer
1
active
oldest
votes
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];
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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];
add a comment |
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];
add a comment |
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];
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];
answered Nov 15 '18 at 15:54
Fede HenzeFede Henze
535615
535615
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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