I have created top design in MainSegPageViewController in storyboard like this and i have 2 other viewcontrollers as well. now i need same upper design in my all three viewcontrollers. so how to show that 2 viewcontrollers in this wight area only? is that possible? if yes please explain me
code: with this code initially i am getting ViewController1 why? how to show MainSegPageViewController initially? and how to show ViewController1 in white area when i click STEP 2. please guide me.
import UIKit
class MainSegPageViewController: UIPageViewController {
private(set) lazy var subViewcontrollers: [UIViewController] = {
return [
UIStoryboard(name: "Main", bundle: nil) .
instantiateViewController(withIdentifier: "ViewController1"),
UIStoryboard(name: "Main", bundle: nil) .
instantiateViewController(withIdentifier: "ViewController2")
]
}()
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
modalPresentationStyle = .formSheet
if let firstViewController = subViewcontrollers.first {
setViewControllers([firstViewController],
direction: .forward,
animated: true,
completion: nil)
}
setUpNavigationBar(with: "Pagination vc", isBackNeed: true)
self.navigationController?.navigationBar.isHidden = false
}
}
// MARK: UIPageViewControllerDataSource
extension MainSegPageViewController: UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = subViewcontrollers.firstIndex(of:viewController)
else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return nil
}
guard subViewcontrollers.count > previousIndex else {
return nil
}
if previousIndex == 2 {
return nil
}
return subViewcontrollers[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = subViewcontrollers.firstIndex(of:viewController)
else {
return nil
}
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = subViewcontrollers.count
guard orderedViewControllersCount != nextIndex else {
return nil
}
guard orderedViewControllersCount > nextIndex else {
return nil
}
if nextIndex == 3 {
return nil
}
return subViewcontrollers[nextIndex]
}
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return subViewcontrollers.count
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
guard let firstViewController = viewControllers?.first, let firstViewControllerIndex = subViewcontrollers.firstIndex(of: firstViewController) else {
return 0
}
return firstViewControllerIndex
}
}
And i am reaching to MainSegPageViewController through side menu like this
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "MainSegPageViewController") as? MainSegPageViewController
self.navigationController?.pushViewController(vc!, animated: true)
o/p: with the code initially i am getting ViewController1 screen but i need MainSegPageViewController to show first.


To achieve your layout, one approach is to create a "Profile" view controller containing your "Welcome to Profile" label and
UISegmentedControl, and then embed theUIPageViewControllerin a Container View.So, since your post says you are pushing to this layout in a navigation controller, we start with this in Storyboard:
The button will be connected to this code:
Then, layout
ProfileViewControllerin Storyboard like this:The large view is a
UIContainerView:and we've embedded a typical
UIPageViewControllerin it.Assuming you have working code for your page view controller, this can be run and you'll be able to swipe back and forth through the pages.
Next, we need to add some code so tapping on a Segment will change the page.
We can setup the
ProfileViewControllerlike this:When embedding a controller in a Container View, we automatically get a
seguewhere we can grab a reference to the embedded page view controller.If we add this func to our
MainSegPageViewControllerclass:we can now change the page from the segmented control.
Finally, we need to add a
closureso the page view controller can inform the "parent" controller when the user drags to a new page.So, in
MainSegPageViewControllerclass, we add this property:and implement:
We set that closure back in
prepare(for segue:...)back inProfileViewController:I've put up a complete project here - https://github.com/DonMag/ContainerPageViewController so you can inspect the code and Storyboard -- and see it in action -- while building your own.