如何在pageviewcontroller上调整viewcontroller大小

时间:2013-09-16 03:35:06

标签: ios uipageviewcontroller

我正在使用pageviewcontroller尝试应用
我添加了一个viewcontroller作为childviewcontoller 但正如您在图中看到的那样,红色空间出现在顶部 我怎么能摆脱它?

pageviewcontroller

提前致谢。

添加了代码

#import <UIKit/UIKit.h>
#import "ChildViewController.h"

@interface ViewController :      UIViewController<UIPageViewControllerDelegate,UIPageViewControllerDataSource>
{
    UIPageViewController *pageViewController;
    int pageIndex;
    int pageMax;
}
- (ChildViewController*)setChildViewController;
- (int)getPageIndex:(ChildViewController*)vc;

@end



#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor redColor];

    //Init Page
    pageIndex = 5;

    //Max Page Num
    pageMax = 10;

    //Declare UIPageViewController
    pageViewController = [[UIPageViewController alloc]
                      initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl     //アニメーション
                          navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal //水平
                      options:nil];

    //Set delegate and datasource
    pageViewController.delegate = self;
    pageViewController.dataSource = self;
    pageViewController.view.frame = self.view.frame;

    //Instantiate VC
    UIViewController* vc = [self setChildViewController];

    //Set VC
    NSArray *viewControllers = [NSArray arrayWithObject:vc];
    [pageViewController setViewControllers:viewControllers
                             direction:UIPageViewControllerNavigationDirectionForward
                              animated:NO
                            completion:NULL];

    [self addChildViewController:pageViewController];

    [self.view addSubview:pageViewController.view];

}

//Instantiate VC
- (ChildViewController*)setChildViewController{
    ChildViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ChildViewController"];
    //ShowViewController* vc = [[ShowViewController alloc] init];
    vc.pageIndex = pageIndex;
    return vc;
}

/*
 * Back
 */
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
  viewControllerBeforeViewController:(UIViewController *)viewController
{

    //Get Current Index
    pageIndex = [self getPageIndex:(ChildViewController*)viewController];

    //MIN
    if (1 == pageIndex) {
        return nil;
    }

    pageIndex--;


    //Return VC
    ChildViewController* vc = [self setChildViewController];

    return vc;
}


/*
 * Forward
 */
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
   viewControllerAfterViewController:(UIViewController *)viewController
{
    //Get Current Index
    pageIndex = [self getPageIndex:(ChildViewController*)viewController];

    //MAX
    if (pageMax == pageIndex) {
        return nil;
    }

    pageIndex++;

    ChildViewController* vc = [self setChildViewController];

    return vc;
}

//Finish Animation
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:    (BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{

    //Set Page
    if (completed) {
        ChildViewController* vc = [previousViewControllers objectAtIndex:0];
        vc.pageIndex = pageIndex;
    }
}

/*
 * Get Current Page
 */
- (int)getPageIndex:(ChildViewController*)vc
{
    return vc.pageIndex;
}

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

@end


#import <UIKit/UIKit.h>

@interface ChildViewController : UIViewController
@property int pageIndex;
@property (retain, nonatomic) IBOutlet UILabel *pageLabel;

- (void)setIndex:(int)index;

@end


#import "ChildViewController.h"

@interface ChildViewController ()

@end

@implementation ChildViewController
@synthesize pageIndex;
@synthesize pageLabel;

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    pageLabel.text = [NSString stringWithFormat:@"Page %d",pageIndex];
    self.view.backgroundColor = [UIColor greenColor];
}

//Keep Current Page
- (void)setIndex:(int)index{
    pageIndex = index;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

2 个答案:

答案 0 :(得分:0)

我希望这会奏效。 。

-(void) loadView
{

    UIView * pView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.view = pView ;

    [pView release];

    pView = nil;

}

答案 1 :(得分:0)

你可以尝试这行代码可能这可以解决你的问题,如果你仍然面临问题然后评论我

 UIPageViewController *pageViewController
[pageViewController.view setFrame:self.view.frame];