PageViewController With Custom UIPageControl Full Code...
IN STORYBOARD:
add page control to ViewController and add one pageViewController with storyboard id pageContainer & 5 viewControllers with storyboard id's Page1 Page2 Page3 Page4 Page5...
@interface ViewController : UIViewController<UIPageViewControllerDelegate, UIPageViewControllerDataSource>
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@end
#import "ViewController.h"
#import "Page3.h"
@interface ViewController ()
@property (nonatomic, strong) UIPageViewController *pageContainer;
@property (nonatomic, strong) NSArray *pages;
@property NSUInteger currentindex;
@property NSUInteger pendingIndex;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Setup the pages
UIViewController *page1 = [self.storyboard instantiateViewControllerWithIdentifier:@"page1"];
UIViewController *page2 = [self.storyboard instantiateViewControllerWithIdentifier:@"page2"];
UIViewController *page3 = [self.storyboard instantiateViewControllerWithIdentifier:@"page3"];
UIViewController *page4 = [self.storyboard instantiateViewControllerWithIdentifier:@"page4"];
UIViewController *page5 = [self.storyboard instantiateViewControllerWithIdentifier:@"page5"];
_pages = [[NSArray alloc]initWithObjects:page1,page2,page3,page4,page5, nil];
// Create the page container
_pageContainer = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
_pageContainer.delegate = self;
_pageContainer.dataSource = self;
NSArray *startingViewControllers =[NSArray arrayWithObject:page1];
[_pageContainer setViewControllers:startingViewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
// Add it to the view
[self.view addSubview:_pageContainer.view];
// Configure our custom pageControl
[self.view bringSubviewToFront:_pageControl];
_pageControl.numberOfPages = _pages.count;
_pageControl.currentPage = 0;
}
#pragma mark - UIPageViewController delegates
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
_currentindex = [_pages indexOfObject:viewController];
if (_currentindex == 0) {
return nil;
}
NSUInteger previousIndex = ((_currentindex - 1) % _pages.count);
return _pages[previousIndex];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
_currentindex = [_pages indexOfObject:viewController];
if (_currentindex == _pages.count-1) {
return nil;
}
NSUInteger nextIndex = ((_currentindex + 1) % _pages.count);
return _pages[nextIndex];
}
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers NS_AVAILABLE_IOS(6_0); {
_pendingIndex = [_pages indexOfObject:pendingViewControllers.firstObject];
}
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed;
{
if (completed) {
_currentindex = _pendingIndex;
if ( _currentindex) {
_pageControl.currentPage = _currentindex;
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
No comments:
Post a Comment