Ios 如何以编程方式将多个UIViewController添加到UIScrollView
通过这里的好人的帮助,我学会了如何将Ios 如何以编程方式将多个UIViewController添加到UIScrollView,ios,iphone,swift,uiviewcontroller,uiscrollview,Ios,Iphone,Swift,Uiviewcontroller,Uiscrollview,通过这里的好人的帮助,我学会了如何将UIViewController添加到UIScrollView——正确的方法。问题是,我需要添加多个视图控制器。我尝试创建一个UIViewController数组,然后使用for…in循环将这些控制器添加为子视图控制器,然后将它们的视图推送到滚动视图。那不行 现在,我在滚动视图中只看到一个ViewController,第一个…有人能告诉我我做错了什么吗 请参阅我的viewDidLoad()中的相关代码以及我为创建相关对象而设置的属性: @IBOutle
UIViewController
添加到UIScrollView
——正确的方法。问题是,我需要添加多个视图控制器。我尝试创建一个UIViewController数组,然后使用for…in
循环将这些控制器添加为子视图控制器,然后将它们的视图推送到滚动视图。那不行
现在,我在滚动视图中只看到一个ViewController,第一个…有人能告诉我我做错了什么吗
请参阅我的viewDidLoad()
中的相关代码以及我为创建相关对象而设置的属性:
@IBOutlet var contentScrollView: UIScrollView!
var frame = CGRectMake(0, 0, 0, 0)
var dataArray = [PostViewController]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var da = PostItem(text: "test 1", pic: "beans.jpg")
var vc = PostViewController(nibName:"PostViewController", bundle:nil)
vc.text = da!.postText
vc.image = da!.postImage
self.dataArray.append(vc)
da = PostItem(text: "test 2", pic: "coffeeCup.jpg")
vc = PostViewController(nibName:"PostViewController", bundle:nil)
vc.text = da!.postText
vc.image = da!.postImage
self.dataArray.append(vc)
da = PostItem(text: "test 2", pic: "coffeeMill.jpg")
vc = PostViewController(nibName:"PostViewController", bundle:nil)
vc.text = da!.postText
vc.image = da!.postImage
self.dataArray.append(vc)
//...
for index in 0..<self.dataArray.count{
self.frame.origin.y = self.contentScrollView.frame.size.height * CGFloat(index)
self.frame.size = self.contentScrollView.frame.size
self.contentScrollView.pagingEnabled = false
let vc = self.dataArray[index]
self.addChildViewController(vc)
self.contentScrollView.addSubview(vc.view)
}
self.contentScrollView.contentSize = CGSizeMake(self.view.frame.size.width, CGFloat(self.dataArray.count) * self.contentScrollView.frame.height)
}
@IBOutlet var contentScrollView:UIScrollView!
var frame=CGRectMake(0,0,0,0)
var dataArray=[PostViewController]()
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从nib执行任何其他设置。
var da=positem(文本:“测试1”,图片:“beans.jpg”)
var vc=PostViewController(nibName:“PostViewController”,bundle:nil)
vc.text=da!.postText
vc.image=da!.postImage
self.dataArray.append(vc)
da=positem(文本:“测试2”,图片:“coffeeCup.jpg”)
vc=PostViewController(nibName:“PostViewController”,捆绑包:nil)
vc.text=da!.postText
vc.image=da!.postImage
self.dataArray.append(vc)
da=positem(文本:“测试2”,图片:“coffeeMill.jpg”)
vc=PostViewController(nibName:“PostViewController”,捆绑包:nil)
vc.text=da!.postText
vc.image=da!.postImage
self.dataArray.append(vc)
//...
对于0中的索引,您需要将新的vc.view.frame.origin.y设置为awell.ATM。您正在将每个vc添加到相同的位置,这使得看起来只有一个vc被添加。您不认为在viewDidLoad中设置位置太早了吗(尝试在ViewWillLayoutSubView中设置它们)?此外,您是否尝试调试UIScrollView层次结构(XCode6对此有一个视图检查器)?查看视图的添加是否正确或位置是否错误会很有帮助。这是否会编译?如果self是UIViewController,则self.frame将无法工作。@仲裁人:是的,代码编译良好。正如我所说,它确实显示了几个视图控制器中的一个。@Tiago Maia,我没有尝试视图检查器。我会尝试,不是吗o看看我是否能获得一些见解。