滚动视图不起作用iOS Swift

Posted

技术标签:

【中文标题】滚动视图不起作用iOS Swift【英文标题】:Scroll View not working iOS Swift 【发布时间】:2017-07-21 12:22:27 【问题描述】:

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

我一天中的大部分时间都在尝试解决这个问题。任何帮助将不胜感激。

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)
            ])
    


【问题讨论】:

【参考方案1】:

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

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

【讨论】:

很好发现,它有效。如果您也可以看看这个问题,那就太好了。 ***.com/q/45229375/2652541 太棒了!很高兴我能帮上忙。【参考方案2】:

您必须设置滚动视图的内容大小。 见documentation

【讨论】:

在应用 Constraints 时不需要,这不是答案,而是建议,所以请在评论中提出。 对不起,我还是没办法评论。

以上是关于滚动视图不起作用iOS Swift的主要内容,如果未能解决你的问题,请参考以下文章

通过点击状态栏滚动到顶部 TableView 不起作用(有许多子视图)SWIFT

Swift - UIScrollView 滚动动画在 viewDidLoad() 中不起作用

iOS 滚动视图不起作用

Swift编程约束下边距不起作用

缩放在滚动视图中不起作用

ios 8 - 水平滚动视图中的按钮拦截平移事件 - 滚动不起作用