滚动视图无法使用iOS Swift

时间:2017-07-21 12:22:27

标签: ios swift uiscrollview

下面是我尝试将滚动视图设置为视图控制器的代码。有人可以建议我为什么我无法向下滚动视图。

我有很大一部分时间试图解决这个问题。任何帮助将不胜感激。

import UIKit

class ScrollViewTestVC: UIViewController {


// FOR SCREEN DIMENSIONS
let screenSize      : CGRect = UIScreen.main.bounds
var screenWidth     : CGFloat
var screenHeight    : CGFloat

var scrollView = UIScrollView()
var contentView = UIView()

required init?(coder aDecoder: NSCoder) {
    screenHeight    = screenSize.height
    screenWidth     = screenSize.width
    super.init(coder: aDecoder)
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Setting scroll view size
    scrollView = UIScrollView(frame: view.bounds)
    scrollView.isScrollEnabled = true


    let firstName : UILabel = {
        let label       = UILabel()
        label.text      = "First Name"
        return label
    }()

    let lastName : UILabel = {
        let label       = UILabel()
        label.text      = "Last Name"
        return label
    }()

    let age : UILabel = {
        let label       = UILabel()
        label.text      = "Age"
        return label
    }()

    let weight : UILabel = {
        let label       = UILabel()
        label.text      = "Weight"
        return label
    }()

    let gender : UILabel = {
        let label       = UILabel()
        label.text      = "Gender"
        return label
    }()

    // TEXT FIELDS
    let firstNameText : UITextField = {
        let tf      = UITextField()
        tf.placeholder = "Enter first name"
        return tf
    }()

    let lastNameText : UITextField = {
        let tf      = UITextField()
        tf.placeholder = "Enter last name"
        return tf
    }()

    let ageText : UITextField = {
        let tf      = UITextField()
        tf.placeholder  = "Enter Age"
        return tf
    }()

    let weightText : UITextField = {
        let tf      = UITextField()
        tf.placeholder  = "Enter weight"
        return tf
    }()

    let genderText : UITextField = {
        let tf      = UITextField()
        tf.placeholder  = "Enter Gender"
        return tf
    }()

    // ADDING SUB VIEWS
    contentView.addSubview(firstName)
    contentView.addSubview(lastName)
    contentView.addSubview(age)
    contentView.addSubview(weight)
    contentView.addSubview(gender)
    contentView.addSubview(firstNameText)
    contentView.addSubview(lastNameText)
    contentView.addSubview(ageText)
    contentView.addSubview(weightText)
    contentView.addSubview(genderText)

    // Adding the content view to the scroll view
    scrollView.addSubview(contentView)
    //self.view.addSubview(contentView)

    // Pinning the contentView to the scrollView
    pinView(contentView, to: scrollView)


    // Adding scroll view to the main view
    self.view.addSubview(scrollView)

    // Removing default constraints
    firstName.translatesAutoresizingMaskIntoConstraints     = false
    lastName.translatesAutoresizingMaskIntoConstraints      = false
    age.translatesAutoresizingMaskIntoConstraints           = false
    weight.translatesAutoresizingMaskIntoConstraints        = false
    gender.translatesAutoresizingMaskIntoConstraints        = false
    firstNameText.translatesAutoresizingMaskIntoConstraints  = false
    lastNameText.translatesAutoresizingMaskIntoConstraints  = false
    ageText.translatesAutoresizingMaskIntoConstraints       = false
    weightText.translatesAutoresizingMaskIntoConstraints    = false
    genderText.translatesAutoresizingMaskIntoConstraints    = false


    // Views Dict
    var viewsDict = [
        "firstName"     :   firstName,
        "lastName"      :   lastName,
        "age"           :   age,
        "weight"        :   weight,
        "gender"        :   gender,
        "firstNameText" :   firstNameText,
        "lastNameText"  :   lastNameText,
        "ageText"       :   ageText,
        "weightText"    :   weightText,
        "genderText"    :   genderText

    ]
    // Do any additional setup after loading the view.

    // SETTING CONSTRAINTS

    var space = 100
    var edgeSpace = 10
    // First Name
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-10-[firstName]", options: [], metrics: nil, views: viewsDict))
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-\(edgeSpace)-[firstName]", options: [], metrics: nil, views: viewsDict))

    // First Name Text
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[firstName]-5-[firstNameText]", options: [], metrics: nil, views: viewsDict))
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-\(edgeSpace)-[firstNameText]|", options: [], metrics: nil, views: viewsDict))

    // Last Name
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[firstNameText]-\(space)-[lastName]", options: [], metrics: nil, views: viewsDict))
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-\(edgeSpace)-[lastName]", options: [], metrics: nil, views: viewsDict))

    // Last Name Text
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[lastName]-5-[lastNameText]", options: [], metrics: nil, views: viewsDict))
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-\(edgeSpace)-[lastNameText]|", options: [], metrics: nil, views: viewsDict))

    // Age
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[lastNameText]-\(space)-[age]", options: [], metrics: nil, views: viewsDict))
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-\(edgeSpace)-[age]", options: [], metrics: nil, views: viewsDict))

    // Age Text
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[age]-5-[ageText]", options: [], metrics: nil, views: viewsDict))
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-\(edgeSpace)-[ageText]|", options: [], metrics: nil, views: viewsDict))

    // Weight
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[ageText]-\(space)-[weight]", options: [], metrics: nil, views: viewsDict))
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-\(edgeSpace)-[weight]", options: [], metrics: nil, views: viewsDict))

    // Weight Text
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[weight]-5-[weightText]", options: [], metrics: nil, views: viewsDict))
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-\(edgeSpace)-[weightText]|", options: [], metrics: nil, views: viewsDict))

    // Gender
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[weightText]-\(space)-[gender]", options: [], metrics: nil, views: viewsDict))
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-\(edgeSpace)-[gender]", options: [], metrics: nil, views: viewsDict))

    // Gender Text
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[gender]-5-[genderText]", options: [], metrics: nil, views: viewsDict))
    self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-\(edgeSpace)-[genderText]|", options: [], metrics: nil, views: viewsDict))


}

override func viewDidAppear(_ animated: Bool) {

    super.viewDidAppear(animated)

 }

    // The below function is used to pin one view to another view
    public func pinView(_ view: UIView, to scrollView: UIScrollView) {
        view.translatesAutoresizingMaskIntoConstraints = false
        view.pin(to: scrollView)
    }

    public func pin(to view: UIView) {
        NSLayoutConstraint.activate([
            leadingAnchor.constraint(equalTo: view.leadingAnchor),
            trailingAnchor.constraint(equalTo: view.trailingAnchor),
            topAnchor.constraint(equalTo: view.topAnchor),
            bottomAnchor.constraint(equalTo: view.bottomAnchor)
            ])
    }

}

2 个答案:

答案 0 :(得分:3)

您最底层的项目需要连接到超级视图,以便滚动视图可以计算其高度。添加“|”:

self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[gender]-5-[genderText]|", options: [], metrics: nil, views: viewsDict))

答案 1 :(得分:-1)

您必须设置scrollview的内容大小。 见documentation