Change height of one of multiple fields in stack view
In my code I have a stack view that initially has 1 element PhoneNumberField
. Another PhoneNumberFields
can be added dynamically in the runtime:
@IBAction func addAlternatePhoneNumberAction(_ sender: UIButton) {
let alternateNumberView = PhoneNumberField()
...
phoneNumberStackView.addArrangedSubview(alternateNumberView)
}
This is what xib for PhoneNumberField
looks like:
The problem is that I would like to be able to dynamically hide the 'Name for other phone' field based on the content of 'Mobile combobox'. When I set the 'isHidden' parameter everything works as expected, the only problem is that the PhoneNumberField
height stays the same. I would like it to shrink when the 'Name for other phone' field is hidden.
I tried doing it using the outlet for height constant for otherNumberNameField
in the PhoneNumberField.swift
file but the problem is that in that case all of the PhoneNumberFields
in the stack view have the size of the first field.
What would be the correct solution for this?
edit: In addition to the answer below: I had to set the distribution for the phoneNumberStackView to equal spacing
. Worked like a charm.
swift xcode autolayout
add a comment |
In my code I have a stack view that initially has 1 element PhoneNumberField
. Another PhoneNumberFields
can be added dynamically in the runtime:
@IBAction func addAlternatePhoneNumberAction(_ sender: UIButton) {
let alternateNumberView = PhoneNumberField()
...
phoneNumberStackView.addArrangedSubview(alternateNumberView)
}
This is what xib for PhoneNumberField
looks like:
The problem is that I would like to be able to dynamically hide the 'Name for other phone' field based on the content of 'Mobile combobox'. When I set the 'isHidden' parameter everything works as expected, the only problem is that the PhoneNumberField
height stays the same. I would like it to shrink when the 'Name for other phone' field is hidden.
I tried doing it using the outlet for height constant for otherNumberNameField
in the PhoneNumberField.swift
file but the problem is that in that case all of the PhoneNumberFields
in the stack view have the size of the first field.
What would be the correct solution for this?
edit: In addition to the answer below: I had to set the distribution for the phoneNumberStackView to equal spacing
. Worked like a charm.
swift xcode autolayout
Try edited answer
– Robert Dresler
Nov 20 '18 at 15:43
add a comment |
In my code I have a stack view that initially has 1 element PhoneNumberField
. Another PhoneNumberFields
can be added dynamically in the runtime:
@IBAction func addAlternatePhoneNumberAction(_ sender: UIButton) {
let alternateNumberView = PhoneNumberField()
...
phoneNumberStackView.addArrangedSubview(alternateNumberView)
}
This is what xib for PhoneNumberField
looks like:
The problem is that I would like to be able to dynamically hide the 'Name for other phone' field based on the content of 'Mobile combobox'. When I set the 'isHidden' parameter everything works as expected, the only problem is that the PhoneNumberField
height stays the same. I would like it to shrink when the 'Name for other phone' field is hidden.
I tried doing it using the outlet for height constant for otherNumberNameField
in the PhoneNumberField.swift
file but the problem is that in that case all of the PhoneNumberFields
in the stack view have the size of the first field.
What would be the correct solution for this?
edit: In addition to the answer below: I had to set the distribution for the phoneNumberStackView to equal spacing
. Worked like a charm.
swift xcode autolayout
In my code I have a stack view that initially has 1 element PhoneNumberField
. Another PhoneNumberFields
can be added dynamically in the runtime:
@IBAction func addAlternatePhoneNumberAction(_ sender: UIButton) {
let alternateNumberView = PhoneNumberField()
...
phoneNumberStackView.addArrangedSubview(alternateNumberView)
}
This is what xib for PhoneNumberField
looks like:
The problem is that I would like to be able to dynamically hide the 'Name for other phone' field based on the content of 'Mobile combobox'. When I set the 'isHidden' parameter everything works as expected, the only problem is that the PhoneNumberField
height stays the same. I would like it to shrink when the 'Name for other phone' field is hidden.
I tried doing it using the outlet for height constant for otherNumberNameField
in the PhoneNumberField.swift
file but the problem is that in that case all of the PhoneNumberFields
in the stack view have the size of the first field.
What would be the correct solution for this?
edit: In addition to the answer below: I had to set the distribution for the phoneNumberStackView to equal spacing
. Worked like a charm.
swift xcode autolayout
swift xcode autolayout
edited Nov 20 '18 at 18:18
Marta Panuszewska
asked Nov 20 '18 at 14:26
Marta PanuszewskaMarta Panuszewska
187
187
Try edited answer
– Robert Dresler
Nov 20 '18 at 15:43
add a comment |
Try edited answer
– Robert Dresler
Nov 20 '18 at 15:43
Try edited answer
– Robert Dresler
Nov 20 '18 at 15:43
Try edited answer
– Robert Dresler
Nov 20 '18 at 15:43
add a comment |
1 Answer
1
active
oldest
votes
First, create StackView.
Don't set its height constraint, just set top, leading, trailing and bottom constraints.
Bottom constraint set equal to Error label top constraint.
Then set its distribution to Fill Equally.
Now put first two Views into one view and put this view together with OtherNumberField view to this StackView.
So now your hierarchy should look like this:
Now when you hide one view from StackView, StackView will be smaller because you didn't set its height.
It doesn't seem to be working. ThePhoneNumberField
doesn't change size after hiding the field.
– Marta Panuszewska
Nov 20 '18 at 16:13
Yeah, the `SubStackView' height is set to be in certain ratio to the error label so how could it change to be smaller?
– Marta Panuszewska
Nov 20 '18 at 16:27
@MartaPanuszewska because items in SubStackView are fill equally. If you hide item in stack view, all other items will try to take place of this hidden item equally
– Robert Dresler
Nov 20 '18 at 16:45
@MartaPanuszewska ok, I edited my answer, try it now. It should be easier way
– Robert Dresler
Nov 20 '18 at 16:52
Robert, I might have not explained myself correctly. When I hide theOtherNumberField
I don't need the theView
(MobileComboBox
+NumberField
) to become bigger. I need the wholePhoneNumberField
(so, the stack view) to become smaller. So I would like thePhoneNumberField
to change its size from approximately 134 points (2 fields + label) to 78 points (1 field + label).
– Marta Panuszewska
Nov 20 '18 at 17:06
|
show 2 more comments
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%2f53395160%2fchange-height-of-one-of-multiple-fields-in-stack-view%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
First, create StackView.
Don't set its height constraint, just set top, leading, trailing and bottom constraints.
Bottom constraint set equal to Error label top constraint.
Then set its distribution to Fill Equally.
Now put first two Views into one view and put this view together with OtherNumberField view to this StackView.
So now your hierarchy should look like this:
Now when you hide one view from StackView, StackView will be smaller because you didn't set its height.
It doesn't seem to be working. ThePhoneNumberField
doesn't change size after hiding the field.
– Marta Panuszewska
Nov 20 '18 at 16:13
Yeah, the `SubStackView' height is set to be in certain ratio to the error label so how could it change to be smaller?
– Marta Panuszewska
Nov 20 '18 at 16:27
@MartaPanuszewska because items in SubStackView are fill equally. If you hide item in stack view, all other items will try to take place of this hidden item equally
– Robert Dresler
Nov 20 '18 at 16:45
@MartaPanuszewska ok, I edited my answer, try it now. It should be easier way
– Robert Dresler
Nov 20 '18 at 16:52
Robert, I might have not explained myself correctly. When I hide theOtherNumberField
I don't need the theView
(MobileComboBox
+NumberField
) to become bigger. I need the wholePhoneNumberField
(so, the stack view) to become smaller. So I would like thePhoneNumberField
to change its size from approximately 134 points (2 fields + label) to 78 points (1 field + label).
– Marta Panuszewska
Nov 20 '18 at 17:06
|
show 2 more comments
First, create StackView.
Don't set its height constraint, just set top, leading, trailing and bottom constraints.
Bottom constraint set equal to Error label top constraint.
Then set its distribution to Fill Equally.
Now put first two Views into one view and put this view together with OtherNumberField view to this StackView.
So now your hierarchy should look like this:
Now when you hide one view from StackView, StackView will be smaller because you didn't set its height.
It doesn't seem to be working. ThePhoneNumberField
doesn't change size after hiding the field.
– Marta Panuszewska
Nov 20 '18 at 16:13
Yeah, the `SubStackView' height is set to be in certain ratio to the error label so how could it change to be smaller?
– Marta Panuszewska
Nov 20 '18 at 16:27
@MartaPanuszewska because items in SubStackView are fill equally. If you hide item in stack view, all other items will try to take place of this hidden item equally
– Robert Dresler
Nov 20 '18 at 16:45
@MartaPanuszewska ok, I edited my answer, try it now. It should be easier way
– Robert Dresler
Nov 20 '18 at 16:52
Robert, I might have not explained myself correctly. When I hide theOtherNumberField
I don't need the theView
(MobileComboBox
+NumberField
) to become bigger. I need the wholePhoneNumberField
(so, the stack view) to become smaller. So I would like thePhoneNumberField
to change its size from approximately 134 points (2 fields + label) to 78 points (1 field + label).
– Marta Panuszewska
Nov 20 '18 at 17:06
|
show 2 more comments
First, create StackView.
Don't set its height constraint, just set top, leading, trailing and bottom constraints.
Bottom constraint set equal to Error label top constraint.
Then set its distribution to Fill Equally.
Now put first two Views into one view and put this view together with OtherNumberField view to this StackView.
So now your hierarchy should look like this:
Now when you hide one view from StackView, StackView will be smaller because you didn't set its height.
First, create StackView.
Don't set its height constraint, just set top, leading, trailing and bottom constraints.
Bottom constraint set equal to Error label top constraint.
Then set its distribution to Fill Equally.
Now put first two Views into one view and put this view together with OtherNumberField view to this StackView.
So now your hierarchy should look like this:
Now when you hide one view from StackView, StackView will be smaller because you didn't set its height.
edited Nov 20 '18 at 20:33
answered Nov 20 '18 at 15:00


Robert DreslerRobert Dresler
5,6701526
5,6701526
It doesn't seem to be working. ThePhoneNumberField
doesn't change size after hiding the field.
– Marta Panuszewska
Nov 20 '18 at 16:13
Yeah, the `SubStackView' height is set to be in certain ratio to the error label so how could it change to be smaller?
– Marta Panuszewska
Nov 20 '18 at 16:27
@MartaPanuszewska because items in SubStackView are fill equally. If you hide item in stack view, all other items will try to take place of this hidden item equally
– Robert Dresler
Nov 20 '18 at 16:45
@MartaPanuszewska ok, I edited my answer, try it now. It should be easier way
– Robert Dresler
Nov 20 '18 at 16:52
Robert, I might have not explained myself correctly. When I hide theOtherNumberField
I don't need the theView
(MobileComboBox
+NumberField
) to become bigger. I need the wholePhoneNumberField
(so, the stack view) to become smaller. So I would like thePhoneNumberField
to change its size from approximately 134 points (2 fields + label) to 78 points (1 field + label).
– Marta Panuszewska
Nov 20 '18 at 17:06
|
show 2 more comments
It doesn't seem to be working. ThePhoneNumberField
doesn't change size after hiding the field.
– Marta Panuszewska
Nov 20 '18 at 16:13
Yeah, the `SubStackView' height is set to be in certain ratio to the error label so how could it change to be smaller?
– Marta Panuszewska
Nov 20 '18 at 16:27
@MartaPanuszewska because items in SubStackView are fill equally. If you hide item in stack view, all other items will try to take place of this hidden item equally
– Robert Dresler
Nov 20 '18 at 16:45
@MartaPanuszewska ok, I edited my answer, try it now. It should be easier way
– Robert Dresler
Nov 20 '18 at 16:52
Robert, I might have not explained myself correctly. When I hide theOtherNumberField
I don't need the theView
(MobileComboBox
+NumberField
) to become bigger. I need the wholePhoneNumberField
(so, the stack view) to become smaller. So I would like thePhoneNumberField
to change its size from approximately 134 points (2 fields + label) to 78 points (1 field + label).
– Marta Panuszewska
Nov 20 '18 at 17:06
It doesn't seem to be working. The
PhoneNumberField
doesn't change size after hiding the field.– Marta Panuszewska
Nov 20 '18 at 16:13
It doesn't seem to be working. The
PhoneNumberField
doesn't change size after hiding the field.– Marta Panuszewska
Nov 20 '18 at 16:13
Yeah, the `SubStackView' height is set to be in certain ratio to the error label so how could it change to be smaller?
– Marta Panuszewska
Nov 20 '18 at 16:27
Yeah, the `SubStackView' height is set to be in certain ratio to the error label so how could it change to be smaller?
– Marta Panuszewska
Nov 20 '18 at 16:27
@MartaPanuszewska because items in SubStackView are fill equally. If you hide item in stack view, all other items will try to take place of this hidden item equally
– Robert Dresler
Nov 20 '18 at 16:45
@MartaPanuszewska because items in SubStackView are fill equally. If you hide item in stack view, all other items will try to take place of this hidden item equally
– Robert Dresler
Nov 20 '18 at 16:45
@MartaPanuszewska ok, I edited my answer, try it now. It should be easier way
– Robert Dresler
Nov 20 '18 at 16:52
@MartaPanuszewska ok, I edited my answer, try it now. It should be easier way
– Robert Dresler
Nov 20 '18 at 16:52
Robert, I might have not explained myself correctly. When I hide the
OtherNumberField
I don't need the the View
(MobileComboBox
+ NumberField
) to become bigger. I need the whole PhoneNumberField
(so, the stack view) to become smaller. So I would like the PhoneNumberField
to change its size from approximately 134 points (2 fields + label) to 78 points (1 field + label).– Marta Panuszewska
Nov 20 '18 at 17:06
Robert, I might have not explained myself correctly. When I hide the
OtherNumberField
I don't need the the View
(MobileComboBox
+ NumberField
) to become bigger. I need the whole PhoneNumberField
(so, the stack view) to become smaller. So I would like the PhoneNumberField
to change its size from approximately 134 points (2 fields + label) to 78 points (1 field + label).– Marta Panuszewska
Nov 20 '18 at 17:06
|
show 2 more comments
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%2f53395160%2fchange-height-of-one-of-multiple-fields-in-stack-view%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
Try edited answer
– Robert Dresler
Nov 20 '18 at 15:43