我正在尝试使用本教程使用视图控制器而不是xib文件进行滑动导航:https://www.youtube.com/watch?v=3jAlg5BnYUU
我设法用视图控制器替换了xib文件,但是当我运行应用程序时,视图控制器的宽度小于正常值:
我不明白,因为它的设置与xib文件相同。有人知道如何解决吗?
这是我的代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
let vc0 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController0")
self.addChildViewController(vc0!)
self.scrollView.addSubview(vc0!.view)
vc0!.didMoveToParentViewController(self)
let vc1 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController1")
var frame1 = vc1!.view.frame
frame1.origin.x = self.view.frame.size.width
vc1!.view.frame = frame1
self.addChildViewController(vc1!)
self.scrollView.addSubview(vc1!.view)
vc1!.didMoveToParentViewController(self)
let vc2 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController2")
var frame2 = vc2!.view.frame
frame2.origin.x = self.view.frame.size.width * 2
vc2!.view.frame = frame2
self.addChildViewController(vc2!)
self.scrollView.addSubview(vc2!.view)
vc2!.didMoveToParentViewController(self)
let vc3 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController3")
var frame3 = vc3!.view.frame
frame3.origin.x = self.view.frame.size.width * 3
vc3!.view.frame = frame3
self.addChildViewController(vc3!)
self.scrollView.addSubview(vc3!.view)
vc3!.didMoveToParentViewController(self)
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 4, self.view.frame.size.height - 66)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
最佳答案
请注意,手动将viewDidLoad用于设置框架会有些冒险,应避免使用,因为未设置边界。而是使用viewDidLayoutSubviews来执行此操作,但请看一下,因为在UI元素(包括约束)发生更改时,始终会调用此方法。
var scrollViewAdded = false
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if !scrollViewAdded {
self.loadSrollView()
self.scrollViewAdded = true
}
}
func loadSrollView() {
self.scrollView.pagingEnabled = true
let vc0 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController0")
self.addChildViewController(vc0!)
self.scrollView.addSubview(vc0!.view)
vc0!.didMoveToParentViewController(self)
let vc1 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController1")
var frame1 = vc1!.view.frame
frame1.origin.x = self.view.frame.size.width
vc1!.view.frame = frame1
self.addChildViewController(vc1!)
self.scrollView.addSubview(vc1!.view)
vc1!.didMoveToParentViewController(self)
let vc2 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController2")
var frame2 = vc2!.view.frame
frame2.origin.x = self.view.frame.size.width * 2
vc2!.view.frame = frame2
self.addChildViewController(vc2!)
self.scrollView.addSubview(vc2!.view)
vc2!.didMoveToParentViewController(self)
let vc3 = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController3")
var frame3 = vc3!.view.frame
frame3.origin.x = self.view.frame.size.width * 3
vc3!.view.frame = frame3
self.addChildViewController(vc3!)
self.scrollView.addSubview(vc3!.view)
vc3!.didMoveToParentViewController(self)
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 4, self.view.frame.size.height - 66)
}
关于ios - 快速滑动导航调整 View Controller 大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38640519/