在 UIScrollView Swift3 中加载 UIViewController
Posted
技术标签:
【中文标题】在 UIScrollView Swift3 中加载 UIViewController【英文标题】:Loading UIViewController inside an UIScrollView Swift3 【发布时间】:2017-07-07 10:18:20 【问题描述】:我有 2 个UIViewController
s。
一个UIViewController
有一个带有许多按钮的视图,并且高+2000 点。
另一个UIViewController
有一个UIScrollView
。
这是我的代码:
import UIKit
class ViewController: UIViewController
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
scrollView.contentSize = CGSize(width: 375, height: 2884)
DispatchQueue.main.async
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let thumb_vc = storyboard.instantiateViewController(withIdentifier: "thumb") as! ThumbsViewController
//self.present(vc, animated: true, completion: nil)
self.addChildViewController(thumb_vc)
self.scrollView.addSubview(thumb_vc.view)
虽然拇指 UIViewController
已加载到 UIScrollView
,但大小错误且无法滚动。
任何帮助表示赞赏
【问题讨论】:
在您的项目中使用自动布局或大小类? 【参考方案1】:您遇到了在运行时加载视图时调整大小的问题。几个选项...
// NOT USING constraints / auto-layout
func setupA() -> Void
scrollView.contentSize = CGSize(width: 375, height: 2884)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let vc = storyboard.instantiateViewController(withIdentifier: "thumb") as? ThumbsViewController
// add the child VC
self.addChildViewController(vc)
// don't let "thumbs" view auto-resize
vc.view.autoresizingMask = []
// set "thumbs" view frame size to scroll view content size
vc.view.frame = CGRect(origin: CGPoint.zero, size: scrollView.contentSize)
// add "thumbs" view to the scroll view
self.scrollView.addSubview(vc.view)
// finish child VC process
vc.didMove(toParentViewController: self)
// USING constraints / auto-layout, with hard-coded sizing
func setupB() -> Void
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let vc = storyboard.instantiateViewController(withIdentifier: "thumb") as? ThumbsViewController
// add the child VC
self.addChildViewController(vc)
// add "thumbs" view to the scroll view
self.scrollView.addSubview(vc.view)
// disable auto-resizing mask
vc.view.translatesAutoresizingMaskIntoConstraints = false
// set constraints to both control the size of the "thumbs" view
// as well as the contentSize of the scroll view
// set width and height constraints for the "thumbs" view
vc.view.widthAnchor.constraint(equalToConstant: 375).isActive = true
vc.view.heightAnchor.constraint(equalToConstant: 2884).isActive = true
// set leading, top, trailing, and bottom constraints to the scroll view
vc.view.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
vc.view.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
vc.view.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
vc.view.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
// finish child VC process
vc.didMove(toParentViewController: self)
// USING constraints / auto-layout, with sizing controlled by constraints set in IB for the "thumbs" view
func setupC() -> Void
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let vc = storyboard.instantiateViewController(withIdentifier: "althumb") as? ALThumbsViewController
// add the child VC
self.addChildViewController(vc)
// add "thumbs" view to the scroll view
self.scrollView.addSubview(vc.view)
// disable auto-resizing mask
vc.view.translatesAutoresizingMaskIntoConstraints = false
// the constraints set in Interface Builder for the elements in the "thumbs" view
// will control its size
// setting the "edge" constraints relative to the scroll view will control the contentSize
// set leading, top, trailing, and bottom constraints to the scroll view
vc.view.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
vc.view.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
vc.view.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
vc.view.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
// finish child VC process
vc.didMove(toParentViewController: self)
我推荐setupC()
中的方法,它使用没有硬编码大小值的自动布局约束。更容易规划不同的设备尺寸(以及未来在其他视图/应用程序/作业/等中的使用)。
【讨论】:
【参考方案2】:当您可以使用 Autolayout 轻松地将 View 嵌入到滚动视图中时,为什么要为 ScrollView 提供静态高度。 你可以参考这个链接, https://www.natashatherobot.com/ios-autolayout-scrollview/
【讨论】:
你指的是直接滚动视图链接而不是主页链接natashatherobot.com/ios-autolayout-scrollview 哎呀,谢谢 :)【参考方案3】:您还必须指定孩子的框架。此外,无需在 viewDidLoad
方法中将您的 UI 更改分派到主队列。并且在将子视图添加为子视图后,您必须调用子视图控制器的 didMove(toParentViewController:)
方法。你可以阅读更多关于容器视图控制器和他们的孩子here。
【讨论】:
以上是关于在 UIScrollView Swift3 中加载 UIViewController的主要内容,如果未能解决你的问题,请参考以下文章
在搜索栏iOS swift3中加载带有条目的单元格时,图像无法正确显示