简体   繁体   English

如何在主视图的滚动视图内的子视图中关闭IOS Swift中的键盘?

[英]How to dismiss keyboard in IOS Swift from a subview inside a scroll view in the main view?

I am having a 2 Subviews inside a scroll view in a main view. 我在主视图的滚动视图中有2个子视图。 Bases on a two buttons in the scroll view the 2sub views will show and hide. 基于滚动视图中的两个按钮,两个子视图将显示和隐藏。 Both the subviews have one text field each to enter numbers. 两个子视图都有一个文本字段,每个文本字段都可以输入数字。 the keypad is a num pad. 键盘是数字键盘。 I have implemented func touchesBegan and also the UITapGestureRecognizer for both the subviews and added the resignFirstResponder() as well as endEditing(true) . 我已经为子视图实现了func touchesBeganUITapGestureRecognizer ,并添加了resignFirstResponder()endEditing(true) But still my keyboard does not goes away. 但是我的键盘仍然没有消失。 I cant find a proper solution for this type of scenario in any forum. 我无法在任何论坛中找到针对此类情况的适当解决方案。

I am using Swift 2.0 xcode7.1.1 我正在使用Swift 2.0 xcode7.1.1

Edit: My code ( Since it is a business project i cannot share the full content sorry) 编辑:我的代码(由于这是一个业务项目,所以我无法共享全部内容,抱歉)

in this code when i touch outside in the viewAutomatic the keyboard goes. 在此代码中,当我在viewAutomatic触摸外部时,键盘就消失了。 but when i touch outside in the viewManual the keyboard does not goes off. 但是当我在viewManual触摸外部时,键盘没有关闭。 It is so weird for me. 对我来说太奇怪了。

@IBOutlet weak var viewScroll: UIScrollView!
@IBOutlet weak var txtNewBid: UITextField!
@IBOutlet weak var txtMyMaxBid: UITextField!
@IBOutlet weak var viewManual: UIView!
@IBOutlet weak var viewAutomatic: UIView!


override func viewDidLoad() {
 let singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: "singleTapped:")
        singleTapGestureRecognizer.numberOfTapsRequired = 1
        singleTapGestureRecognizer.numberOfTouchesRequired = 1
        singleTapGestureRecognizer.enabled = true
        singleTapGestureRecognizer.cancelsTouchesInView = false

        self.viewManual.addGestureRecognizer(singleTapGestureRecognizer)
        self.viewAutomatic.addGestureRecognizer(singleTapGestureRecognizer)
}
 override func viewWillAppear(animated: Bool) {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}

 func singleTapped(sender: UITapGestureRecognizer) {

    self.view.endEditing(true)
    self.viewManual.endEditing(true)
    self.viewAutomatic.endEditing(true)
    self.viewScroll.endEditing(true)
    txtNewBid.resignFirstResponder()
    txtMyMaxBid.resignFirstResponder()

}
 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    self.view.endEditing(true)
    txtNewBid.resignFirstResponder()
    txtMyMaxBid.resignFirstResponder()
}

func keyboardWillShow(notification: NSNotification) {
    // adjusting the scroll view for the keyboard
    let frame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    if( self.viewManual.hidden == true)
    {
        self.viewScroll.frame.size.height -= frame.height 
    }
    else
    {
        self.viewScroll.frame.size.height -= frame.height 
    }
    let bottomOffset: CGPoint = CGPointMake(0, self.txtNewBid.frame.origin.y + self.txtNewBid.frame.size.height)
    self.viewScroll.contentOffset = bottomOffset
}

func keyboardWillHide(notification: NSNotification) {

    let frame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    if( self.viewManual.hidden == true)
    {
        self.viewScroll.frame.size.height += frame.height 
    }
    else
    {
        self.viewScroll.frame.size.height += frame.height 
    }

    }
override func viewWillDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(self)
    txtNewBid.text = ""
    txtMyMaxBid.text = ""
}

My guess is that you are approaching the problem the wrong way. 我的猜测是您正在以错误的方式解决问题。 I assume that the controller should be responsible for dismissing the keyboard, but you might try something like this (preferrably on your hosting view controller): 我认为控制器应该负责关闭键盘,但是您可以尝试执行以下操作(最好在托管视图控制器上):

yourView.endEditing(true) // boolean parameter specifies whether to force dismissal or not

endEditing(force) should traverse the view hierarchy and check for any active responders, so this should actually work. endEditing(force)应该遍历视图层次结构并检查是否有任何活动的响应者,因此这实际上应该起作用。 Have you checked if you indeed reference the correct view ? 您是否检查过您是否确实引用了正确的视图? Can you add some of your code (Please just the necessary portions) ? 您能否添加一些代码(请仅添加必要的部分)?

The reason I point out that it's important to call this on your hosting controller's view is that it goes through all the subviews and checks for any active responders. 我指出,在托管控制器的视图上调用此控件很重要,因为它会遍历所有子视图并检查任何活动的响应者。

If you have a reference to the textfield and you know for sure which one it is you can also call 如果您对文本字段有一个引用,并且您知道它是哪一个,也可以致电

yourTextField.resignFirstResponder()

I guess that's what endEditing(force) does internally, it traverses all the subviews and calls resignFirstResponder() on any textField it encounters that also is the first responder. 我想这就是endEditing(force)内部执行的操作,它遍历所有子视图,并在遇到的任何resignFirstResponder()也是第一个响应者resignFirstResponder()上调用resignFirstResponder()

EDIT 编辑

Due to the fact that the UIScrollView will intercept gestures, I would directly add the tap gesture recognizer to the scroll view like so: 由于UIScrollView会拦截手势,因此我可以将滚动手势识别器直接添加到滚动视图中,如下所示:

self.viewScroll.addGestureRecognizer(singleTapGestureRecognizer)

And then do something like this: 然后执行以下操作:

func singleTapped(sender: UITapGestureRecognizer) {

    // this should suffice
    self.viewScroll.endEditing(true)
    // or call it on the touched view (in this case the viewScroll)
    // sender.view.endEditing(true)

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM