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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:22:09+00:00 2026-05-31T21:22:09+00:00

I am building an iPad app with the main screen composed of a UIScrollView

  • 0

I am building an iPad app with the main screen composed of a UIScrollView populated with a dynamic number of UIViews of fixed size. I am currently creating the UIViews programmatically and manually placing them by setting the center (setCenter) prior to adding to the scrollview. The functionality looks very much like a simplified version of the opening screen of Pages on the iPad. I am programmatically changing the vertical length of the scrollview container when each row is filled with UIViews. I have to manually place each newly created UIView since the number will be dynamically determined by the number of objects created by the user.

Since I also want to allow for orientation change, I am using the notification center to capture orientation change events. I want to leave the individual UIViews the same size regardless of orientation, but need to reorder them with orientation change since the portrait orientation will have one less UIView per row.

The only way I can think to reorder the UIViews within the scrollview is to programmatically change each of the UIView centers (setCenter), wrapping them in an animation.

Is there any other way to reorder the UIViews that I am not thinking about besides walking the subviews and adjusting their center positions?

Thanks!

  • 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-05-31T21:22:10+00:00Added an answer on May 31, 2026 at 9:22 pm

    When you have a view that needs to lay out its subviews in a specific way, the general approach is to create your own view subclass and override its layoutSubviews method. The system automatically sends layoutSubviews to a view at various times, including whenever the view’s size changes. If the system is autorotating, it will send you layoutSubviews from inside the autorotation animation block, so you don’t need to worry about animating the changes to your subviews’ frames.

    This is made slightly trickier when your view is a scroll view, because UIScrollView sends itself the layoutSubview messages a lot – every time it changes it contentOffset. So if you subclass UIScrollView, you want to be efficient. You need to keep track of the last size you laid out for, and only lay out again if the size has changed. Something like this:

    @implementation MyScrollView {
        CGSize _lastLayoutSize;
    }
    
    - (void)forceLayout {
        _lastLayoutSize = CGSizeMake(-1, -1);
        [self setNeedsLayout];
    }
    
    - (void)didAddSubview:(UIView *)view {
        // UIView sends itself this whenever a subview is added.
        [super didAddSubview:view];
        [self forceLayout];
    }
    
    - (void)didRemoveSubview:(UIView *)view {
        // UIView sends itself this whenever a subview is removed.
        [super didAddSubview:view];
        [self setNeedsLayout];
    }
    
    - (void)layoutSubviews {
        [super layoutSubviews];
    
        CGSize mySize = self.bounds.size;
        if (CGSizeEqualToSize(mySize, _lastLayoutSize))
            return;
        _lastLayoutSize = mySize;
    
        static CGFloat const margin = 8;
        CGFloat yMax = 0;
        CGPoint point = CGPointMake(margin, margin);
        BOOL rowIsEmpty = YES;
        for (UIView *subview in self.subviews) {
            CGRect frame = subview.frame;
            CGFloat xNext = point.x + frame.size.width + margin;
            if (rowIsEmpty || xNext <= mySize.width) {
                rowIsEmpty = NO;
                point.x = xNext;
            } else {
                point.x = margin;
                point.y = yMax + margin;
            }
            frame.origin = point;
            subview.frame = frame;
            yMax = MAX(yMax, CGRectGetMaxY(frame));        
        }
    
        self.contentSize = CGSizeMake(mySize.width, yMax + margin);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Building an iPad app. I have a button on my main view that I
I am building an iPad app using Flash CS6 to compile to AIR 3.3
I'm building an iPad app with views that are split horizontally and animate in
I'm building an iPad app, and would like to use a tiled display with
I am building an iPad app that has a split view controller. Is it
I am building a ios app for ipad, in which the user will be
I'm building an app for the iPad and am a little confused as to
I am building a venue layout app for the ipad that will use different
I'm building an iPad app and I wish to popup a uipickerview when the
I'm building iPhone / iPad app with Xcode. now i want to take screenshot

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.