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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:48:40+00:00 2026-06-16T17:48:40+00:00

I am trying to build a view that will have several different controls, but

  • 0

I am trying to build a view that will have several different controls, but I want the view to only scroll in the vertical direction. I’m using auto-layout so I don’t want to have to specify the size manually (I know I can do that, but from what I understand according to the release notes on UIScrollView you should not have to).

So if I create a simple view controller (with no .xib file) and simply add the following to the init method I’d expect that my labels would wrap (which they do) and that my view would scroll vertically, however, this is not the case:

- (id)init {
    self = [super init];
    if (self) {

        UIScrollView *_scrollView = [UIScrollView new];
        [_scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];

        UILabel *label1 = [UILabel new];
        UILabel *label2 = [UILabel new];

        [label1 setTranslatesAutoresizingMaskIntoConstraints:NO];
        [label2 setTranslatesAutoresizingMaskIntoConstraints:NO];

        [label1 setNumberOfLines:0];
        [label2 setNumberOfLines:0];

        [label1 setPreferredMaxLayoutWidth:240];
        [label2 setPreferredMaxLayoutWidth:240];

        NSString *sampleText = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
        [label1 setText:sampleText];
        [label2 setText:sampleText];

        [self.view addSubview:_scrollView];
        [_scrollView addSubview:label1];
        [_scrollView addSubview:label2];

        NSArray *constraints;
        constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)];
        [self.view addConstraints:constraints];

        constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)];
        [self.view addConstraints:constraints];

        constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label1]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1)];
        [_scrollView addConstraints:constraints];

        constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label2]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label2)];
        [_scrollView addConstraints:constraints];

        constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label1]-[label2]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1, label2)];
        [_scrollView addConstraints:constraints];
    }

    return self;
}

There are several results from this code that I think are odd. If you log the scrollviewheight in the viewDidLayoutSubviews method (after the super call), which is where all constraints should be applied, then the height is reporting 0. What’s also strange is that the width is reporting a different number than I would expect. I would expect it to report the width of the superview due to the very first constraint (I even tried forcing the width inside that constraint with a priority that is supposedly required but it still didn’t work).

As I understand it, the UIScrollView‘s content size should be set by the intrinsic sizes of its content but that doesn’t seem to be happening.

So, does anyone know what is missing?

  • 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-16T17:48:41+00:00Added an answer on June 16, 2026 at 5:48 pm

    I had contacted Apple’s support to get an answer on my question. The problem with the code is that the objects contained within the UIScrollView must be pinned to both the top and bottom vertically for it to determine its intrinsic content size. So the following line of code

    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label1]-[label2]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1, label2)];
    [_scrollView addConstraints:constraints];
    

    Should be changed to:

    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label1]-[label2]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label1, label2)];
    [_scrollView addConstraints:constraints];
    

    They also pointed out that I was using the “new” keyword on several objects where the basic “init” is not the designated initializer. While that had nothing to do with my specific problem they did point out that you should avoid doing that as your object could be in an invalid state.

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

Sidebar

Related Questions

I am trying to build a app that will have a view hidden unless
I'm trying to build a pipeline with gst-launch in Windows 7 that will view
I am trying to create view or a function that will build a HTML
I'm trying to build a view that operators something similar to this: The user
I am currently trying to build a cakephp app that will list items from
I'm trying to do a script in WWW::Mechanize that will build a job in
I'm working on a grid view that will display cells that will have some
I am trying to build a program using model-view-controller. Specifically: I have a state
I have a form that is added through the nodeapi displayed only on view
I'm trying to build an app that will display stats for a certain game

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.