i'm developing an application for iPhone using Swift 3 and i have some doubts about the navigation between pages inside a ViewController.
On my android application, i have an activity using a ViewPager com two fragments. Each fragments have a different implementation and interacts with the main activity.
Like this:
and result is:
My question is how i can do it in swift?
There is no direct control available in ios, Anyway if you want achieve as same as android view pager functionalities and effects, use below controls
MainViewController{
//Design your main controller
// Place Container view in main controller (You can give custom height for container view)
}
create new view controller which extends UIPageViewController
MyPagerViewController : UIPageViewController{
// this will be directly connected to container view of Main view controller
}
MyPagerViewController : UIPageViewController {
var selectedBlock:Int = 1
required init?(coder aDecoder: NSCoder) {
super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self;
delegate = self;
// Setting First View
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController],
direction: .forward,
animated: true,
completion: nil)
}
}
// List of UIView Controllers
private(set) lazy var orderedViewControllers: [UIViewController] = {
return [self.newColoredViewController(name: “Green”),
self.newColoredViewController(name: “Yellow”)]
}()
private func newColoredViewController(name: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil) .
instantiateViewController(withIdentifier: "\(name)ViewController")
}
}
extension MyPagerViewController: UIPageViewControllerDataSource, UIPageViewControllerDelegate {
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
//selectedBlock = viewControllerIndex+1
// User is on the first view controller and swiped left to loop to
// the last view controller.
guard previousIndex >= 0 else {
return nil//orderedViewControllers.last
}
guard orderedViewControllers.count > previousIndex else {
return nil
}
return orderedViewControllers[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
//selectedBlock = viewControllerIndex+1
// print("**********" + String(selectedBlock));
let orderedViewControllersCount = orderedViewControllers.count
// User is on the last view controller and swiped right to loop to
// the first view controller.
guard orderedViewControllersCount != nextIndex else {
return nil//orderedViewControllers.first
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
return orderedViewControllers[nextIndex]enter code here
}
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool){
if(completed){
if pageViewController.viewControllers![0] is GreenViewController {
selectedBlock = 1 // this is global variable
}else{
selectedBlock = 2
}
}
}
}