gpt4 book ai didi

ios - 将 UIViewController 添加到 UIScrollView swift 3

转载 作者:搜寻专家 更新时间:2023-10-31 08:04:11 24 4
gpt4 key购买 nike

我正在处理 ScrollView。目前我的 ScrollView 中有两个图像,我想在 ScrollView 中至少添加一个新的 UIViewController

这是我的代码:

    self.scrollView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.scrollView.frame.height)
let scrollviewHeight = self.scrollView.frame.height
let scrollviewWidth = self.scrollView.frame.width

var imgOne = UIImageView(frame: CGRect(x: 0, y: 0, width: scrollviewWidth, height: scrollviewHeight))
var imgTwo = UIImageView(frame: CGRect(x: scrollviewWidth, y: 0, width: scrollviewWidth, height: scrollviewHeight))

imgOne.image = UIImage(named: "preview1")
imgTwo.image = UIImage(named: "preview2")

self.scrollView.addSubview(imgOne)
self.scrollView.addSubview(imgTwo)

self.scrollView.contentSize = CGSize(width: self.scrollView.frame.width * 2, height: self.scrollView.frame.height)
self.scrollView.isPagingEnabled = true

设计:

The first one is the root ViewController, where the ScrollView is and the second one I want to add in the ScrollView

最佳答案

试试看:

self.scrollView.frame = CGRect( <set frame> )

var imgOne = UIImageView(frame: CGRect( <set frame> ))
var imgTwo = UIImageView(frame: CGRect( <set frame> ))
var vcView = UIView(frame: CGRect( <set frame> ))
addChildVC(vcView: vcView)

imgOne.image = UIImage(named: "preview1")
imgTwo.image = UIImage(named: "preview2")

self.scrollView.addSubview(imgOne)
self.scrollView.addSubview(imgTwo)
self.scrollView.addSubview(vcView)



self.scrollView.contentSize = CGSize( <set content size> )
self.scrollView.isPagingEnabled = true

添加 subview Controller

func addChildVC(vcView: UIView){

let testVC = self.storyboard?.instantiateViewControllerWithIdentifier("testIdentifier") as! TestViewController
testVC.view.frame = vcView.bounds
vcView.addSubview(testVC.view)
self.addChildViewController(testVC)
testVC.didMoveToParentViewController(self)
}

关于ios - 将 UIViewController 添加到 UIScrollView swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44634119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com