【发布时间】:2018-04-13 09:32:36
【问题描述】:
我正在制作一个通用的演示 xcode 项目,它是一个 UIScrollView 包含两个页面,左右滚动来回。
我的问题是 iPad 和 iPhone 的布局不一样。
- 我在情节提要中创建了 3 个视图控制器,如下所示:
ViewController 具有UIScrollView。
AViewController 在中心包含一个UILabel("Good morning...")。
BViewController 在中心包含一个UILabel("Good night...")。
- UILabels 的约束是:
-
这里是
ViewController的代码:class ViewController: UIViewController { @IBOutlet weak var scrollView: UIScrollView! override func viewDidLoad() { super.viewDidLoad() let story = UIStoryboard(name: "Main", bundle: nil) let vc1 = story.instantiateViewController(withIdentifier: "AViewController") let vc2 = story.instantiateViewController(withIdentifier: "BViewController") vc1.view.backgroundColor = UIColor.green vc2.view.backgroundColor = UIColor.red addContentView([vc1, vc2]) scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width * 2, height: scrollView.frame.height) } func addContentView(_ viewControllers: [UIViewController]) { viewControllers.enumerated().forEach { addChildViewController($1) $1.view.frame = CGRect(x: UIScreen.main.bounds.width * CGFloat($0), y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height) scrollView.addSubview($1.view) didMove(toParentViewController: $1) } }}
如果我选择 以 iPhone 8 方式查看,那么布局在 iPhone 8 中可以正常工作,如下所示:(然后从绿色滑动到红色)
但对于 iPad:
标签没有居中!!。
如果我选择 View as iPad ... ,那么对于 iPhone,它的布局不正确。
这是视图层次结构,很明显布局是正确的,但由于某种原因,绿色视图大于屏幕尺寸并被红色视图覆盖。
感谢您的帮助。
【问题讨论】:
标签: ios swift uiscrollview autolayout uicontainerview