iOS / Swift 3:UIPageController 中的 UIView 太大
Posted
技术标签:
【中文标题】iOS / Swift 3:UIPageController 中的 UIView 太大【英文标题】:iOS / Swift 3: UIView too big within UIPageController 【发布时间】:2017-03-23 23:59:20 【问题描述】:我在 ContainerView 中嵌入了一个 PageController。 为什么我在 PageController 中显示的视图没有缩放以适应 PageController?我怎样才能实现“适应规模”? (至少 PageController 本身会根据 ContainerView 调整它的大小)
我的课程是:
import UIKit
class YellowController : UIViewController
class GreenController : UIViewController
//from a tutorial:
class PageController : UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate
// from *** to avoid the black box at the bottom of the pagecontroller
override func viewDidLayoutSubviews()
super.viewDidLayoutSubviews()
for view in self.view.subviews
if view is UIScrollView
view.frame = UIScreen.main.bounds
else
view.backgroundColor = UIColor.clear
lazy var VCArray: [UIViewController] =
return [self.VCInstance(name: "InfoPage"),
self.VCInstance(name: "FragePage"),
self.VCInstance(name: "AntwortPage") ]
()
private func VCInstance (name: String) -> UIViewController
let VCC = UIStoryboard(name: "Lerneinheit", bundle: nil).instantiateViewController(withIdentifier: name)
return VCC
override func viewDidLoad()
super.viewDidLoad()
self.dataSource = self
self.delegate = self
if let InfoPage = VCArray.first
setViewControllers([InfoPage], direction: .forward, animated: true, completion: nil)
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
guard let viewControllerIndex = VCArray.index(of: viewController)
else
return nil
if (viewControllerIndex<1)
return nil
return VCArray[viewControllerIndex - 1]
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
guard let viewControllerIndex = VCArray.index(of: viewController)
else
return nil
if viewControllerIndex == VCArray.count-1
return nil
return VCArray[viewControllerIndex + 1]
public func presentationCount(for pageViewController: UIPageViewController) -> Int
return VCArray.count
public func presentationIndex(for pageViewController: UIPageViewController) -> Int
guard let firstViewController = viewControllers?.first, let firstViewControllerIndex = VCArray.index(of: firstViewController)
else
return 0
return firstViewControllerIndex
IB: 切割黄色视图而不是缩放以适应 PageController 的结果: 黄色视图的约束:
【问题讨论】:
您需要提供更多数据。 “两个控制器”实际上是否包含嵌入标签内的滚动视图?您是否正确使用自动布局?您如何将 VC 的“馈送”到 UIPage ?提供剪辑代码会有所帮助 您的 txtInfo 在该 VC 中是否可滚动?另外,看起来你有一个灰色的约束,请删除它。 是的,它是可滚动的。 所以你添加了一个文本视图,你抱怨为什么它是可滚动的? 问题是,黄色视图大小未调整为 PageControllers 大小。这不是 TextField 的问题。 【参考方案1】:下面的代码是多余的,删除它:
override func viewDidLayoutSubviews()
super.viewDidLayoutSubviews()
for view in self.view.subviews
if view is UIScrollView
view.frame = UIScreen.main.bounds
else
view.backgroundColor = UIColor.clear
【讨论】:
以上是关于iOS / Swift 3:UIPageController 中的 UIView 太大的主要内容,如果未能解决你的问题,请参考以下文章