【发布时间】:2018-03-01 22:33:12
【问题描述】:
我正在尝试复制一个类似 Tinder 的菜单,其中 3 个 UIViewControllers 在 UIScrollView 中,顶部有一个自定义菜单选项卡,每个 UIViewController 都有一个按钮。我遇到了一个有趣的问题,UIViewControllers 视图完全适合 scrollView.frame,但仅适用于 iPhone 8。相比之下,对于 iPhone SE,它会留下白边,对于 iPhone 8+,它似乎与视图重叠在 scrollView.view 中。有人可以解释为什么会发生这种情况以及我该如何解决?
这是我的代码,我正在调整在我的UIScrollView 中设置我的UIViewControllers:
import UIKit
class MainViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var navigationView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
setUpHorizontalScrollViews()
}
func setUpHorizontalScrollViews(){
let view = (
x: self.view.bounds.origin.x,
y: self.view.bounds.origin.y,
width: self.view.bounds.width,
height: self.view.bounds.height
)
let scrollWidth = 3 * view.width
let scrollHeight = view.height
scrollView.contentSize = CGSize(width: scrollWidth, height: scrollHeight)
scrollView.contentOffset.x = view.x
if let messagesView = self.storyboard?.instantiateViewController(withIdentifier: "MessagesVC") as UIViewController! {
self.addChildViewController(messagesView)
self.scrollView.addSubview(messagesView.view)
messagesView.didMove(toParentViewController: self)
messagesView.view.frame = CGRect(x: 0,
y: 0,
width: view.width,
height: view.height
)
}
if let friendsView = self.storyboard?.instantiateViewController(withIdentifier: "FriendsVC") as UIViewController! {
self.addChildViewController(friendsView)
self.scrollView.addSubview(friendsView.view)
friendsView.didMove(toParentViewController: self)
friendsView.view.frame = CGRect(x: view.width,
y: 0,
width: view.width,
height: view.height
)
}
if let settingsView = self.storyboard?.instantiateViewController(withIdentifier: "SettingsVC") as UIViewController! {
self.addChildViewController(settingsView)
self.scrollView.addSubview(settingsView.view)
settingsView.didMove(toParentViewController: self)
settingsView.view.frame = CGRect(x: 2 * view.width,
y: 0,
width: view.width,
height: view.height
)
}
// offset to the second view
self.scrollView.contentOffset.x = view.width
}
override var shouldAutorotate: Bool {
return false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这就是我的安装程序的样子。顶部是 MainViewController,包含 UIScrollView,底部是应该进入 scrollView 的 3 个视图控制器。
这是我希望它的样子,以及它在 iPhone 8 中的设置方式:
这是它在 iPhone SE 上的样子,也是我的问题所在:
【问题讨论】:
标签: swift uiview uiviewcontroller uiscrollview frame