我正在构建应用,而我有一个UIPageViewController
,其中包含2 UITableViewControllers
。
这是VC头文件:
#import <UIKit/UIKit.h>
@interface EMPageViewController : UIViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate>
// init
-(id)initWithViewControllers:(NSArray<UIViewController*> *)viewControllers showDots:(BOOL)showDots pageSpacing:(CGFloat)pageSpacing frame:(CGRect)frame;
// Properties
@property (nonatomic, strong,readonly) UIPageViewController *pageViewController;
@property (nonatomic, strong, readonly) NSArray *navViewControllers;
@end
和实施文件:
#import "EMPageViewController.h"
@interface EMPageViewController ()
@property (nonatomic, strong, readwrite) UIPageViewController *pageViewController;
@property (nonatomic, strong, readwrite) NSArray *navViewControllers;
@end
@implementation EMPageViewController
-(id)initWithViewControllers:(NSArray<UIViewController*> *)viewControllers showDots:(BOOL)showDots pageSpacing:(CGFloat)pageSpacing frame:(CGRect)frame
{
if (self = [super init])
{
/* init pageViewController */
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:@{UIPageViewControllerOptionInterPageSpacingKey : [NSNumber numberWithFloat:pageSpacing]}];
self.pageViewController.dataSource = self;
self.pageViewController.delegate = self;
self.pageViewController.view.frame = self.view.frame;
[self.pageViewController willMoveToParentViewController:self];
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
if (!showDots)
{
/* remove dots */
NSArray *subviews = self.pageViewController.view.subviews;
UIPageControl *bottomControl = nil;
for (int i=0; i<[subviews count]; i++) {
if ([[subviews objectAtIndex:i] isKindOfClass:[UIPageControl class]]) {
bottomControl = (UIPageControl *)[subviews objectAtIndex:i];
}
}
bottomControl.hidden = true;
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height+40);
}
/* set viewcontrollers */
self.navViewControllers = [NSArray arrayWithArray:viewControllers];
[self.pageViewController setViewControllers:@[self.navViewControllers[0]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
/* set other properties */
self.view.frame = frame;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - page view controller data source
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSInteger currentIndex = [self.navViewControllers indexOfObject:viewController];
if (currentIndex != 0)
{
return self.navViewControllers[currentIndex - 1];
} else
{
return nil;
}
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSInteger currentIndex = [self.navViewControllers indexOfObject:viewController];
if (currentIndex != ([self.navViewControllers count] - 1))
{
return self.navViewControllers[currentIndex + 1];
} else
{
return nil;
}
}
-(NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return self.navViewControllers.count;
}
-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return 0;
}
此控制器来自另一个VC,如下所示:
PartyTableViewController *VC1 = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstVC"];
MessagingViewController *VC2 = [[OtherVC new];
UINavigationController *NC2 = [[UINavigationController alloc] initWithRootViewController:VC2];
EMPageViewController *pageViewController = [[EMPageViewController alloc] initWithViewControllers:@[VC1, NC2] showDots:NO pageSpacing:3.0f frame:self.view.frame];
[self.navigationController pushViewController:pageViewController animated:YES];
会发生什么?
当显示页面VC时,我的第一根手指滑动不会做任何事情 - 但第二次完全滑动它。因此,只有其他所有滑动工作。
VC1和VC2都属于UITableViewController
。
知道为什么吗?