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 8648531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:20:05+00:00 2026-06-12T13:20:05+00:00

In iOS6, I am using autolayouts. I have 2 views v1 and v2 programatically

  • 0

In iOS6, I am using autolayouts.

  • I have 2 views v1 and v2 programatically created.
  • v2 is added as a subview to v1
  • The constraints for v1 have already been created programatically (not shown here).
  • I want v1 and v2 to be of the same size and I want v2 to be placed on top of v1 exactly the horizontal and vertical center matching such that v2 is exactly on top of v1

Question:

  • How can I achieve this (preferably using visual format, if its not possible can you suggest alternate approach)?

Code – Given below is my attempt but however it doesn’t provide the desired output. – No errors thrown. V1 is placed correctly but V2 is one the left hand corner of V1

Note – I have explained below in the comments, what I am trying to do.

[v1 addSubView:v2];

v1.translatesAutoresizingMaskIntoConstraints        = NO;
v2.translatesAutoresizingMaskIntoConstraints        = NO;

NSDictionary *viewDictionary = NSDictionaryOfVariableBindings(v1,
                                                              v2);

//I wanted v1 and v2 to be of the same height and wanted the center Y to be aligned together
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[v2(==v1)]"
                                                                         options:NSLayoutFormatAlignAllCenterY
                                                                         metrics:nil
                                                                           views:viewDictionary];


//I wanted v1 and v2 to be of the same height and wanted the center X to be aligned together
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[v2(==v1)]"
                                                                       options:NSLayoutFormatAlignAllCenterX
                                                                       metrics:nil
                                                                         views:viewDictionary];

[v1 addConstraints:horizontalConstraints];
[v1 addConstraints:verticalConstraints];
  • 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-12T13:20:05+00:00Added an answer on June 12, 2026 at 1:20 pm

    Shouldn’t just this work?

    NSMutableArray *constraints = [NSMutableArray new];
    
    constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|[v2]|"
                                                                            options:0
                                                                            metrics:nil
                                                                              views:NSDictionaryOfVariableBindings(v2)];
    constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[v2]|"
                                                                            options:0
                                                                            metrics:nil
                                                                              views:NSDictionaryOfVariableBindings(v2)];
    [v1 addConstraints:constraints];
    

    What this does is it makes v2 stretch its height and width to the size of its superview, which in this case is v1 and positions it so its right on top of its superview too.

    It seems that in your case, you don’t need to align the view centerX and centerY. Otherwise, I’m not understanding the context of your question properly.

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

Sidebar

Related Questions

I'm experiencing some strange behavior when using tableviews in iOS6. I see they've added
How long after iOS 6 comes out that developers HAVE to submit using iOS6?
Is there any native solution using Google maps in iOS6 development? Let's not assume
I have created a Universal app using Interface Builder. My view controllers are embedded
I have done routing in iOS apps before, but with iOS6 we are now
I'm trying to figure out if I should start using the recently added object
We're currently using PhoneGap for our mobile applications, and I know they have device.udid
i upgraded to Xcode 4.5 and have started using the iOS SDK 6.0: i
I noticed this while using iOS6 beta 3 When I create a new subclass
I am trying to check whether client opening the page is using iOS6 or

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.