首页 > 解决方案 > 带有 textField 和按钮的自定义视图没有响应

问题描述

我有一些标签、一个文本字段和一个按钮的自定义视图。当我将它添加到 VC 中的视图时,自定义视图看起来很好,但我无法点击文本字段或按钮,它们没有响应。有人可以告诉我代码有什么问题吗?这是简化版:

import UIKit

class CustomView: UIView {
    
    let title: UILabel = {
        let title = UILabel()
        title.font = UIFont.systemFont(ofSize: 24)
        title.text = "Title"
        title.numberOfLines = 0
        title.textAlignment = .center
        title.translatesAutoresizingMaskIntoConstraints = false
        return title
    }()
    
    let textView: UITextField = {
        let textView = UITextField()
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.placeholder = "Placeholder text"
        textView.backgroundColor = UIColor.lightGray
        textView.layer.cornerRadius = 10
        return textView
    }()
    
    
    let searchButton: UIButton = {
        let button = UIButton()
        button.translatesAutoresizingMaskIntoConstraints = false
        button.backgroundColor = UIColor.blue
        button.setTitle("Tap me", for: UIControl.State.normal)
        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        return button
    }()
    
    private func setupView() {
        
        addSubview(title)
        addSubview(textView)
        addSubview(searchButton)
        
        NSLayoutConstraint.activate([
            title.topAnchor.constraint(equalTo: self.topAnchor, constant: 100),
            title.rightAnchor.constraint(equalTo: self.rightAnchor),
            title.leftAnchor.constraint(equalTo: self.leftAnchor),
        ])
        
        NSLayoutConstraint.activate([
            
            textView.topAnchor.constraint(equalTo: title.bottomAnchor, constant: 20),
            textView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20),
            textView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20),
            textView.heightAnchor.constraint(equalToConstant: 60)
        ])
        
        NSLayoutConstraint.activate([
            
            searchButton.topAnchor.constraint(equalTo: textView.bottomAnchor, constant: 20),
            searchButton.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20),
            searchButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20),
            searchButton.heightAnchor.constraint(equalToConstant: 60)
        ])
        
    }
    
    @objc func buttonAction(_ sender:UIButton!)
    {
        print("Button tapped")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupView()
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

import UIKit

class ViewController: UIViewController {
    
    private let customView = CustomView()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(customView)
        
        customView.translatesAutoresizingMaskIntoConstraints = false
        customView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        customView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        customView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    }
}

基本上我想要一个单独的文件,其中包含所有设计的视图,我只想将它放入 VC 中而不做任何特别的事情。这甚至是一个好方法吗?我应该在哪里设置按钮操作?我的意思是一旦它可以点击......谢谢!

标签: iosswiftuikit

解决方案


先试试这个 - 在 末尾viewDidLoad(),添加这一行:

customView.backgroundColor = .red

运行应用程序时,您会注意到没有红色框。

现在,在该行之后添加这一行:

customView.clipsToBounds = true

再次运行它,然后......我们什么也看不到

问题是,你没有给你customView任何身高。

searchButton要修复它,请在自定义视图类中约束底部:

    NSLayoutConstraint.activate([
        
        searchButton.topAnchor.constraint(equalTo: textView.bottomAnchor, constant: 20),
        searchButton.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20),
        searchButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20),
        searchButton.heightAnchor.constraint(equalToConstant: 60),
        
        // add this line!
        searchButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -20),

    ])

推荐阅读