繁体   English   中英

在UIScrollView Swift3内部加载UIViewController

[英]Loading UIViewController inside an UIScrollView Swift3

我有2个UIViewController 一个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 ,但是大小错误并且无法滚动。

任何帮助表示赞赏

在运行时加载视图时,调整视图的大小会遇到问题。 情侣选择...

// 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()使用该方法,该方法使用自动布局约束,并且没有硬编码的大小值。 使计划不同的设备尺寸(以及将来在其他视图/应用/作业/等中使用)的规划变得更加容易。

当您可以使用Autolayout轻松将View嵌入滚动视图时,为什么要给ScrollView赋予静态高度。 您可以参考以下链接, https://www.natashatherobot.com/ios-autolayout-scrollview/

您还必须指定孩子的镜框。 同样,也无需在viewDidLoad方法中将UI更改分派到主队列。 并且,在将其视图添加为子视图之后,必须调用子视图控制器的didMove(toParentViewController:)方法。 您可以在此处阅读有关容器视图控制器及其子级的更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM