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

  • SEARCH
  • Home
  • 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 9201367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:59:23+00:00 2026-06-17T22:59:23+00:00

I have a content view with two subviews. I’ve stacked the subviews vertically and

  • 0

I have a content view with two subviews. I’ve stacked the subviews vertically and centered them horizontally (x axis) in the content view:

[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[buttonView]-[label]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(buttonView, label)]];

Now i want to center them vertically (y-axis). I want equal space from the top of the buttonView and bottom of the label view.

how can i do that? top and bottom constraints with priorities?

EDIT:

Scott, here is how the code you suggested makes it look:

The buttonView is too far up

I tried making the spacing between the buttonView and label -1- but didn’t help.
and this is how i need it to look:

enter image description here

Here is the code i used to achieve it:

[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-13-[buttonView]-[label]-10-|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(buttonView, label)]];

my constraints work, but is hard coded which i would prefer to stay away from if possible.

  • 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-17T22:59:24+00:00Added an answer on June 17, 2026 at 10:59 pm

    To get exactly what you’re asking for, you’ll need to specify a number of specific constraints. A naive attempt might start with (don’t use the following line):

    [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[buttonView]-[label]-|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(buttonView, label)]];
    

    But this results in the button and label being stretched to satisfy the constraints. So instead let’s just tell the system what we want. We’ll start with your code, since that gives us a basic alignment.

    [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[buttonView]-[label]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(buttonView, label)]];
    [contentView addConstraint:[NSLayoutConstraint constraintWithItem:buttonView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:-4.0]];
    [contentView addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
    [contentView addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
    

    Where did that 4.0 come from? It’s half of the default subview spacing - (8), aligning the bottom with the center Y, but up a bit to let the middle line up.

    Also, you’ll note I removed the NSLayoutFormatAlignAllCenterX, because it doesn’t do anything meaningful at this point, and wasn’t actually aligning the views with the superview’s center X, just with each others.

    Edit:

    I made a simple example app, which has a single ViewController and a button and label added to a view. See the code as a gist. This produces the following output:

    Example app with my constraints

    As a comparison, here’s just using your original constraint in this app (which is why I thought NSLayoutAlignAllCenterX wasn’t doing the centering):

    Example app with questioner's constraints

    I’m wondering if there’s more going on with your button (is it a custom class?) and the contentView.

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

Sidebar

Related Questions

I have two view controllers, one of them is a subview of the other.
I have a master view and two subviews. I would like to switch from
I have two instances of NSScrollView both presenting a view on the same content.
I have two view controllers let's call them A and B (1) in A
I have an activity ( MainActivity.java ) in which content view is like this
I have this method in App Delegate that makes a window and content view
I have following view <asp:Content ID=Content2 ContentPlaceHolderID=MainContent runat=server> <h2>Tables <%=ViewData[RetriverName] %></h2> <%using (Html.BeginForm(ResfreshSelectedTables, Home))
So I have a view containing a web view with HTML content. This is
I have an action 'approval' that renders a view which displays some content from
I have this Haml view: !!! strict %html{:xmlns => http://www.w3.org/1999/xhtml} %head %meta{:content => text/html;

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.