通过代码添加时 UITextField 不起作用
UITextField doesn't work when added via code
我在通过代码添加 UITextField 时遇到问题。当我在空视图控制器中使用这个简单的代码时,一切正常:
let textField = UITextField(frame: CGRect(x: 10, y: 10, width: 200, height: 20))
textField.text = "test"
scrollView.addSubview(textField)
但在我的自定义 UIScrollView
中,由于某种原因它不起作用(编辑未开始)。
我尝试为 textField
和 superview 设置 userInteractionEnabled
但这没有帮助。我不确定如何调试以查看哪个 属性 正在阻止编辑。没有弹出键盘;我无法在模拟器中输入 and/or 进行选择。同样奇怪的是,其他控件(如 UIButton)可以完美运行并接收触摸。
可以看出,我没有添加委托,但它在没有它的空 UIViewController 上运行得非常好。
po textField.canBecomeFirstResponder()
returns 正确。
将scrollView.delaysContentTouches设置为否
您可能需要想出自己的逻辑来转动它 off/on 取决于意外触摸 scrollView 内的控件是否会成为问题。
您也可以在不滚动的情况下点击并按住 textField。它应该调出键盘,但大多数用户可能不知道这样做。
我正在阅读 apple documentation 并且我发现了这个:
If the value of this property is true, the scroll view delays handling the touch-down gesture until it can determine if scrolling is
the intent. If the value is false , the scroll view immediately calls
touchesShouldBegin:withEvent:inContentView:. The default value is
true.
这来自 apple documentation:
This method traverses the view hierarchy by calling the
pointInside:withEvent: method of each subview to determine which
subview should receive a touch event. If pointInside:withEvent:
returns true, then the subview’s hierarchy is similarly traversed
until the frontmost view containing the specified point is found. If a
view does not contain the point, its branch of the view hierarchy is
ignored.
我在通过代码添加 UITextField 时遇到问题。当我在空视图控制器中使用这个简单的代码时,一切正常:
let textField = UITextField(frame: CGRect(x: 10, y: 10, width: 200, height: 20))
textField.text = "test"
scrollView.addSubview(textField)
但在我的自定义 UIScrollView
中,由于某种原因它不起作用(编辑未开始)。
我尝试为 textField
和 superview 设置 userInteractionEnabled
但这没有帮助。我不确定如何调试以查看哪个 属性 正在阻止编辑。没有弹出键盘;我无法在模拟器中输入 and/or 进行选择。同样奇怪的是,其他控件(如 UIButton)可以完美运行并接收触摸。
可以看出,我没有添加委托,但它在没有它的空 UIViewController 上运行得非常好。
po textField.canBecomeFirstResponder()
returns 正确。
将scrollView.delaysContentTouches设置为否
您可能需要想出自己的逻辑来转动它 off/on 取决于意外触摸 scrollView 内的控件是否会成为问题。
您也可以在不滚动的情况下点击并按住 textField。它应该调出键盘,但大多数用户可能不知道这样做。
我正在阅读 apple documentation 并且我发现了这个:
If the value of this property is true, the scroll view delays handling the touch-down gesture until it can determine if scrolling is the intent. If the value is false , the scroll view immediately calls touchesShouldBegin:withEvent:inContentView:. The default value is true.
这来自 apple documentation:
This method traverses the view hierarchy by calling the pointInside:withEvent: method of each subview to determine which subview should receive a touch event. If pointInside:withEvent: returns true, then the subview’s hierarchy is similarly traversed until the frontmost view containing the specified point is found. If a view does not contain the point, its branch of the view hierarchy is ignored.