Center UILabel inside UIView in Stack View












0















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?










share|improve this question























  • Make a container view for every UIView + UILabel and center label inside this container.

    – DennyDog
    Nov 21 '18 at 9:33
















0















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?










share|improve this question























  • Make a container view for every UIView + UILabel and center label inside this container.

    – DennyDog
    Nov 21 '18 at 9:33














0












0








0








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












1 Answer
1






active

oldest

votes


















1














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





share|improve this answer
























  • its working...thx...

    – da32
    Nov 22 '18 at 6:54











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









1














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





share|improve this answer
























  • its working...thx...

    – da32
    Nov 22 '18 at 6:54
















1














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





share|improve this answer
























  • its working...thx...

    – da32
    Nov 22 '18 at 6:54














1












1








1







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 9:56









gulyashkigulyashki

35124




35124













  • 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





its working...thx...

– da32
Nov 22 '18 at 6:54




















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%2f53408876%2fcenter-uilabel-inside-uiview-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