Search code examples
iosswiftiphone-xsafearealayoutguide

iOS 11 Swift 4 iPhone X Safe Area Support of full screen ScrollView


I currently have my layout design setup as a fullscreen scrollview on one view controller in which I add other view controllers as sub views to create a paged effect. On normal iphone screens it works beautifully. However when running on iPhone X, things seem to appear off centered, and I can page in multiple times in one page.

Here is my code for setup of the scrollview

self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.size.height * 3)
        if #available(iOS 11.0, *) {
            self.scrollView.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
        }


        let V1 = self.storyboard?.instantiateViewController(withIdentifier: "S1") as! UINavigationController!
        self.addChildViewController(V1!)
        self.scrollView.addSubview(V1!.view)
        V1?.didMove(toParentViewController: self)
        V1?.view.frame = scrollView.bounds
        myViewsArray.append(V1!)

        let V2 = self.storyboard?.instantiateViewController(withIdentifier: "S2") as UIViewController!
        self.addChildViewController(V2!)
        self.scrollView.addSubview(V2!.view)
        V2?.didMove(toParentViewController: self)
        V2?.view.frame = scrollView.bounds
        myViewsArray.append(V2!)

        var V1Frame: CGRect = V1!.view.frame
        V1Frame.origin.y = 2*self.view.frame.height
        V1?.view.frame = V1Frame

        var V2Frame: CGRect = V2!.view.frame
        V2Frame.origin.y = (self.view.frame.height)
        V2?.view.frame = V2Frame

        V2!.view.alpha = 1

I have safe area on in story board.

enter image description here

enter image description here

enter image description here

enter image description here


Solution

  • You can do this using safe area layout guide and get more info using the link:

    https://medium.com/rosberryapps/ios-safe-area-ca10e919526f

    You can also do this without safe area: I have prepared a demo for you and in this demo, we have three view controllers added on scroll view and scroll view is added on another view controller(ContainerViewController)

    ContainerViewController:

    import UIKit
    
    class ContainerViewController: UIViewController, UIScrollViewDelegate {
    
        @IBOutlet weak var scrollView: UIScrollView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
            let V1 = self.storyboard?.instantiateViewController(withIdentifier: "first")
            self.addChildViewController(V1!)
            self.scrollView.addSubview(V1!.view)
            V1?.didMove(toParentViewController: self)
            V1?.view.frame = scrollView.bounds
    
            let V2 = self.storyboard?.instantiateViewController(withIdentifier: "second")
            self.addChildViewController(V2!)
            self.scrollView.addSubview(V2!.view)
            V2?.didMove(toParentViewController: self)
            V2?.view.frame = scrollView.bounds
    
            let V3 = self.storyboard?.instantiateViewController(withIdentifier: "third")
            self.addChildViewController(V3!)
            self.scrollView.addSubview(V3!.view)
            V3?.didMove(toParentViewController: self)
            V3?.view.frame = scrollView.bounds
    
            var V1Frame: CGRect = V1!.view.frame
            V1Frame.origin.y = 0
            V1?.view.frame = V1Frame
    
            var V2Frame: CGRect = V2!.view.frame
            V2Frame.origin.y = (self.view.frame.height)
            V2?.view.frame = V2Frame
    
            var V3Frame: CGRect = V3!.view.frame
            V3Frame.origin.y = (self.view.frame.height)*2
            V3?.view.frame = V3Frame
        }
        override func viewDidLayoutSubviews() {
            scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height*3)
        }
    }
    

    Note: Remove top and bottom constraint from the safe area and add them from SuperView(for Scroll view, InnerView(FirstVC, SecondVC, ThirdVC))`

    You can check all the functionality and constraints of the demo project. Download URL: https://www.dropbox.com/s/4ovfqmrtwt2i8yi/StackOverflow.zip?dl=0

    I have tested the demo in iPhoneX and iPhone6 and iPhone8+

    Screenshots are below:

    enter image description here

    enter image description here

    enter image description here

    enter image description here