我正在尝试使用 pageviewcontroller 的应用程序。
我添加了一个 viewcontroller 作为 childviewcontoller。
但正如你在图中看到的,红色的空间出现在顶部。
我怎样才能摆脱它?
提前致谢。
添加代码
#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