首先让我说,我玩编程,但我目前是一个新手。
我混合了编程视图和脚本对象:
情节提要对象:
按钮
文本字段
文本框
程序视图:
按钮文字
viewForMessageLabel视图
当我按下按钮时,就会添加viewForMessageLabel
。在viewDidLoad
中,我添加了一个点击手势,以便在点击背景时删除viewForMessageLabel
。我还将相同的轻触手势添加到文本字段中,以删除viewForMessageLabel
(如果存在)。我再次将相同的轻敲手势添加到文本字段中以将其删除。
如果键盘存在,我将另一个轻触手势添加到文本字段的inviewDidLoad
中,也将其取消。我注意到事情很古怪,我失去了联系。
如果我在触摸背景时按按钮添加标签,它不会被取消。如果我按文本字段,它将关闭它并显示键盘。当textField仍然处于打开状态时,如果我再次按下按钮,标签就会出现,我再次按下textField,什么也不会发生。当我按下return隐藏键盘(我实现了这个方法)时,键盘消失,按下按钮,viewForMessageLabel
就会出现,现在当我按下文本字段时,viewForMessageLabel
就会消失。基本上,textField也发生了同样的事情。
我想要的是
如果存在viewForMessageLabel
,并且我按了background、textField或textView,它应该会消失。
如果textField或textView的键盘存在,并且我按下背景,键盘也应该消失。
我的代码:
class ViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {
//MARK:- Outlets
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var textView: UITextView!
@IBOutlet weak var button: UIButton!
let messagelabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Pizza Pizza Pizza Pizza Pizza"
label.font = UIFont(name: "Helvetica-Regular", size: 17)
label.sizeToFit()
label.numberOfLines = 0
label.textAlignment = .center
label.textColor = UIColor.white
label.backgroundColor = UIColor.clear
return label
}()
let viewForMessageLabel: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor.red
return view
}()
//View Controller Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
textView.delegate = self
// 0. hide viewForMessageLabel is background is tapped
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(removeViewForMessageLabel))
view.addGestureRecognizer(tapGesture)
// 1. hide viewForMessageLabel if textView is tapped
textView.addGestureRecognizer(tapGesture)
// 2. hide keyboard if background if tapped
let hideKeyboard = UITapGestureRecognizer(target: self, action: #selector(hideKeyboardWhenBackGroundTapped))
view.addGestureRecognizer(hideKeyboard)
// 3. hide keyboard if textView is tapped
textView.addGestureRecognizer(hideKeyboard)
// 4. hide viewForMessageLabel for textField if background is tapped
textField.addTarget(self, action: #selector(removeViewForMessageLabel), for: .editingDidBegin)
}
//MARK:- Button
@IBAction func buttonPressed(_ sender: UIButton) {
view.addSubview(viewForMessageLabel)
setViewForMessageLabelAnchors()
setMessageLabelAnchors()
}
//MARK:- Functions
func setViewForMessageLabelAnchors(){
viewForMessageLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 44).isActive = true
viewForMessageLabel.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
viewForMessageLabel.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
viewForMessageLabel.addSubview(messagelabel)
}
func setMessageLabelAnchors(){
messagelabel.topAnchor.constraint(equalTo: viewForMessageLabel.topAnchor, constant: 0).isActive = true
messagelabel.widthAnchor.constraint(equalTo: viewForMessageLabel.widthAnchor).isActive = true
viewForMessageLabel.bottomAnchor.constraint(equalTo: messagelabel.bottomAnchor, constant: 0).isActive = true
}
func removeViewForMessageLabel(){
viewForMessageLabel.removeFromSuperview()
}
func hideKeyboardWhenBackGroundTapped(){
textField.resignFirstResponder()
}
//MARK:- TextField Delegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
view.endEditing(true)
return true
}
func textViewDidBeginEditing(_ textView: UITextView) {
removeViewForMessageLabel()
}
//MARK:- TextView Delegate
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if(text == "\n") {
textView.resignFirstResponder()
return false
}
return true
}
}
最佳答案
如果textField或textView的键盘存在,并且我按下背景,键盘也应该消失。
你呈现这个就像它的条件是键盘是否当前显示,但你的代码没有反映出这一点(它不应该)。你可以想打多少次电话给resignFirstResponder
,就不会发生什么坏事。您还可以对已删除的视图调用removeFromSuperview
(请参见here)。
因此,我认为您可以将一个动作附加到一个轻敲手势识别器上:
var tapGesture: UITapGestureRecognizer?
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
textView.delegate = self
// 0. hide viewForMessageLabel is background is tapped
tapGesture = UITapGestureRecognizer(target: self, action: #selector(removeLabelAndHideKeyboard))
view.addGestureRecognizer(tapGesture)
// 1. hide viewForMessageLabel if textView is tapped
textView.addGestureRecognizer(tapGesture)
}
func removeLabelAndHideKeyboard() {
viewForMessageLabel.removeFromSuperview()
textField.resignFirstResponder()
}