use*_*705 13 ios swift safearealayoutguide iphone-x
我目前将我的布局设计设置为一个视图控制器上的全屏滚动视图,其中我将其他视图控制器添加为子视图以创建分页效果.在普通的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
故事板上有安全区域.
您可以safe area layout guide使用以下链接执行此操作并获取更多信息:
您也可以在没有安全区域的情况下执行此操作:我为您准备了一个演示,在此演示中,我们在滚动视图上添加了三个视图控制器,并在另一个视图控制器上添加了滚动视图(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)
    }
}
注意:从安全区域移除顶部和底部约束并添加它们
SuperView(对于滚动视图,InnerView(FirstVC,SecondVC,ThirdVC))`
您可以检查演示项目的所有功能和约束。下载地址:https : //www.dropbox.com/s/4ovfqmrtwt2i8yi/StackOverflow.zip?dl=0
我在测试的演示iPhoneX,并iPhone6与iPhone8+
截图如下:
| 归档时间: | 
 | 
| 查看次数: | 6067 次 | 
| 最近记录: |