2015-01-26 123 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