当点击位于子视图中的uitextfield外部时,iOS是否隐藏键盘?
我是新来的(我也是阿根廷人,很抱歉我的基础英语),我真的不知道该怎么解决这个问题。我有一个视图,其中包含一个用于滚动视图的当点击位于子视图中的uitextfield外部时,iOS是否隐藏键盘?,ios,objective-c,uitextfield,Ios,Objective C,Uitextfield,我是新来的(我也是阿根廷人,很抱歉我的基础英语),我真的不知道该怎么解决这个问题。我有一个视图,其中包含一个用于滚动视图的UIScrollView,在其中我有一个子视图,其中包含许多UITextField。关键是当我在UITextField之外触摸时隐藏键盘,但我认为触摸事件没有触发(我不知道为什么,也许你可以帮我)。我也是iOS的新手,但我正在学习!谢谢 编辑:首先,感谢大家的回答!对于所有说要禁用usar交互的人来说,它适用于UITextFields,但现在我无法与UITable的内部de子
UIScrollView
,在其中我有一个子视图,其中包含许多UITextField
。关键是当我在UITextField
之外触摸时隐藏键盘,但我认为触摸事件没有触发(我不知道为什么,也许你可以帮我)。我也是iOS的新手,但我正在学习!谢谢
编辑:首先,感谢大家的回答!对于所有说要禁用usar交互的人来说,它适用于UITextFields,但现在我无法与UITable的内部de子视图交互!有什么帮助吗?试试这个
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
还是这个
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIView * youtxtfl in self.view.subviews){
if ([youtxtfl isKindOfClass:[UITextField class]] && [youtxtfl isFirstResponder]) {
[youtxtfl resignFirstResponder];
}
}
}
滚动视图将截取手势识别器,您需要在单击文本字段后禁用视图上的用户交互 你可以通过
scrollView.userInteractionEnabled = NO;
希望这对您有所帮助:)
XIB文件中有属性,请查看下面的屏幕截图 雨燕: 下一个解决方案是UITextView,但您可以为UITextField修改它 1) Add Keyboard将在包含字段的视图控制器中显示事件处理程序,并保存对字段的引用
/**
Listen keyboard events
:param: animated the animation flag
*/
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
initNavigation()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
/**
Remove self as keyboard listener
:param: animated the animation flag
*/
override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
/**
Save reference to focused textView
:param: sender the sender
*/
func keyboardWillShow(sender: AnyObject){
if self.searchTextField?.isFirstResponder() ?? false {
KeyboardDismissingUIViewTarget = self.searchTextField
}
}
/**
Clean KeyboardDismissingUIViewTarget
:param: sender the sender
*/
func keyboardWillHide(sender: AnyObject){
KeyboardDismissingUIViewTarget = nil
}
2) 对最顶层的UIView使用自定义类:
/// the target view (textView) which should not dismiss the keyboard
var KeyboardDismissingUIViewTarget: UIView?
/**
* Custom class for top view that dismisses keyboard when tapped outside the given textView or textField
*
* @author Alexander Volkov
* @version 1.0
*/
class KeyboardDismissingUIView: UIView {
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
if let targetView = KeyboardDismissingUIViewTarget as? UITextView {
// Convert the point to the target view's coordinate system.
// The target view isn't necessarily the immediate subview
let pointForTargetView = targetView.convertPoint(point, fromView: self)
if CGRectContainsPoint(targetView.bounds, pointForTargetView) {
return targetView.hitTest(pointForTargetView, withEvent: event)
}
else {
KeyboardDismissingUIViewTarget = nil
targetView.resignFirstResponder()
return nil
}
}
return super.hitTest(point, withEvent: event)
}
}
尝试scrollView.userInteractionEnabled=NO?好吧,这是可行的,但现在我有一个问题,我不能与位于同一位置的UITables交互。
/// the target view (textView) which should not dismiss the keyboard
var KeyboardDismissingUIViewTarget: UIView?
/**
* Custom class for top view that dismisses keyboard when tapped outside the given textView or textField
*
* @author Alexander Volkov
* @version 1.0
*/
class KeyboardDismissingUIView: UIView {
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
if let targetView = KeyboardDismissingUIViewTarget as? UITextView {
// Convert the point to the target view's coordinate system.
// The target view isn't necessarily the immediate subview
let pointForTargetView = targetView.convertPoint(point, fromView: self)
if CGRectContainsPoint(targetView.bounds, pointForTargetView) {
return targetView.hitTest(pointForTargetView, withEvent: event)
}
else {
KeyboardDismissingUIViewTarget = nil
targetView.resignFirstResponder()
return nil
}
}
return super.hitTest(point, withEvent: event)
}
}