我想将“ViewController2”设置为ScrollView中的初始页面,但是更改setContentOffset(CGPointMake(0,0), animated: false)
会将第一页设置为初始页面,我尝试使用不同的数字修改它,但它只是弄乱了页面,任何提示?
import UIKit
class ViewController: UIViewController {
@IBOutlet var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.bounces = false
scrollView.pagingEnabled = true
scrollView.setContentOffset(CGPointMake(0,0), animated: false)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewDidLayoutSubviews() {
initScrollView()
}
func initScrollView(){
let viewController1 = storyboard?.instantiateViewControllerWithIdentifier("ViewController1") as! ViewController1
viewController1.willMoveToParentViewController(self)
viewController1.view.frame = scrollView.bounds
let viewController2 = storyboard?.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2
viewController2.willMoveToParentViewController(self)
viewController2.view.frame.size = scrollView.frame.size
viewController2.view.frame.origin = CGPoint(x: view.frame.width, y: 0)
let viewController3 = storyboard?.instantiateViewControllerWithIdentifier("ViewController3") as! ViewController3
viewController3.willMoveToParentViewController(self)
viewController3.view.frame.size = scrollView.frame.size
viewController3.view.frame.origin = CGPoint(x: view.frame.width * 2, y: 0)
scrollView.contentSize = CGSize(width: 3 * scrollView.frame.width, height: scrollView.frame.height)
scrollView.addSubview(viewController3.view)
self.addChildViewController(viewController3)
viewController3.didMoveToParentViewController(self)
scrollView.addSubview(viewController2.view)
self.addChildViewController(viewController2)
viewController2.didMoveToParentViewController(self)
scrollView.addSubview(viewController1.view)
self.addChildViewController(viewController1)
viewController1.didMoveToParentViewController(self)
}}
答案 0 :(得分:0)
答案 1 :(得分:0)
通过将scrollView.setContentOffset(CGPointMake(375,0), animated: false)
放入viewDidlayoutSubViews中,它会加载下一个视图,请注意我使用的是iphone 4,7英寸屏幕,因此viewController的宽度为375.将375更改为viewController的宽度,例如你想首先加载最后一页你要添加375到375等等..
override func viewDidLayoutSubviews() {
initScrollView()
scrollView.setContentOffset(CGPointMake(375,0), animated: false)
}