Change height of one of multiple fields in stack view












1















In my code I have a stack view that initially has 1 element PhoneNumberField. Another PhoneNumberFieldscan be added dynamically in the runtime:



@IBAction func addAlternatePhoneNumberAction(_ sender: UIButton) {
let alternateNumberView = PhoneNumberField()
...
phoneNumberStackView.addArrangedSubview(alternateNumberView)
}


This is what xib for PhoneNumberFieldlooks like:
enter image description here



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.










share|improve this question

























  • Try edited answer

    – Robert Dresler
    Nov 20 '18 at 15:43
















1















In my code I have a stack view that initially has 1 element PhoneNumberField. Another PhoneNumberFieldscan be added dynamically in the runtime:



@IBAction func addAlternatePhoneNumberAction(_ sender: UIButton) {
let alternateNumberView = PhoneNumberField()
...
phoneNumberStackView.addArrangedSubview(alternateNumberView)
}


This is what xib for PhoneNumberFieldlooks like:
enter image description here



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.










share|improve this question

























  • Try edited answer

    – Robert Dresler
    Nov 20 '18 at 15:43














1












1








1








In my code I have a stack view that initially has 1 element PhoneNumberField. Another PhoneNumberFieldscan be added dynamically in the runtime:



@IBAction func addAlternatePhoneNumberAction(_ sender: UIButton) {
let alternateNumberView = PhoneNumberField()
...
phoneNumberStackView.addArrangedSubview(alternateNumberView)
}


This is what xib for PhoneNumberFieldlooks like:
enter image description here



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.










share|improve this question
















In my code I have a stack view that initially has 1 element PhoneNumberField. Another PhoneNumberFieldscan be added dynamically in the runtime:



@IBAction func addAlternatePhoneNumberAction(_ sender: UIButton) {
let alternateNumberView = PhoneNumberField()
...
phoneNumberStackView.addArrangedSubview(alternateNumberView)
}


This is what xib for PhoneNumberFieldlooks like:
enter image description here



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












1 Answer
1






active

oldest

votes


















3














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.



enter image description here



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:



enter image description here



Now when you hide one view from StackView, StackView will be smaller because you didn't set its height.






share|improve this answer


























  • 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











  • @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 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













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%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









3














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.



enter image description here



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:



enter image description here



Now when you hide one view from StackView, StackView will be smaller because you didn't set its height.






share|improve this answer


























  • 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











  • @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 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


















3














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.



enter image description here



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:



enter image description here



Now when you hide one view from StackView, StackView will be smaller because you didn't set its height.






share|improve this answer


























  • 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











  • @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 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
















3












3








3







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.



enter image description here



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:



enter image description here



Now when you hide one view from StackView, StackView will be smaller because you didn't set its height.






share|improve this answer















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.



enter image description here



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:



enter image description here



Now when you hide one view from StackView, StackView will be smaller because you didn't set its height.







share|improve this answer














share|improve this answer



share|improve this answer








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. 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











  • @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 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





















  • 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











  • @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 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



















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




















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%2f53395160%2fchange-height-of-one-of-multiple-fields-in-stack-view%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

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith