簡體   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