首页 > 解决方案 > UITextField 不响应以编程方式创建的 UI 中的点击

问题描述

我正在以编程方式创建一个 ViewController。我在其中添加了一个滚动视图、一个内容视图和一个带有约束的文本字段。UI 显示正确。但是当我点击文本字段时,什么也没有发生。

override func viewDidLoad() {
    initUI()
}

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

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

    titleText = UITextField(frame: CGRect.zero)
    titleText!.translatesAutoresizingMaskIntoConstraints = false
    titleText!.borderStyle = .roundedRect
    titleText!.isEnabled = true
    titleText!.isUserInteractionEnabled = true
    titleText!.placeholder = Constants.Messages.titlePlaceholder
    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: scrollView!.topAnchor, constant: 20.0),
        titleText!.leadingAnchor.constraint(equalTo: scrollView!.leadingAnchor, constant: 8.0),
        titleText!.trailingAnchor.constraint(equalTo: scrollView!.trailingAnchor, constant: -8.0)
    ])
}

如何使文本字段响应点击?


屏幕


更新:当titleText受 约束时scrollView,我得到:

 Optional<Any>
  - some : <UIView: 0x7f84496c8550; frame = (0 0; 414 896); autoresize = W+H; layer = <CALayer: 0x600003a2cc40>>
   | <UIScrollView: 0x7f8445834400; frame = (0 0; 0 0); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600003320510>; layer = <CALayer: 0x600003a082e0>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}>
   |    | <UIView: 0x7f8449759610; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <CALayer: 0x600003a085e0>>
   |    |    | <UITextField: 0x7f8445863a00; frame = (0 0; 0 0); text = ''; opaque = NO; layer = <CALayer: 0x600003a08520>>
   |    |    |    | <_UITextFieldRoundedRectBackgroundViewNeue: 0x7f844976e740; frame = (0 0; 0 0); opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x600003a087a0>>
   |    |    |    | <_UITextFieldContentView: 0x7f8449777fa0; frame = (0 0; 0 0); opaque = NO; userInteractionEnabled = NO; layer = <__UITextTiledLayer: 0x6000018e8f60>>

titleText受 约束时contentView,我得到:

Optional<Any>
  - some : <UIView: 0x7fadabcc42e0; frame = (0 0; 414 896); autoresize = W+H; layer = <CALayer: 0x6000035f9e40>>
   | <UIScrollView: 0x7fadad123800; frame = (0 0; 0 0); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600003c661f0>; layer = <CALayer: 0x6000035f86c0>; contentOffset: {0, 0}; contentSize: {0, 0}; adjustedContentInset: {0, 0, 0, 0}>
   |    | <UIView: 0x7fadabcfbcf0; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <CALayer: 0x6000035f9ce0>>
   |    |    | <UITextField: 0x7fadad0f6000; frame = (0 0; 0 0); text = ''; opaque = NO; layer = <CALayer: 0x6000035fa320>>
   |    |    |    | <_UITextFieldRoundedRectBackgroundViewNeue: 0x7fadabf07840; frame = (0 0; 0 0); opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x6000035f8a20>>
   |    |    |    | <_UITextFieldContentView: 0x7fadabcdeaf0; frame = (0 0; 0 0); opaque = NO; userInteractionEnabled = NO; layer = <__UITextTiledLayer: 0x6000017c4f60>>

标签: iosswift

解决方案


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

试试这个代码,这对你有用。我已经测试了您的代码,并且在以编程方式执行时不要使用强制展开。

您的代码的问题是您应该在使用时使用底部锚点,UIScrollView并且您将“标题文本字段放入 ScrollView”而不是放入ScrollView您应该锚定在contentView.


推荐阅读