视图控制满满文本不使用滚动视图向下滚动的编辑



我有一个视图控制器,有 8 到 9 个文本编辑,用户必须填写它们才能保存到数据库中,但它占用了我的很多屏幕,并且由于 iphone 屏幕的大小,一些 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)
])

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

谢谢

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。如果您忘记使用它,那么它将无法滚动。

最新更新