iOS点击位于子视图中的uitextfield外部时隐藏键盘?

时间:2014-11-20 11:51:34

标签: ios objective-c uitextfield

我是新来的(也是我来自阿根廷,对不起我的基本英语),我真的不知道该怎么办这个问题。我有一个视图,其中包含一个UIScrollView来滚动视图,在里面我有一个包含大量UITextField的子视图。关键是当我触摸UITextField之外时隐藏键盘,但我认为触摸事件没有被触发(我不知道为什么,也许你可以帮助我)。我也是iOS的新手,但我正在学习!谢谢!

编辑:首先,感谢大家的回答!对于所有说禁用usar交互的人来说,它适用于UITextFields,但现在我无法与UITable的内部子视图交互!有什么帮助吗?

5 个答案:

答案 0 :(得分:1)

试试这个

- (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];
        }
    }
}

答案 1 :(得分:1)

滚动视图将拦截手势识别器,您需要在单击文本字段后禁用视图上的用户交互。

你可以通过;

来做到这一点
scrollView.userInteractionEnabled = NO;

答案 2 :(得分:0)

希望这会对你有所帮助:)。

- (void)viewDidLoad
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                       initWithTarget:self
                                       action:@selector(dismissKeyboard)];
        [self.view addGestureRecognizer:tap];
}



    -(void)dismissKeyboard
    {
    [svScrollView setContentOffset:CGPointMake(0, 0) animated:YES];

        [self resignFirstResponder];

    }

答案 3 :(得分:0)

XIB文件中有属性,请在下面截图中找到。

XIB Scrollview Property

答案 4 :(得分:0)

夫特:

下一个解决方案适用于UITextView,但您可以为UITextField修改它

1)在包含该字段的视图控制器中添加keyboardWillShow事件处理程序并保存对该字段的引用

/**
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)
    }
}