以编程方式在没有插座的swift中创建scrollview

时间:2015-01-26 19:04:40

标签: swift uiscrollview

我已经尝试了但是我不明白代码有什么问题,当我创建一个插座时工作正常,但后来我发现我不需要插座,所以想要以编程方式创建它。

var BestPractices = UIScrollView(frame: CGRectMake(0, 94, 768, 924))
BestPractices.hidden = true

我无法访问" BestPractices"在viewController中,相同的代码在游乐场中正常工作

2 个答案:

答案 0 :(得分:4)

这是this问题的答案。

class ScrollingViewController : UIViewController {
// Create a scrollView property that we'll set as our view in -loadView
let scrollView = UIScrollView(frame: UIScreen.mainScreen().bounds)

override func loadView() {
    // calling self.view later on will return a UIView!, but we can simply call 
    // self.scrollView to adjust properties of the scroll view:
    self.view = self.scrollView

    // setup the scroll view
    self.scrollView.contentSize = CGSize(width:1234, height: 5678)
    // etc...
}

func example() {
    let sampleSubView = UIView()
    self.view.addSubview(sampleSubView) // adds to the scroll view

    // cannot do this:
    // self.view.contentOffset = CGPoint(x: 10, y: 20)
    // so instead we do this:
    self.scrollView.contentOffset = CGPoint(x: 10, y: 20)
}

答案 1 :(得分:0)

添加此行以识别loadview()的最后一个滚动条。

        scrollView.backgroundColor = UIColor.blue