我需要在contentSize
函数之外设置UIScrollView
的{{1}}。
但是在尝试这样做时,我收到错误loadView
。
'UIView' does not have a member named 'contentSize'
谢谢!
备注:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
var bigRect = view.bounds
bigRect.size.width *= CGFloat(someInt)
view.contentSize = bigRect // 'UIView' does not have a member named 'contentSize'
}
override func loadView() {
view = UIScrollView(frame: UIScreen.mainScreen().bounds)
}
}
确实是view
的一个实例,但发送UIScrollView
会给我同样的错误。contentSize
替换为view
并不能解决问题;这里没有局部变量干扰。答案 0 :(得分:3)
view
是UIViewController
类型的UIView
属性。因此,您应首先在局部变量中配置滚动视图,然后将其设置为视图变量:
override func loadView() {
var scrollView = UIScrollView(frame: UIScreen.mainScreen().bounds)
scrollView.contentSize = <Whatever>
view = scrollView
}
如果您打算稍后更改滚动视图,则应考虑:
view
属性as UIScrollView
(非常糟糕的做法)E.g:
class ViewController: UIViewController {
var scrollView:UIScrollView!
override func viewDidLoad() {
var bigRect = view.bounds
bigRect.size.width *= CGFloat(someInt)
scrollView.contentSize = bigRect.size
}
override func loadView() {
scrollView = UIScrollView(frame: UIScreen.mainScreen().bounds)
view = scrollView
}
}
答案 1 :(得分:2)
尝试将其重命名为scrollView而不是视图,因为视图已经是附加到所有UIViewControllers的变量,因此它可能会变得混乱。当然,除非你试图说你用故事板中的scrollView替换了视图。在这种情况下,您需要将视图转换为scrollView,如下所示:
(view as UIScrollView).contentSize