【问题标题】:ViewControlling full of textEdits not scrolling down with scrollviewViewControlling full of textEdits 不使用滚动视图向下滚动
【发布时间】:2019-04-11 21:18:01
【问题描述】:

我有一个带有 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)
])

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

谢谢

【问题讨论】:

  • 查看之前的答案。 Programmatic UIScrollview with Autolayout。它将帮助解释如何将您的 UITextFields 添加到 scrollView 并确保它滚动。
  • 此外,由于您有这么多文本字段,您可能需要考虑使用UITableViewController,而不是为每个文本字段添加一个单元格,它可以免费为您提供键盘处理。

标签: swift scrollview


【解决方案1】:
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的例子。当你创建一个scrollView的时候,苹果建议在里面放一个contentView,放在scrollView里面,不要忘记使用bottomAnchor。如果你忘记使用它,它就不会滚动。

【讨论】:

    猜你喜欢
    • 2020-10-05
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 2016-07-02
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多