2

Currently I am using this line of code to set the origin on a UILabel at the top left corner...

addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))

However I want the origin to be 0 pixels from left edge H and half of height if that makes sense.

Any suggestions? I've tried changing the multiplier to no avail.

What I am trying to do is move the origin so that (0,0) instead of being the top left corner, is the furthermost left of the label and half of the label height so that I can center the label properly.

1
  • 1
    I suggest you modify your code so that people can read it without ridiculous scrolling. I'm refusing to read it as it is. Commented Apr 15, 2017 at 18:08

3 Answers 3

1

Just to clarify, you need the label left aligned and centered top to bottom?

Have you looked at NSLayoutAnchor?

messageLabel = UILabel(/* Initialized somehow */)
messageLabel.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(messageLabel)
view.centerYAnchor.constraint(equalTo: messageLabel.centerYAnchor).isActive = true
view.leadingAnchor.constraint(equalTo: messageLabel.leadingAnchor).isActive = true
Sign up to request clarification or add additional context in comments.

Comments

0

try to set the origin of your label with this:

messageLabel.horizontalAlignmentMode = .Left
messageLabel.position = CGPoint(x:0.0, y:self.size.height)

no need for constraint!!

Hope that help you out :)

3 Comments

There are no need for constraints if your application is rotation locked. Meaning you have only one orientation setting for your app, either landscape or portrait. If you want to allow for orientation change within your app you really need to use auto layout. The only other option would be to listen for orientation changes from the system and manually adjusting the position of your UI elements. Which is very tedious, isn't as elegant during transitions as auto layout, and really in todays day n age of iOS development, not using auto layout is a major mistake.
Since there is no requirement of Multiplier or Constant therefore constraint is not needed. stefan just wants to set button to top let corner that's it
Without proper constraints label frame will remain the same upon orientation change. Yes you can add it that way but the question originally was about NSLayoutConstraint.
0

First you have to add your view as a subview of its container. Once you have done this you will want to set the translatesAutoResizingMaskToConstraints to false. Then it is time to add your NSLayoutConstraints:

    let labelWidth:CGFloat = 320
    let labelHeight:CGFloat = 30
    // Container is what you are adding label too
    let container = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
    let label = UILabel(frame: CGRect(x: 0, y: (container.frame.size.height / 2) - (labelHeight / 2), width: labelWidth, height: labelHeight))
    container.addSubview(label)
    label.translatesAutoresizingMaskIntoConstraints = false

    // Add Width and Height (IF is required,
    // if not required, anchor your label to appropriately)
    label.addConstraints([
        NSLayoutConstraint.init(item: label, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: labelWidth),
        NSLayoutConstraint.init(item: label, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: labelHeight),
    ])

    label.layoutIfNeeded()

    // Add left constraint and center in container constraint
    container.addConstraints([
        NSLayoutConstraint.init(item: label, attribute: .left, relatedBy: .equal, toItem: .container, attribute: .left, multiplier: 1.0, constant: 0.0),
        NSLayoutConstraint.init(item: label, attribute: .centerX, relatedBy: .equal, toItem: .container, attribute: .left, multiplier: 1.0, constant: 0.0)
    ])

    container.layoutIfNeeded()

EDIT

Yes there is documentation given to you automatically by Xcode when you use dot syntax. If you go to the parameter of the attribute and just enter a period, you will see a drop down of all the possible options.

enter image description here

3 Comments

For which constant are you referring to? If you mean the width and height for the label, I set that at the very top of my code.
Is there anywhere in the documentation where I can see all possible attributes for NSLayoutConstraints?
I've updated my question a bit to better communicate what exactly I'm trying to do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.