ios - UITextField 不响应以编程方式创建的 UI 中的点击

标签 ios swift

我正在以编程方式创建一个 ViewController。我在其中添加了一个 ScrollView 、一个内容 View 和一个带有约束的文本字段。 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)
    ])
}

如何让文本框响应点击?


screen


更新: 当 titleTextscrollView 约束时,我得到:

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

titleTextcontentView 约束时,我得到:

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

最佳答案

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 时使用底部 anchor ,并且您将“Title Text Field in ScrollView”而不是放在 ScrollView 中应该锚定在 contentView 中。

关于ios - UITextField 不响应以编程方式创建的 UI 中的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55588430/

相关文章:

ios - 将 UIView 放在某些 View Controller 的状态栏顶部

ios - 使用 SWIFT 在 Tableview 中设置自定义单元格

ios - TableView 动态行高未被调用

objective-c - 将自定义字体设置为粗体

ios - Mac 上 SwiftUI 中的 UIDocumentPickerViewController (macCatalyst)

ios - Swift:当应用程序在后台时调用 .requestLocation()

swift - EKEvent的镜像不显示数据

ios - 在核心蓝牙框架中创建可写特性的问题

swift - 如何在 swift 4 中获取子字符串?

function - Swift、数学函数和协议(protocol)