ios - 带有 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 中而不做任何特别的事情。这甚至是一个好方法吗?我应该在哪里设置按钮操作?我的意思是一旦它可以点击......谢谢!
解决方案
先试试这个 - 在 末尾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),
])
推荐阅读
- ramda.js - 如何使用 Ramda 执行数组中的表达式字符串
- c++ - 是什么导致 WSHStringToAddress() 与 Widows Qt 发生崩溃?
- javascript - 如何在 Redux Store 中按 id 存储项目
- timeout - 本机 Apple Pay 超时
- java - 是否“在警告级别使用 slf4j 记录”,阻止运行“logger.debug(...”
- python - 如何创建多个具有不同名称的文件并在 Python 中写入它们
- python - 计算二进制数末尾尾随 0 数量的有效代码
- python - 检查字符串是否包含所有元音的函数
- sql - PostgreSQL - 在终端中查询 - 没有输出?
- jquery - Hide option when selected option has value