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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:41:45+00:00 2026-06-11T15:41:45+00:00

I have a UILabel (named contentLbl) that will change dynamically within a UIScrollView. Above

  • 0

I have a UILabel (named contentLbl) that will change dynamically within a UIScrollView. Above it is another UILabel (named contentTitleLbl) that is a title and does not contain dynamic content. To successfully scroll I know that I must at least set the ContentSize of the UIScrollView to that of the content view’s. I’ve also seen that the Frame should be set. I have tried both but it still does not scroll.

In my example code contentView contains both the contentLbl and contentTitleLbl. scrollView is the reference to the UIScrollView that I’m currently trying to use.

Here is my current code:

public override void ViewDidLoad ()
{
    base.ViewDidLoad ();
    //contentLbl is to have dynamic text, and currently stores a large lorem ipsum paragraph
    //contentTitleLbl is the title and is not dynamic
    this.contentLbl.SizeToFit();
    this.scrollView.ContentSize = new SizeF(this.contentView.Frame.Width, this.contentLbl.Frame.Size.Height + this.contentTitleLbl.Frame.Size.Height);
    this.scrollView.Frame = new RectangleF(this.scrollView.Frame.X, this.scrollView.Frame.Y, this.scrollView.ContentSize.Width, this.scrollView.ContentSize.Height);
    this.scrollView.AddSubview(this.contentView);
}

Any help would be greatly appreciated!

  • 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-11T15:41:46+00:00Added an answer on June 11, 2026 at 3:41 pm

    Quick question: Are you intending for the title to scroll with your text? In this solution, I’ve done that, as this is my interpretation. I’ll adjust it, if you’d like, for the title not to scroll.
    I recreated your sample by making this hierarchy in XCode

    • UIView (main view of the view controller)
      • UIView (contentView)
        • UILabel (contentTitleLbl)
          • Frame = (20,15,160,21)
        • UILabel (contentLbl)
          • Lines = 100
          • Frame = (20,43,160,190)
          • Text = “Lorem ipsum…” (long paragraph)
      • UIScrollView (scrollView)
        • Frame = (20,20,140,140)

    I set the background colors of all the items to different values so that I could see the dimensions of all the components.

    First, I needed to make sure all the auto-resizing of the labels was turned off. You can do this in XCode, or programmatically:

    contentLbl.AutoresizingMask = UIViewAutoresizing.None;
    contentTitleLbl.AutoresizingMask = UIViewAutoresizing.None;
    

    I’d prefer to place the contentView inside the scrollView via XCode, but it looks like you had them as siblings under the main View. So we reparent the contentView to place it inside the scrollView

    scrollView.AddSubview( contentView );
    

    Then make the title the same width as the scrollView, positioning it at the origin and leaving the height as designed in XCode

    var f = contentTitleLbl.Frame;
    f.X = 0;
    f.Y = 0;
    f.Width = scrollView.Frame.Width;
    contentTitleLbl.Frame = f;
    

    Then the first major issue: resizing a UILabel to fit your contents dynamically. SizeToFit resizes the label to a single line of text (or at least, did for me). What you really want is NSString.sizeWithFont which appears in MonoTouch as UIView.StringSize. Be careful, as some of these return the size for a single line of text. Here I’ve constrained the width to the width of the scroll view, but left the height unbounded by using float.MaxValue.

    SizeF sz = this.View.StringSize(
        contentLbl.Text,
        contentLbl.Font,
        new SizeF( scrollView.Frame.Width, float.MaxValue),
        UILineBreakMode.WordWrap );
    

    Then resize and position the label just underneath the title:

    f = contentLbl.Frame;
    f.X = 0;
    f.Y = contentTitleLbl.Frame.Bottom; // nudge the contentLbl right up against the title
    f.Width = sz.Width;    // size matches the measured values from above
    f.Height = sz.Height;
    contentLbl.Frame = f;
    

    Then adjust the size of the contentView to hold the title and the content. This view will be placed at the origin in the scroll view. The size is determined dynamically by the code above. I’ve added a small margin at the bottom so I can tell that the whole content has been rendered

    f = contentView.Frame;
    f.X = 0;
    f.Y = 0;
    f.Width = scrollView.Frame.Width;
    f.Height = contentLbl.Frame.Bottom + 20;
    contentView.Frame = f;
    

    Then set the ContentSize of the scroll view to the size of the contentView. If the size is larger than the scroll view’s size — which should be the case with the Lorem Ipsum text — you’ll get your scrolling.

    scrollView.ContentSize = contentView.Frame.Size;
    

    Cheers.

    [Edit: fixed typo for ‘width’ vs. ‘height’]

    [Edit: simplest scroll view]

    Here’s the simplest scroll view I could come up with. Dragging up and down on the red area should show the scroll bar. When you get to the bottom, the green background of the scroll view should show.

    var v =  new UIView( new RectangleF(0,0,200,1000));
    v.BackgroundColor = UIColor.Red;
    
    var sv = new UIScrollView( new RectangleF(20,20,200,300));
    sv.BackgroundColor = UIColor.Green;
    sv.ContentSize = v.Frame.Size;
    
    View.AddSubview(sv);
    sv.AddSubview(v);
    

    [Edit: an even simpler scroll view]
    I created a scroll view in XCode and simply set the background color and the content size to be larger than the scroll view’s size. When dragging on the blue scroll view, the scroll bars should appear.

    designerScrollView.BackgroundColor = UIColor.Blue;
    designerScrollView.ContentSize = new SizeF(1000,1000);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three button named(titled) hello, nothing, heaven and one label (IBOutlet UIlabel lab).
I have a UILabel that displays some chars. Like x, y or rpm. How
I have a UILabel with right-aligned text that may vary from between three to
In my application I have a UILabel that holds an expanding number of entries
HI have a custom view class that is loaded and placed within my main
I want to display Image,Title name,and two left right buttons. That I have done
I have a UILabel that has a formatted String (formatted for currency), so there
I have a subclass of a UILabel that overloads initWithCoder and I was wondering
I have a UILabel and i have a uint16_t. How can I display the
I have three UILabel with different font and I want to put then on

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.