iOS 11 Swift 4 iPhone X安全区支持全屏ScrollView

时间:2018-04-13 20:25:20

标签: ios swift iphone-x safearealayoutguide

我目前将我的布局设计设置为一个视图控制器上的全屏滚动视图,其中我将其他视图控制器添加为子视图以创建分页效果。在普通的iPhone屏幕上,它工作得很漂亮。然而,当在iPhone X上运行时,事物似乎偏离中心,我可以在一个页面中多次翻页。

这是我设置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

我在故事板上有安全区域。

enter image description here

enter image description here

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:4)

您可以使用safe area layout guide执行此操作,并使用以下链接获取更多信息:

  

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

您也可以在没有安全区域的情况下执行此操作:我已为您准备了演示,在此演示中,我们在滚动视图上添加了三个视图控制器,并在另一个视图控制器上添加了滚动视图({{1} })

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) } } 添加它们(对于滚动视图,InnerView(FirstVC,SecondVC,ThirdVC))`

您可以检查演示项目的所有功能和约束。下载网址:https://www.dropbox.com/s/4ovfqmrtwt2i8yi/StackOverflow.zip?dl=0

我已在SuperViewiPhoneX以及iPhone6

中测试了该演示
  

屏幕截图如下:

enter image description here

enter image description here

enter image description here

enter image description here

答案 1 :(得分:2)

  

您可以轻松解决此问题。

1 enter image description here

2。 enter image description here

答案 2 :(得分:1)

使用安全区域设置约束 在这里你可以结账......

https://stackoverflow.com/a/45334411/6898523

https://developer.apple.com/ios/human-interface-guidelines/overview/iphone-x/

将scrollview约束设置为安全区域后,选择viewController场景和 取消选中Adjust scroll view insets属性。

enter image description here

<强>更新

if #available(iOS 11.0, *) {
   fullScreenScrollView?.contentInsetAdjustmentBehavior = .always
 }