2015-01-26 126 views
0

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

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

我不能访问的viewController“BestPractices”的性质,而相同的代码工作在操场

回答

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) 
} 
+0

很好的回答!但我有一个问题它返回我的黑色背景,我无法改变它。 – 2016-11-11 17:18:48

0

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

 scrollView.backgroundColor = UIColor.blue