Positioning constraints not having any effect
I am trying to code the label in the viewDidLoad function. The label is showing up but the code I am using now is not affecting the positioning. I am trying to code everything in the viewDidLoad function. You can see the screenshot below.
override func viewDidLoad()
super.viewDidLoad()
let backbutton = UILabel()
backbutton.backgroundColor = UIColor.orange
backbutton.translatesAutoresizingMaskIntoConstraints = false
backbutton.widthAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.heightAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.centerXAnchor.constraint(equalTo: backbutton.centerXAnchor, constant: 100).isActive = true
backbutton.centerYAnchor.constraint(equalTo: backbutton.centerYAnchor, constant: 300).isActive = true
view.addSubview(backbutton)

ios swift constraints nslayoutconstraint
add a comment |
I am trying to code the label in the viewDidLoad function. The label is showing up but the code I am using now is not affecting the positioning. I am trying to code everything in the viewDidLoad function. You can see the screenshot below.
override func viewDidLoad()
super.viewDidLoad()
let backbutton = UILabel()
backbutton.backgroundColor = UIColor.orange
backbutton.translatesAutoresizingMaskIntoConstraints = false
backbutton.widthAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.heightAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.centerXAnchor.constraint(equalTo: backbutton.centerXAnchor, constant: 100).isActive = true
backbutton.centerYAnchor.constraint(equalTo: backbutton.centerYAnchor, constant: 300).isActive = true
view.addSubview(backbutton)

ios swift constraints nslayoutconstraint
You are trying to constraint the button to its own centre. You wantbackbutton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: 100).isActive = trueand the similar thing for the Y center
– Paulw11
Nov 12 '18 at 2:59
@Paulw11 That does not work.
– Sam Burns
Nov 12 '18 at 3:41
@Paulw11 a runtime error occurs.
– Sam Burns
Nov 12 '18 at 3:41
You need to add the button to its superview before you can create constraints that reference the superview
– Paulw11
Nov 12 '18 at 3:44
before applying any constraint first add thebackbuttonas subview.
– Ratul Sharker
Nov 12 '18 at 5:50
add a comment |
I am trying to code the label in the viewDidLoad function. The label is showing up but the code I am using now is not affecting the positioning. I am trying to code everything in the viewDidLoad function. You can see the screenshot below.
override func viewDidLoad()
super.viewDidLoad()
let backbutton = UILabel()
backbutton.backgroundColor = UIColor.orange
backbutton.translatesAutoresizingMaskIntoConstraints = false
backbutton.widthAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.heightAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.centerXAnchor.constraint(equalTo: backbutton.centerXAnchor, constant: 100).isActive = true
backbutton.centerYAnchor.constraint(equalTo: backbutton.centerYAnchor, constant: 300).isActive = true
view.addSubview(backbutton)

ios swift constraints nslayoutconstraint
I am trying to code the label in the viewDidLoad function. The label is showing up but the code I am using now is not affecting the positioning. I am trying to code everything in the viewDidLoad function. You can see the screenshot below.
override func viewDidLoad()
super.viewDidLoad()
let backbutton = UILabel()
backbutton.backgroundColor = UIColor.orange
backbutton.translatesAutoresizingMaskIntoConstraints = false
backbutton.widthAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.heightAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.centerXAnchor.constraint(equalTo: backbutton.centerXAnchor, constant: 100).isActive = true
backbutton.centerYAnchor.constraint(equalTo: backbutton.centerYAnchor, constant: 300).isActive = true
view.addSubview(backbutton)

