UITextField是不可编辑的,只有触摸事件,没有编辑事件

时间:2019-12-05 19:17:25

标签: ios swift iphone xcode uitextfield

我在Google Maps View(GMSView)上方有一个UITextField。当我点击Textfield时,它不会成为第一响应者,任何委托或动作事件(例如textFieldShouldBeginEditing之类的Delete Event和.allEditingEvents之类的Action事件)都不会起作用。

Textfield成为第一响应者的唯一方法是使用

对其进行硬编码。
tf.becomeFirstResponder()

但是当我点击它时,什么也没发生。

Action Touch事件(如.touchUpInside等)确实可以在TextField上正常工作,因此我知道框架是可以的。

我正在这样创建和添加TextView:

tf = UITextField(frame: CGRect(x: 100, y: 200, width: 300, height: 50))
tf.backgroundColor = .white
tf.placeholder = "sdfdsf"
tf.translatesAutoresizingMaskIntoConstraints = false
tf.addTarget(self, action: #selector(self.tapped), for: .touchUpInside)

self.mapView.addSubview(tf)

tf.centerXAnchor.constraint(equalTo: mapView.centerXAnchor).isActive = true
tf.centerYAnchor.constraint(equalTo: mapView.centerYAnchor).isActive = true
tf.widthAnchor.constraint(equalToConstant: 300).isActive = true
tf.heightAnchor.constraint(equalToConstant: 50).isActive = true

如果您知道为什么会发生这种情况(或者在这种情况下不会发生),请发表评论或回答。我已经连续进行了超过2天的工作。

  

更新:

     

我实际上将文本字段添加到了Mapview本身。 Mapview   有点对视图和混乱的层次有些奇怪   在TextView的视图上。

     

解决方案是将TextView添加到主视图,而不是添加到   MapView子视图。

     

所以(从后到前):

     

ViewController

     

(主)视图

       Google Maps view
       View
          TextField
     

这有效

3 个答案:

答案 0 :(得分:0)

请尝试以下操作: self.mapView.bringSubviewToFront(tf)//在“ self.mapView.addSubview(tf)”下面添加此行 这样可以确保tf(TextField位于顶部且可贴壁)

答案 1 :(得分:0)

您是否将委托分配给文本字段,如:- tf。委托=自我

答案 2 :(得分:0)

  

如果要将子视图置于最前面,可以使用:

self.view.bringSubviewToFront(tf)
相关问题