我使用UIPageViewControl
实现了一个不错的PageControl
。当滑动指示器改变并显示正确的当前页面时。
但是我注意到当前页面指示器切换所需的全部是开始滑动。这意味着如果我开始滑动但是然后松开手指,当前页面指示器已经切换,就像页面已经切换一样,但它没有。这是我用来进行切换的代码:
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
NSUInteger index = [self.navigationController.viewControllers indexOfObject:viewController];
self.pageControl.currentPage = index;
}
我注意到的另一件事是,当我向右和向左滑动更改视图时,页面指示器只是卡住而不会移动。
所以它只适用于你不快速更改视图的情况。如果您需要任何其他代码,请告诉我们。谢谢。
修改
这是我用来实现我的UIPageViewController的代码。
- (void)viewDidLoad
{
[super viewDidLoad];
// Create the data model
self.navigationItem.title = @"Tabell";
self.identifiers = [[NSMutableArray alloc] init];
[self.identifiers addObject:@"rankTable"];
[self.identifiers addObject:@"page2"];
// Create page view controller
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
self.pageViewController.dataSource = self;
self.pageViewController.delegate = self;
self.navigationController.delegate = self;
CGSize navBarSize = self.navigationController.navigationBar.bounds.size;
CGPoint origin = CGPointMake( navBarSize.width/2, navBarSize.height/2 );
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(origin.x, origin.y+16,
0, 0)]; //Here added 45 to Y and it did the trick
self.pageControl.pageIndicatorTintColor = navbarColor;
self.pageControl.currentPageIndicatorTintColor = [UIColor lightGrayColor];
[self.pageControl setNumberOfPages:2];
[self.navigationController.navigationBar addSubview:self.pageControl];
UITableViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = @[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;
// Change the size of page view controller
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - tabBarHeight);
[self addChildViewController:_pageViewController];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
}
答案 0 :(得分:1)
为什么不实施UIPageViewControllerDelegate
的
- pageViewController: didFinishAnimating: previousViewControllers: transitionCompleted:
方法?它有一个transitionCompleted
布尔值,告诉您是否应更新页面控件。您当前的实现似乎有问题,因为只要您点击一个页面,它就会为下一页加载一个视图控制器并调用...didShow...
方法。希望这可以解决。