iOS 11 Swift 4 iPhone X Safe Area 支持全屏ScrollView
iOS 11 Swift 4 iPhone X Safe Area Support of full screen ScrollView
我目前将我的布局设计设置为一个视图控制器上的全屏滚动视图,我在其中添加其他视图控制器作为子视图以创建分页效果。在正常的 iphone 屏幕上,它工作得很好。然而,当 运行 在 iPhone X 上时,事情似乎偏离了中心,我可以在一页中多次翻页。
这是我设置滚动视图的代码
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
我在故事板上有安全区域。
You can solve this easily.
1
2。
您可以使用 safe area layout guide
执行此操作并使用 link:
获取更多信息
你也可以在没有安全区域的情况下这样做:我为你准备了一个演示,在这个演示中,我们在滚动视图上添加了三个视图控制器,在另一个视图控制器上添加了滚动视图( 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))`
您可以检查演示项目的所有功能和限制。下载URL:https://www.dropbox.com/s/4ovfqmrtwt2i8yi/Whosebug.zip?dl=0
我已经在iPhoneX
和iPhone6
和iPhone8+
中测试了演示
Screenshots are below:
使用安全区域设置约束
在这里你可以结帐...
https://developer.apple.com/ios/human-interface-guidelines/overview/iphone-x/
一旦您将滚动视图约束设置为安全区域..select 您的 viewController 场景和
取消选中 Adjust scroll view insets
属性.
已更新
if #available(iOS 11.0, *) {
fullScreenScrollView?.contentInsetAdjustmentBehavior = .always
}
我目前将我的布局设计设置为一个视图控制器上的全屏滚动视图,我在其中添加其他视图控制器作为子视图以创建分页效果。在正常的 iphone 屏幕上,它工作得很好。然而,当 运行 在 iPhone X 上时,事情似乎偏离了中心,我可以在一页中多次翻页。
这是我设置滚动视图的代码
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
我在故事板上有安全区域。
You can solve this easily.
1
2。
您可以使用 safe area layout guide
执行此操作并使用 link:
你也可以在没有安全区域的情况下这样做:我为你准备了一个演示,在这个演示中,我们在滚动视图上添加了三个视图控制器,在另一个视图控制器上添加了滚动视图( 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))`
您可以检查演示项目的所有功能和限制。下载URL:https://www.dropbox.com/s/4ovfqmrtwt2i8yi/Whosebug.zip?dl=0
我已经在iPhoneX
和iPhone6
和iPhone8+
Screenshots are below:
使用安全区域设置约束 在这里你可以结帐...
https://developer.apple.com/ios/human-interface-guidelines/overview/iphone-x/
一旦您将滚动视图约束设置为安全区域..select 您的 viewController 场景和
取消选中 Adjust scroll view insets
属性.
已更新
if #available(iOS 11.0, *) {
fullScreenScrollView?.contentInsetAdjustmentBehavior = .always
}