ios swift constraints nslayoutconstraint
ios swift constraints nslayoutconstraint
edited Nov 12 '18 at 4:28
rmaddy
238k27310376
238k27310376
asked Nov 12 '18 at 2:34
Sam Burns
44412
44412
You are trying to constraint the button to its own centre. You wantbackbutton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: 100).isActive = trueand the similar thing for the Y center
– Paulw11
Nov 12 '18 at 2:59
@Paulw11 That does not work.
– Sam Burns
Nov 12 '18 at 3:41
@Paulw11 a runtime error occurs.
– Sam Burns
Nov 12 '18 at 3:41
You need to add the button to its superview before you can create constraints that reference the superview
– Paulw11
Nov 12 '18 at 3:44
before applying any constraint first add thebackbuttonas subview.
– Ratul Sharker
Nov 12 '18 at 5:50
add a comment |
You are trying to constraint the button to its own centre. You wantbackbutton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: 100).isActive = trueand the similar thing for the Y center
– Paulw11
Nov 12 '18 at 2:59
@Paulw11 That does not work.
– Sam Burns
Nov 12 '18 at 3:41
@Paulw11 a runtime error occurs.
– Sam Burns
Nov 12 '18 at 3:41
You need to add the button to its superview before you can create constraints that reference the superview
– Paulw11
Nov 12 '18 at 3:44
before applying any constraint first add thebackbuttonas subview.
– Ratul Sharker
Nov 12 '18 at 5:50
You are trying to constraint the button to its own centre. You want
backbutton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: 100).isActive = true and the similar thing for the Y center– Paulw11
Nov 12 '18 at 2:59
You are trying to constraint the button to its own centre. You want
backbutton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: 100).isActive = true and the similar thing for the Y center– Paulw11
Nov 12 '18 at 2:59
@Paulw11 That does not work.
– Sam Burns
Nov 12 '18 at 3:41
@Paulw11 That does not work.
– Sam Burns
Nov 12 '18 at 3:41
@Paulw11 a runtime error occurs.
– Sam Burns
Nov 12 '18 at 3:41
@Paulw11 a runtime error occurs.
– Sam Burns
Nov 12 '18 at 3:41
You need to add the button to its superview before you can create constraints that reference the superview
– Paulw11
Nov 12 '18 at 3:44
You need to add the button to its superview before you can create constraints that reference the superview
– Paulw11
Nov 12 '18 at 3:44
before applying any constraint first add the
backbutton as subview.– Ratul Sharker
Nov 12 '18 at 5:50
before applying any constraint first add the
backbutton as subview.– Ratul Sharker
Nov 12 '18 at 5:50
add a comment |
3 Answers
3
active
oldest
votes
This is how you can align the label into the center of the view,
let backbutton = UILabel()
view.addSubview(backbutton)
backbutton.backgroundColor = UIColor.orange
backbutton.translatesAutoresizingMaskIntoConstraints = false
backbutton.widthAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.heightAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
backbutton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true
When you are setting constraints programmatically, make sure you add the view into the super/parent view before applying constraints. Secondly in the below lines, you are telling the backButton label to align its center to itself (i.e, backbutton.centerXAnchor.constraint(equalTo: backbutton.centerXAnchor).
backbutton.centerXAnchor.constraint(equalTo: backbutton.centerXAnchor, constant: 100).isActive = true
backbutton.centerYAnchor.constraint(equalTo: backbutton.centerYAnchor, constant: 300).isActive = true
As you want to align it center vertically and horizontally to its parent view so you should set the center constraints equal to parent view as below,
backbutton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
backbutton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true

add a comment |
Try this!!
self.view.setNeedsUpdateConstraints()
self.view.layoutIfNeeded()
OP added constraint inviewDidLoad, afterviewDidLoadconstraint already update once.
– Ratul Sharker
Nov 12 '18 at 5:51
still that constraint not getting updated might view is not settled down. so explicitly called it.
– Pushp
Nov 12 '18 at 6:03
My guess is that OP added constraint before adding the subview.
– Ratul Sharker
Nov 12 '18 at 6:07
add a comment |
Try using CGRect instead:
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
let xPostion:CGFloat = screenWidth - 150 //150 is half of view's width
let yPostion:CGFloat = screenHeight - 150 //150 is half of view's height
let buttonWidth:CGFloat = 300
let buttonHeight:CGFloat = 300
backbutton.frame = CGRect(x:xPostion, y:yPostion, width:buttonWidth, height:buttonHeight)
instead of screen width/height you can also try to get the width of your super view using:
self.view.frame.width
self.view.frame.height
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%2f53255308%2fpositioning-constraints-not-having-any-effect%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is how you can align the label into the center of the view,
let backbutton = UILabel()
view.addSubview(backbutton)
backbutton.backgroundColor = UIColor.orange
backbutton.translatesAutoresizingMaskIntoConstraints = false
backbutton.widthAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.heightAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
backbutton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true
When you are setting constraints programmatically, make sure you add the view into the super/parent view before applying constraints. Secondly in the below lines, you are telling the backButton label to align its center to itself (i.e, backbutton.centerXAnchor.constraint(equalTo: backbutton.centerXAnchor).
backbutton.centerXAnchor.constraint(equalTo: backbutton.centerXAnchor, constant: 100).isActive = true
backbutton.centerYAnchor.constraint(equalTo: backbutton.centerYAnchor, constant: 300).isActive = true
As you want to align it center vertically and horizontally to its parent view so you should set the center constraints equal to parent view as below,
backbutton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
backbutton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true

add a comment |
This is how you can align the label into the center of the view,
let backbutton = UILabel()
view.addSubview(backbutton)
backbutton.backgroundColor = UIColor.orange
backbutton.translatesAutoresizingMaskIntoConstraints = false
backbutton.widthAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.heightAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
backbutton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true
When you are setting constraints programmatically, make sure you add the view into the super/parent view before applying constraints. Secondly in the below lines, you are telling the backButton label to align its center to itself (i.e, backbutton.centerXAnchor.constraint(equalTo: backbutton.centerXAnchor).
backbutton.centerXAnchor.constraint(equalTo: backbutton.centerXAnchor, constant: 100).isActive = true
backbutton.centerYAnchor.constraint(equalTo: backbutton.centerYAnchor, constant: 300).isActive = true
As you want to align it center vertically and horizontally to its parent view so you should set the center constraints equal to parent view as below,
backbutton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
backbutton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true

add a comment |
This is how you can align the label into the center of the view,
let backbutton = UILabel()
view.addSubview(backbutton)
backbutton.backgroundColor = UIColor.orange
backbutton.translatesAutoresizingMaskIntoConstraints = false
backbutton.widthAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.heightAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
backbutton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true
When you are setting constraints programmatically, make sure you add the view into the super/parent view before applying constraints. Secondly in the below lines, you are telling the backButton label to align its center to itself (i.e, backbutton.centerXAnchor.constraint(equalTo: backbutton.centerXAnchor).
backbutton.centerXAnchor.constraint(equalTo: backbutton.centerXAnchor, constant: 100).isActive = true
backbutton.centerYAnchor.constraint(equalTo: backbutton.centerYAnchor, constant: 300).isActive = true
As you want to align it center vertically and horizontally to its parent view so you should set the center constraints equal to parent view as below,
backbutton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
backbutton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true

This is how you can align the label into the center of the view,
let backbutton = UILabel()
view.addSubview(backbutton)
backbutton.backgroundColor = UIColor.orange
backbutton.translatesAutoresizingMaskIntoConstraints = false
backbutton.widthAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.heightAnchor.constraint(equalToConstant: 300).isActive = true
backbutton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
backbutton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true
When you are setting constraints programmatically, make sure you add the view into the super/parent view before applying constraints. Secondly in the below lines, you are telling the backButton label to align its center to itself (i.e, backbutton.centerXAnchor.constraint(equalTo: backbutton.centerXAnchor).
backbutton.centerXAnchor.constraint(equalTo: backbutton.centerXAnchor, constant: 100).isActive = true
backbutton.centerYAnchor.constraint(equalTo: backbutton.centerYAnchor, constant: 300).isActive = true
As you want to align it center vertically and horizontally to its parent view so you should set the center constraints equal to parent view as below,
backbutton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
backbutton.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true

edited Nov 12 '18 at 7:11
answered Nov 12 '18 at 4:43
Kamran
6,27511028
6,27511028
add a comment |
add a comment |
Try this!!
self.view.setNeedsUpdateConstraints()
self.view.layoutIfNeeded()
OP added constraint inviewDidLoad, afterviewDidLoadconstraint already update once.
– Ratul Sharker
Nov 12 '18 at 5:51
still that constraint not getting updated might view is not settled down. so explicitly called it.
– Pushp
Nov 12 '18 at 6:03
My guess is that OP added constraint before adding the subview.
– Ratul Sharker
Nov 12 '18 at 6:07
add a comment |
Try this!!
self.view.setNeedsUpdateConstraints()
self.view.layoutIfNeeded()
OP added constraint inviewDidLoad, afterviewDidLoadconstraint already update once.
– Ratul Sharker
Nov 12 '18 at 5:51
still that constraint not getting updated might view is not settled down. so explicitly called it.
– Pushp
Nov 12 '18 at 6:03
My guess is that OP added constraint before adding the subview.
– Ratul Sharker
Nov 12 '18 at 6:07
add a comment |
Try this!!
self.view.setNeedsUpdateConstraints()
self.view.layoutIfNeeded()
Try this!!
self.view.setNeedsUpdateConstraints()
self.view.layoutIfNeeded()
answered Nov 12 '18 at 5:01
Pushp
285212
285212
OP added constraint inviewDidLoad, afterviewDidLoadconstraint already update once.
– Ratul Sharker
Nov 12 '18 at 5:51
still that constraint not getting updated might view is not settled down. so explicitly called it.
– Pushp
Nov 12 '18 at 6:03
My guess is that OP added constraint before adding the subview.
– Ratul Sharker
Nov 12 '18 at 6:07
add a comment |
OP added constraint inviewDidLoad, afterviewDidLoadconstraint already update once.
– Ratul Sharker
Nov 12 '18 at 5:51
still that constraint not getting updated might view is not settled down. so explicitly called it.
– Pushp
Nov 12 '18 at 6:03
My guess is that OP added constraint before adding the subview.
– Ratul Sharker
Nov 12 '18 at 6:07
OP added constraint in
viewDidLoad, after viewDidLoad constraint already update once.– Ratul Sharker
Nov 12 '18 at 5:51
OP added constraint in
viewDidLoad, after viewDidLoad constraint already update once.– Ratul Sharker
Nov 12 '18 at 5:51
still that constraint not getting updated might view is not settled down. so explicitly called it.
– Pushp
Nov 12 '18 at 6:03
still that constraint not getting updated might view is not settled down. so explicitly called it.
– Pushp
Nov 12 '18 at 6:03
My guess is that OP added constraint before adding the subview.
– Ratul Sharker
Nov 12 '18 at 6:07
My guess is that OP added constraint before adding the subview.
– Ratul Sharker
Nov 12 '18 at 6:07
add a comment |
Try using CGRect instead:
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
let xPostion:CGFloat = screenWidth - 150 //150 is half of view's width
let yPostion:CGFloat = screenHeight - 150 //150 is half of view's height
let buttonWidth:CGFloat = 300
let buttonHeight:CGFloat = 300
backbutton.frame = CGRect(x:xPostion, y:yPostion, width:buttonWidth, height:buttonHeight)
instead of screen width/height you can also try to get the width of your super view using:
self.view.frame.width
self.view.frame.height
add a comment |
Try using CGRect instead:
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
let xPostion:CGFloat = screenWidth - 150 //150 is half of view's width
let yPostion:CGFloat = screenHeight - 150 //150 is half of view's height
let buttonWidth:CGFloat = 300
let buttonHeight:CGFloat = 300
backbutton.frame = CGRect(x:xPostion, y:yPostion, width:buttonWidth, height:buttonHeight)
instead of screen width/height you can also try to get the width of your super view using:
self.view.frame.width
self.view.frame.height
add a comment |
Try using CGRect instead:
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
let xPostion:CGFloat = screenWidth - 150 //150 is half of view's width
let yPostion:CGFloat = screenHeight - 150 //150 is half of view's height
let buttonWidth:CGFloat = 300
let buttonHeight:CGFloat = 300
backbutton.frame = CGRect(x:xPostion, y:yPostion, width:buttonWidth, height:buttonHeight)
instead of screen width/height you can also try to get the width of your super view using:
self.view.frame.width
self.view.frame.height
Try using CGRect instead:
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
let xPostion:CGFloat = screenWidth - 150 //150 is half of view's width
let yPostion:CGFloat = screenHeight - 150 //150 is half of view's height
let buttonWidth:CGFloat = 300
let buttonHeight:CGFloat = 300
backbutton.frame = CGRect(x:xPostion, y:yPostion, width:buttonWidth, height:buttonHeight)
instead of screen width/height you can also try to get the width of your super view using:
self.view.frame.width
self.view.frame.height
answered Nov 12 '18 at 6:35
Mahgol Fa
28714
28714
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53255308%2fpositioning-constraints-not-having-any-effect%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 are trying to constraint the button to its own centre. You want
backbutton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: 100).isActive = trueand the similar thing for the Y center– Paulw11
Nov 12 '18 at 2:59
@Paulw11 That does not work.
– Sam Burns
Nov 12 '18 at 3:41
@Paulw11 a runtime error occurs.
– Sam Burns
Nov 12 '18 at 3:41
You need to add the button to its superview before you can create constraints that reference the superview
– Paulw11
Nov 12 '18 at 3:44
before applying any constraint first add the
backbuttonas subview.– Ratul Sharker
Nov 12 '18 at 5:50