Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9130407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:57:37+00:00 2026-06-17T07:57:37+00:00

I have two labels. I want to be able to move both if one

  • 0

I have two labels. I want to be able to move both if one is moved. How do I “attach” them together with NSLayoutConstraints? I can do this in IB, but need to do it in code.

also, what are NSLayoutAttributeBaseline, NSLayoutAttributeLeading, and NSLayoutAttributeTrailing?

EDIT:

centering poweredByLabel (aka label02):

[constraints addObject:[NSLayoutConstraint constraintWithItem:poweredByLabel
                                                    attribute:NSLayoutAttributeCenterX
                                                    relatedBy:NSLayoutRelationEqual
                                                       toItem:myImage
                                                    attribute:NSLayoutAttributeCenterX
                                                   multiplier:1.0
                                                     constant:0]];

stack the labels and switch vertically:

[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[rememberPasswordSwitch]-10-[rememberPasswordLabel]-10-[versionLabel]-[poweredByLabel]-|"
                                                                         options:NSLayoutFormatAlignAllBaseline
                                                                         metrics:nil
                                                                           views:viewsDictionary]];

which produces the error:

* Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘Unable to parse constraint
format: Options mask required views to be aligned on a vertical edge,
which is not allowed for layout that is also vertical.
V:[rememberPasswordSwitch]-10-[rememberPasswordLabel]-10-[versionLabel]-[poweredByLabel]-|……………………………………………………………………………………………..^’

w/out the NSLayoutFormatAlignAllBaseline option, it runs fine (they stack but are not all centered horizontally).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:57:38+00:00Added an answer on June 17, 2026 at 7:57 am

    If you need to do this in code, first create the NSLayoutConstraint(s), then add the constraint(s) to the labels’ superview.

    There are two ways to create constraints in code. constraintsWithVisualFormat is usually much more concise than constraintWithItem.

    // Make label1's NSLayoutAttributeTrailing be the 'standard Aqua space' away from label2's NSLayoutAttributeLeading. Also, vertically align their baselines.
    NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[label1]-[label2]"  options:NSLayoutFormatAlignAllBaseline  metrics:nil  views:NSDictionaryOfVariableBindings(label1, label2) ] ;
    

    Then you add the constraint(s) to the labels’ superview:

    [label1.superview  addConstraints:constraints] ;   // Use `label1.superview` or your own reference to the label's superview.
    

    The Cocoa Auto Layout Guide is short and easy to follow. Give it a read, and I’d be happy to answer any questions you still have.

    Edit 1

    The option NSLayoutFormatAlignAllBaseline creates constraints (in addition to those created by the VisualFormat string) that vertically align the baselines of all specified objects. If your VisualFormat string is creating vertical constraints (it starts with “V:”), you don’t want to use this option. You’d want to use 0 (which means no options), or an option that creates horizontal constraints, like NSLayoutFormatAlignAllCenterX.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two labels on a form. I want to be able to drag
I have two labels that are stacked. If I want a horizontal line between
i have a form with one button and two labels and i have a
I have two labels in two different positions, when both labels are tapped at
Consider the following table with messages. Some of them have one or more labels
I have two labels (A and B) in a Flex VBox, with the horizontal
For Android I have two labels in a table row. I am trying to
I've created a skin that allows me to have two labels on a spark
I have a view which has two labels. When I swipe left I fill
I have to do a user registration form (classical layout with two columns, labels

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.