你能将TabViewController和PageViewController连接到一个ViewController吗?

时间:2014-04-06 01:13:46

标签: ios xcode uiviewcontroller uipagecontrol tabbarcontroller

我正在构建一个TabBar iOS应用程序,并且在其中一个选项卡式视图中,我想放置一个pagecontrol对象,我希望用户可以在该对象中滑动3个页面(在同一个选项卡视图中)。我该怎么做?

1 个答案:

答案 0 :(得分:0)

我把测试代码,请尝试一下。 此代码不使用故事板。

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