Center UILabel inside UIView in Stack View
I have a horizontal stack view arranged in storyboard. As for the views for the stack view, it is done programatically since the number of views inside the stack view is dynamic. I've set the stack view to be 'Fill Equally'.
For the views inside, there will be a UIlabel. The problem that I have is that I can't center the UILabel inside the UIView which will be added into the stack view.
Here is my code:
let main1 = UIView()
main1.backgroundColor = UIColor.red
stackView.addArrangedSubview(main1)
let text1 = UILabel()
text1.text = "Lvl 1"
text1.textColor = .blue
text1.textAlignment = .center
text1.backgroundColor = .yellow
text1.sizeToFit()
main1.addSubview(text1)
let label2 = UIView()
label2.backgroundColor = UIColor.blue
stackView.addArrangedSubview(label2)
For this example, I'm only adding 2 view, with the label added to the first view. So, how can I center the UILabel which will be inside of each UIView?
ios uilabel uistackview
add a comment |
I have a horizontal stack view arranged in storyboard. As for the views for the stack view, it is done programatically since the number of views inside the stack view is dynamic. I've set the stack view to be 'Fill Equally'.
For the views inside, there will be a UIlabel. The problem that I have is that I can't center the UILabel inside the UIView which will be added into the stack view.
Here is my code:
let main1 = UIView()
main1.backgroundColor = UIColor.red
stackView.addArrangedSubview(main1)
let text1 = UILabel()
text1.text = "Lvl 1"
text1.textColor = .blue
text1.textAlignment = .center
text1.backgroundColor = .yellow
text1.sizeToFit()
main1.addSubview(text1)
let label2 = UIView()
label2.backgroundColor = UIColor.blue
stackView.addArrangedSubview(label2)
For this example, I'm only adding 2 view, with the label added to the first view. So, how can I center the UILabel which will be inside of each UIView?
ios uilabel uistackview
Make a container view for every UIView + UILabel and center label inside this container.
– DennyDog
Nov 21 '18 at 9:33
add a comment |
I have a horizontal stack view arranged in storyboard. As for the views for the stack view, it is done programatically since the number of views inside the stack view is dynamic. I've set the stack view to be 'Fill Equally'.
For the views inside, there will be a UIlabel. The problem that I have is that I can't center the UILabel inside the UIView which will be added into the stack view.
Here is my code:
let main1 = UIView()
main1.backgroundColor = UIColor.red
stackView.addArrangedSubview(main1)
let text1 = UILabel()
text1.text = "Lvl 1"
text1.textColor = .blue
text1.textAlignment = .center
text1.backgroundColor = .yellow
text1.sizeToFit()
main1.addSubview(text1)
let label2 = UIView()
label2.backgroundColor = UIColor.blue
stackView.addArrangedSubview(label2)
For this example, I'm only adding 2 view, with the label added to the first view. So, how can I center the UILabel which will be inside of each UIView?
ios uilabel uistackview
I have a horizontal stack view arranged in storyboard. As for the views for the stack view, it is done programatically since the number of views inside the stack view is dynamic. I've set the stack view to be 'Fill Equally'.
For the views inside, there will be a UIlabel. The problem that I have is that I can't center the UILabel inside the UIView which will be added into the stack view.
Here is my code:
let main1 = UIView()
main1.backgroundColor = UIColor.red
stackView.addArrangedSubview(main1)
let text1 = UILabel()
text1.text = "Lvl 1"
text1.textColor = .blue
text1.textAlignment = .center
text1.backgroundColor = .yellow
text1.sizeToFit()
main1.addSubview(text1)
let label2 = UIView()
label2.backgroundColor = UIColor.blue
stackView.addArrangedSubview(label2)
For this example, I'm only adding 2 view, with the label added to the first view. So, how can I center the UILabel which will be inside of each UIView?
ios uilabel uistackview
ios uilabel uistackview
asked Nov 21 '18 at 9:27
da32da32
1911212
1911212
Make a container view for every UIView + UILabel and center label inside this container.
– DennyDog
Nov 21 '18 at 9:33
add a comment |
Make a container view for every UIView + UILabel and center label inside this container.
– DennyDog
Nov 21 '18 at 9:33
Make a container view for every UIView + UILabel and center label inside this container.
– DennyDog
Nov 21 '18 at 9:33
Make a container view for every UIView + UILabel and center label inside this container.
– DennyDog
Nov 21 '18 at 9:33
add a comment |
1 Answer
1
active
oldest
votes
You can use autolayouts to achieve this. Check the code below:
...
main1.addSubview(text1)
text1.translatesAutoresizingMaskIntoConstraints = false
text1.centerXAnchor.constraint(equalTo: main1.centerXAnchor).isActive = true
text1.centerYAnchor.constraint(equalTo: main1.centerYAnchor).isActive = true
its working...thx...
– da32
Nov 22 '18 at 6:54
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%2f53408876%2fcenter-uilabel-inside-uiview-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
You can use autolayouts to achieve this. Check the code below:
...
main1.addSubview(text1)
text1.translatesAutoresizingMaskIntoConstraints = false
text1.centerXAnchor.constraint(equalTo: main1.centerXAnchor).isActive = true
text1.centerYAnchor.constraint(equalTo: main1.centerYAnchor).isActive = true
its working...thx...
– da32
Nov 22 '18 at 6:54
add a comment |
You can use autolayouts to achieve this. Check the code below:
...
main1.addSubview(text1)
text1.translatesAutoresizingMaskIntoConstraints = false
text1.centerXAnchor.constraint(equalTo: main1.centerXAnchor).isActive = true
text1.centerYAnchor.constraint(equalTo: main1.centerYAnchor).isActive = true
its working...thx...
– da32
Nov 22 '18 at 6:54
add a comment |
You can use autolayouts to achieve this. Check the code below:
...
main1.addSubview(text1)
text1.translatesAutoresizingMaskIntoConstraints = false
text1.centerXAnchor.constraint(equalTo: main1.centerXAnchor).isActive = true
text1.centerYAnchor.constraint(equalTo: main1.centerYAnchor).isActive = true
You can use autolayouts to achieve this. Check the code below:
...
main1.addSubview(text1)
text1.translatesAutoresizingMaskIntoConstraints = false
text1.centerXAnchor.constraint(equalTo: main1.centerXAnchor).isActive = true
text1.centerYAnchor.constraint(equalTo: main1.centerYAnchor).isActive = true
answered Nov 21 '18 at 9:56


gulyashkigulyashki
35124
35124
its working...thx...
– da32
Nov 22 '18 at 6:54
add a comment |
its working...thx...
– da32
Nov 22 '18 at 6:54
its working...thx...
– da32
Nov 22 '18 at 6:54
its working...thx...
– da32
Nov 22 '18 at 6:54
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%2f53408876%2fcenter-uilabel-inside-uiview-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
Make a container view for every UIView + UILabel and center label inside this container.
– DennyDog
Nov 21 '18 at 9:33