ViewControlling充满textEdits不会使用scrollview向下滚动

时间:2019-04-11 21:18:02

标签: swift scrollview

我有一个带有8到9个textEdits的视图控制器,用户必须填写它们才能保存到数据库中,但是这占用了我的很多屏幕,并且由于iphones屏幕的大小,没有显示某些TE。然后,我决定添加这样的UIScrollView:

lazy var myScrollView : UIScrollView = {
    let scrol = UIScrollView()

    scrol.contentSize.height = 10000
    scrol.backgroundColor = appBackgroundColor
    scrol.translatesAutoresizingMaskIntoConstraints = false

    return scrol
}()

...

view.addSubview(myScrollView)
myScrollView.addSubview(labelYear)
myScrollView.addSubview(labelAppTitle)

// then I added the constraints 
NSLayoutConstraint.activate([
    myScrollView.topAnchor.constraint(equalTo: view.topAnchor),
    myScrollView.leftAnchor.constraint(equalTo: view.leftAnchor),
    myScrollView.rightAnchor.constraint(equalTo: view.rightAnchor),
    //enter code here
    myScrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
    labelAppTitle.leftAnchor.constraint(equalTo: myScrollView.leftAnchor,constant: 40),
    labelAppTitle.topAnchor.constraint(equalTo: myScrollView.safeAreaLayoutGuide.topAnchor, constant: 10),
    labelAppTitle.rightAnchor.constraint(equalTo:myScrollView.rightAnchor, constant: -40),
    labelAppTitle.heightAnchor.constraint(equalToConstant: 90)
])

我有更多的textEdits,但是为了节省空间而没有发布。问题是它没有像我想要的那样向下滚动。我该怎么做?

谢谢

1 个答案:

答案 0 :(得分:0)

import UIKit

class TestController: UIViewController, UITextFieldDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        initUI()
    }

    func initUI() {
        let scrollView = UIScrollView()
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.isUserInteractionEnabled = true
        view.addSubview(scrollView)

        let contentView = UIView()
        contentView.translatesAutoresizingMaskIntoConstraints = false
        contentView.isUserInteractionEnabled = true
        contentView.isMultipleTouchEnabled = true
        scrollView.addSubview(contentView)

        let titleText = UITextField(frame: CGRect.zero)
        titleText.translatesAutoresizingMaskIntoConstraints = false
        titleText.borderStyle = .roundedRect
        titleText.isEnabled = true
        titleText.isUserInteractionEnabled = true
        titleText.placeholder = "Constants.Messages.titlePlaceholder"
        titleText.isUserInteractionEnabled = true
        titleText.delegate = self
        contentView.addSubview(titleText)

        // scroll view
        NSLayoutConstraint.activate([
            scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100),
            scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8.0),
            scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8.0),
            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -8.0)
            ])
        // content view
        NSLayoutConstraint.activate([
            contentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
            contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
            contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
            contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
            contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor)
            ])
        // title text field
        NSLayoutConstraint.activate([
            titleText.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 20.0),
            titleText.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8.0),
            titleText.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8.0),
            titleText.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0)
            ])
    }
}

这是使用scrollView的示例。创建滚动视图时,苹果建议将contentView放入其中,并将其放入scrollView中,不要忘记使用bottomAnchor。如果您忘记使用它,它将不会滚动。