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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:42:52+00:00 2026-05-28T20:42:52+00:00

I need to dynamically create some views in my app and place some buttons

  • 0

I need to dynamically create some views in my app and place some buttons on them dynamically again. If the amount (count) of the buttons is more then ten I want to place the buttons on a new view and the transitions between the views must be with UIPageControl. Even though I googled and searched from Apple’s Developer page I couldn’t find a way of solving my problem. Can someone please help?

  • 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-28T20:42:53+00:00Added an answer on May 28, 2026 at 8:42 pm

    Add your views as side-by-side subviews of an UIScrollView, using the addSubview method. Then use the UIPageControl with the UIScrollView, as in this example.


    I made a class that manages a UIScrollView, a UIPageControl, and an array of UIViews. It is a simplified version of what I use in my own code. It does the following:

    • Sets up the scroll view to display an array of UIViews. It doesn’t care if the views have been generated dynamically or not.
    • Handles scroll and page control events.
    • Synchronizes the scroll view with the page control.

    PageViewManager.h

    #import <Foundation/Foundation.h>
    
    @interface PageViewManager : NSObject <UIScrollViewDelegate>
    {
        UIScrollView* scrollView_;
        UIPageControl* pageControl_;
        NSArray* pages_;
        BOOL pageControlUsed_;
        NSInteger pageIndex_;
    }
    
    - (id)initWithScrollView:(UIScrollView*)scrollView
                 pageControl:(UIPageControl*)pageControl;
    - (void)loadPages:(NSArray*)pages;
    - (void)loadControllerViews:(NSArray*)pageControllers;
    
    @end
    

    PageViewManager.m

    #import "PageViewManager.h"
    
    @interface PageViewManager ()
    
    - (void)pageControlChanged;
    
    @end
    
    @implementation PageViewManager
    
    - (id)initWithScrollView:(UIScrollView*)scrollView
                 pageControl:(UIPageControl*)pageControl
    {
        self = [super init];
        if (self)
        {
            scrollView_ = scrollView;
            pageControl_ = pageControl;
            pageControlUsed_ = NO;
            pageIndex_ = 0;
    
            [pageControl_ addTarget:self action:@selector(pageControlChanged)
                   forControlEvents:UIControlEventValueChanged];
        }
        return self;
    }
    
    /*  Setup the PageViewManager with an array of UIViews. */
    - (void)loadPages:(NSArray*)pages
    {
        pages_ = pages;
        scrollView_.delegate = self;
        pageControl_.numberOfPages = [pages count];
    
        CGFloat pageWidth  = scrollView_.frame.size.width;
        CGFloat pageHeight = scrollView_.frame.size.height;
    
        scrollView_.pagingEnabled = YES;
        scrollView_.contentSize = CGSizeMake(pageWidth*[pages_ count], pageHeight);
        scrollView_.scrollsToTop = NO;
        scrollView_.delaysContentTouches = NO;
    
        [pages_ enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop)
        {
            UIView* page = obj;
            page.frame = CGRectMake(pageWidth * index, 0,
                                    pageWidth, pageHeight);
            [scrollView_ addSubview:page];
        }];
    }
    
    /*  Setup the PageViewManager with an array of UIViewControllers. */
    - (void)loadControllerViews:(NSArray*)pageControllers
    {
        NSMutableArray* pages = [NSMutableArray arrayWithCapacity:
                                 pageControllers.count];
        [pageControllers enumerateObjectsUsingBlock:
            ^(id obj, NSUInteger idx, BOOL *stop)
            {
                UIViewController* controller = obj;
                [pages addObject:controller.view];
            }];
    
        [self loadPages:pages];
    }
    
    - (void)pageControlChanged
    {
        pageIndex_ = pageControl_.currentPage;
    
        // Set the boolean used when scrolls originate from the page control.
        pageControlUsed_ = YES;
    
        // Update the scroll view to the appropriate page
        CGFloat pageWidth  = scrollView_.frame.size.width;
        CGFloat pageHeight = scrollView_.frame.size.height;
        CGRect rect = CGRectMake(pageWidth * pageIndex_, 0, pageWidth, pageHeight);
        [scrollView_ scrollRectToVisible:rect animated:YES];
    }
    
    - (void)scrollViewDidScroll:(UIScrollView*)sender
    {
        // If the scroll was initiated from the page control, do nothing.
        if (!pageControlUsed_)
        {
            /*  Switch the page control when more than 50% of the previous/next
                page is visible. */
            CGFloat pageWidth = scrollView_.frame.size.width;
            CGFloat xOffset = scrollView_.contentOffset.x;
            int index = floor((xOffset - pageWidth/2) / pageWidth) + 1;
            if (index != pageIndex_)
            {
                pageIndex_ = index;
                pageControl_.currentPage = index;
            }
        }
    }
    
    - (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
    {
        pageControlUsed_ = NO;
    }
    
    - (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView
    {
        pageControlUsed_ = NO;
    }
    
    @end
    

    To use this class, you embed it inside a UIViewController than contains the UIScrollView and the UIPageControl.

    Usage:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        // Create some views dynamically
        UIView* v1 = ...
        UIView* v2 = ...
    
        // Put the views inside an NSArray:
        NSArray* pages_ = [NSArray arrayWithObjects:v1, v2, nil];
    
        /* Create the PageViewManager, which is a member (or property) of this
           UIViewController. The UIScrollView and UIPageControl belong to this 
           UIViewController, but we're letting the PageViewManager manage them for us. */
        pageViewManager_ = [[PageViewManager alloc]
                            initWithScrollView:self.scrollView
                                   pageControl:self.pageControl];
    
        // Make the PageViewManager display our array of UIViews on the UIScrollView.
        [pageViewManager_ loadViews:pages_];
    }
    

    My sample code assumes that you’re using ARC.

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

Sidebar

Related Questions

We need to dynamically create (i.e. during runtime, via code-behind) UserControls and position them
I need to create some bitmaps and save them to the file system. For
I need to dynamically create a Video object in ActionScript 2 and add it
I need to dynamically create textbox. This is my code, but with this I
I need to create several Divs dynamically, and I need to add onmouseover event
I've got some Oracle-Views which I'm using to generate multiple Letters. For Example: CREATE
How dan I dynamically create some public properties on a custom webcontrol. For example,
I want to dynamically create some image from Java and save it to a
I need to dynamically create an array based on a range. I have a
I need to create some simple graphics using java. Basically what I need to

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.