我目前将我的布局设计设置为一个 View Controller 上的全屏 ScrollView ,我在其中添加其他 View Controller 作为 subview 以创建分页效果。在普通的 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
我在 Storyboard上有安全区域。
最佳答案
您可以使用 safe area layout guide
执行此操作,并使用链接获取更多信息:
您也可以在没有安全区域的情况下执行此操作:我为您准备了一个演示,在这个演示中,我们在 ScrollView 上添加了三个 View Controller ,在另一个 View Controller 上添加了 ScrollView (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))`
您可以检查演示项目的所有功能和限制。下载地址:https://www.dropbox.com/s/4ovfqmrtwt2i8yi/StackOverflow.zip?dl=0
我已经在iPhoneX
和iPhone6
和iPhone8+
上测试了这个demo
Screenshots are below:
关于iOS 11 Swift 4 iPhone X Safe Area 支持全屏ScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49824645/