2015-04-21 40 views
1

我正在使用Interface Builder/storyboards來處理Xcode 6和iOS自動佈局/約束而不使用,但我目前正在使用滾動視圖掙扎。沒有界面生成器的UIScrollView和Autolayout - 滾動視圖如何獲得其高度?

我已經寫了下面的代碼(我學習的時候都是longhand!),它在包含內容視圖的屏幕上放置滾動視圖,而該視圖又包含一些附加視圖。

滾動時,滾動事件觸發正常。我遇到的問題是,當我放開時,滾動視圖只是回到起點,這表明它沒有收到內容高度。我認爲。或者,也許我在某處弄了限制?

任何人都可以給我一些指示,我可能會在哪裏出錯嗎?

謝謝。

class TestScrollController: UIViewController, UIScrollViewDelegate { 


    var scrollView: UIScrollView! 
    var contentView = UIView() 
    var blueView = UIView() 
    var orangeView = UIView() 



    override func viewDidLoad() { 
     super.viewDidLoad() 

     var superView = self.view 

     var scrollView = UIScrollView() 
     scrollView.setTranslatesAutoresizingMaskIntoConstraints(false) 
     scrollView.scrollEnabled = true 
     scrollView.alwaysBounceVertical = true 
     scrollView.backgroundColor = UIColor.brownColor() 
     scrollView.delegate = self 
     superView.addSubview(scrollView); 

     var contentView = UIView() 
     contentView.setTranslatesAutoresizingMaskIntoConstraints(false) 
     contentView.backgroundColor = UIColor.greenColor() 
     scrollView.addSubview(contentView) 

     let blueView = UIView() 
     blueView.setTranslatesAutoresizingMaskIntoConstraints(false) 
     blueView.backgroundColor = UIColor.blueColor() 
     contentView.addSubview(blueView); 

     let orangeView = UIView() 
     orangeView.setTranslatesAutoresizingMaskIntoConstraints(false) 
     orangeView.backgroundColor = UIColor.orangeColor() 
     contentView.addSubview(orangeView); 




     //scrollview Constraints 
     let scrollViewConstraintTop = NSLayoutConstraint(
      item: scrollView, 
      attribute: .Top, 
      relatedBy: .Equal, 
      toItem: superView, 
      attribute: .Top, 
      multiplier: 1.0, 
      constant: 0 
     ) 
     superView.addConstraint(scrollViewConstraintTop) 

     let scrollViewConstraintRight = NSLayoutConstraint(
      item: scrollView, 
      attribute: .Trailing, 
      relatedBy: .Equal, 
      toItem: superView, 
      attribute: .Right, 
      multiplier: 1.0, 
      constant: 0 
     ) 
     superView.addConstraint(scrollViewConstraintRight) 

     let scrollViewConstraintBottom = NSLayoutConstraint(
      item: scrollView, 
      attribute: .Bottom, 
      relatedBy: .Equal, 
      toItem: superView, 
      attribute: .Bottom, 
      multiplier: 1.0, 
      constant: 0 
     ) 
     superView.addConstraint(scrollViewConstraintBottom) 

     let scrollViewConstraintLeft = NSLayoutConstraint(
      item: scrollView, 
      attribute: .Leading, 
      relatedBy: .Equal, 
      toItem: superView, 
      attribute: .Left, 
      multiplier: 1.0, 
      constant: 0 
     ) 
     superView.addConstraint(scrollViewConstraintLeft) 



     //contentView Constraints 
     let contentViewConstraintTop = NSLayoutConstraint(
      item: contentView, 
      attribute: .Top, 
      relatedBy: .Equal, 
      toItem: scrollView, 
      attribute: .Top, 
      multiplier: 1.0, 
      constant: 0 
     ) 
     superView.addConstraint(contentViewConstraintTop) 

     let contentViewConstraintRight = NSLayoutConstraint(
      item: contentView, 
      attribute: .Trailing, 
      relatedBy: .Equal, 
      toItem: scrollView, 
      attribute: .Right, 
      multiplier: 1.0, 
      constant: 0 
     ) 
     superView.addConstraint(contentViewConstraintRight) 

     let contentViewConstraintBottom = NSLayoutConstraint(
      item: contentView, 
      attribute: .Bottom, 
      relatedBy: .Equal, 
      toItem: scrollView, 
      attribute: .Bottom, 
      multiplier: 1.0, 
      constant: 0 
     ) 
     superView.addConstraint(contentViewConstraintBottom) 

     let contentViewConstraintLeft = NSLayoutConstraint(
      item: contentView, 
      attribute: .Leading, 
      relatedBy: .Equal, 
      toItem: scrollView, 
      attribute: .Left, 
      multiplier: 1.0, 
      constant: 0 
     ) 
     superView.addConstraint(contentViewConstraintLeft) 



     //blueView Constraints 
     let blueViewConstraintHeight = NSLayoutConstraint(
      item: blueView, 
      attribute: .Height, 
      relatedBy: .Equal, 
      toItem: nil, 
      attribute: .NotAnAttribute, 
      multiplier: 1.0, 
      constant: 100.0 
     ) 
     superView.addConstraint(blueViewConstraintHeight) 

     let blueViewConstraintTop = NSLayoutConstraint(
      item: blueView, 
      attribute: .Top, 
      relatedBy: .Equal, 
      toItem: contentView, 
      attribute: .Top, 
      multiplier: 1.0, 
      constant: 50.0 
     ) 
     superView.addConstraint(blueViewConstraintTop) 

     let blueViewConstraintLeft = NSLayoutConstraint(
      item: blueView, 
      attribute: .Leading, 
      relatedBy: .Equal, 
      toItem: superView, 
      attribute: .Left, 
      multiplier: 1.0, 
      constant: 50.0 
     ) 
     superView.addConstraint(blueViewConstraintLeft) 

     let blueViewConstraintRight = NSLayoutConstraint(
      item: blueView, 
      attribute: .Trailing, 
      relatedBy: .Equal, 
      toItem: superView, 
      attribute: .Right, 
      multiplier: 1.0, 
      constant: -50.0 
     ) 
     superView.addConstraint(blueViewConstraintRight) 



     //orangeView Constraints 
     let orangeViewConstraintWidth = NSLayoutConstraint(
      item: orangeView, 
      attribute: .Width, 
      relatedBy: .Equal, 
      toItem: nil, 
      attribute: .NotAnAttribute, 
      multiplier: 1.0, 
      constant: 100.0 
     ) 
     superView.addConstraint(orangeViewConstraintWidth) 

     let orangeViewConstraintHeight = NSLayoutConstraint(
      item: orangeView, 
      attribute: .Height, 
      relatedBy: .Equal, 
      toItem: nil, 
      attribute: .NotAnAttribute, 
      multiplier: 1.0, 
      constant: 100.0 
     ) 
     superView.addConstraint(orangeViewConstraintHeight) 


     let orangeViewConstraintTop = NSLayoutConstraint(
      item: orangeView, 
      attribute: .Top, 
      relatedBy: .Equal, 
      toItem: blueView, 
      attribute: .Bottom, 
      multiplier: 1.0, 
      constant: 400.0 
     ) 
     superView.addConstraint(orangeViewConstraintTop) 

     let orangeViewConstraintBottom = NSLayoutConstraint(
      item: orangeView, 
      attribute: .Bottom, 
      relatedBy: .Equal, 
      toItem: contentView, 
      attribute: .Bottom, 
      multiplier: 1.0, 
      constant: 400.0 
     ) 
     superView.addConstraint(orangeViewConstraintBottom) 


     let orangeViewConstraintCenterX = NSLayoutConstraint(
      item: orangeView, 
      attribute: .CenterX, 
      relatedBy: .Equal, 
      toItem: blueView, 
      attribute: .CenterX, 
      multiplier: 1.0, 
      constant: 0.0 
     ) 
     superView.addConstraint(orangeViewConstraintCenterX) 


    } 




    func scrollViewDidScroll(scrollView: UIScrollView) { 
     println("scrollViewDidScroll") 
    } 




} 
+0

,你可以在這裏找到一個很好的解釋(在obj-c):http://stackoverflow.com/a/24972208/2477632 –

+0

這幫了很多,謝謝! – user1418711

回答

0

您可以使用.contentsize(CGRECT)設置內容的尺寸內UIScrollView
既然你沒有設置一個contentSize你不能對任何事情滾動

相關問題