Question

I'm Building a TabBar iOS application, and on one of the tabbed views I want to place a pagecontrol object in which I want to the user to be able to swipe through 3 pages (in the same tabbed view). How do I do so?

Was it helpful?

Solution

I put the test code, please try it. This code does NOT using storyboard.

TMTabBarViewController.h

#import <UIKit/UIKit.h>

@interface TMTabBarViewController : UITabBarController

@end

TMTabBarViewController.m

#import "TMTabBarViewController.h"    
#import "TMPagingViewController.h"

@interface TMTabBarViewController ()

@end

@implementation TMTabBarViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)loadView
{
    [super loadView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    TMPagingViewController *pagingViewController1 = [[TMPagingViewController alloc] initWithNibName:nil     bundle:nil];
    pagingViewController1.title = @"1";

    TMPagingViewController *pagingViewController2 = [[TMPagingViewController alloc] initWithNibName:nil     bundle:nil];
    pagingViewController2.title = @"2";

   [self setViewControllers:@[
                              pagingViewController1,
                              pagingViewController2
                              ]
                   animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

TMPagingViewController.h

#import <UIKit/UIKit.h>

@interface TMPagingViewController : UIViewController <UIScrollViewDelegate>

@property (nonatomic) UIScrollView *scrollView;
@property (nonatomic) UIPageControl *pageControl;

@end

TMPagingViewController.m

#import "TMPagingViewController.h"

@interface TMPagingViewController ()

@end

@implementation TMPagingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)loadView
{
    [super loadView];

    _scrollView = [[UIScrollView alloc] init];
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    _scrollView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_scrollView];

    _pageControl = [[UIPageControl alloc] init];
    _pageControl.numberOfPages = 3;
    [self.view addSubview:_pageControl];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    CGSize viewSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - self.    tabBarController.tabBar.frame.size.height);
    _scrollView.frame = CGRectMake(0.0f, 0.0f, viewSize.width, viewSize.height);
    _scrollView.contentSize = CGSizeMake(viewSize.width * _pageControl.numberOfPages, viewSize.height);

    CGFloat pageControlHeight = 50.0f;
    _pageControl.frame = CGRectMake(0.0f, viewSize.height - pageControlHeight, viewSize.width,     pageControlHeight);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark -
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.scrollView == scrollView) {
        CGFloat pageWidth = self.scrollView.frame.size.width;
        if ((NSInteger)fmod(self.scrollView.contentOffset.x , pageWidth) == 0) {
            // set pageControl indicator to current page.
            _pageControl.currentPage = self.scrollView.contentOffset.x / pageWidth;
        }
    }
}

@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top