2017-08-01 58 views
0

问题很明显,当用户看到键盘正在阻止他们想要键入的文本框而不是按回车键或关闭键盘并点击下一个字段时,他们试图平移到下一个领域。由于我的内容与iPad的大小相匹配,因此当用户尝试平移时,滚动视图不会自动滚动。老实说,我不希望它滚动,除非键盘在屏幕上。希望能够手动滚动以在键盘上方拉文本框

但是,在滚动视图上启用滚动并不能解决问题;即使在这种情况下,它仍然不会响应平移。使viewcontroller不是scrollview的委托并覆盖函数scrollViewDidScroll。我如何获得滚动视图以启用平移,特别是只有在启用键盘时才启用平移。

由于解决方案已经张贴完全不是那么回事,我想我会后我的keyboardWillBeShown和keyboardWillBeHidden代码:

func keyboardWillBeShown(_ sender: Notification) 
{ 
    self.myScrollView.isScrollEnabled = true 

    let info: NSDictionary = (sender as NSNotification).userInfo! as NSDictionary 
    let value: NSValue = info.value(forKey: UIKeyboardFrameBeginUserInfoKey) as! NSValue 
    let keyboardSize: CGSize = value.cgRectValue.size 
    let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0) 
    self.myScrollView.contentInset = contentInsets 
    self.myScrollView.scrollIndicatorInsets = contentInsets 
    self.myScrollView.scrollRectToVisible(self.myScrollView.frame, animated: true) 
    var aRect: CGRect = self.view.frame 
    aRect.size.height -= keyboardSize.height 
    let activeTextFieldRect: CGRect? = activeField?.frame 
    let activeTextFieldOrigin: CGPoint? = activeTextFieldRect?.origin 
    if activeTextFieldOrigin != nil 
    { 
     if (!aRect.contains(activeTextFieldOrigin!)) 
     { 
      let scrollpoint : CGPoint = CGPoint(x: 0.0, y: activeField!.frame.origin.y - keyboardSize.height) 
      self.myScrollView.setContentOffset(scrollpoint, animated: true)//.scrollRectToVisible((activeField?.frame)!, animated: true) 
     } 
    } 

} 

func keyboardWillBeHidden(_ sender: Notification) 
{ 
    myScrollView.isScrollEnabled = false 
    let contentInsets: UIEdgeInsets = UIEdgeInsets.zero 
    myScrollView.contentInset = contentInsets 
    myScrollView.scrollIndicatorInsets = contentInsets 
} 

回答

0

我想出如何得到我想要的东西。当键盘显示时,将myScrollView.contentSize设置为与屏幕和键盘一样大,然后将大小减小到键盘被隐藏时的大小。然后使视图控制器成为UIScrollViewDelegate,设置myScrollView.delegate = self,并实现scrollViewDidScroll,以便如果文本字段正在编辑,并且该文本字段不为空,则更改哪个文本字段是第一个响应者。一旦你意识到设置.contentSize的诀窍,一块蛋糕!

0

试试这个,

声明变量使用

var originalViewCGRect: CGRect? 
var originalOffset: CGPoint! 

in viewDidLoad add keyboard observers

NotificationCenter.default.addObserver(self 
     , selector: #selector(keyboardWillAppear(_:)) 
     , name: NSNotification.Name.UIKeyboardWillShow 
     , object: nil) 
    NotificationCenter.default.addObserver(self 
     , selector: #selector(keyboardWillDisappear(_:)) 
     , name: NSNotification.Name.UIKeyboardWillHide 
     , object: nil) 

最后,添加这些功能

func keyboardWillAppear(_ notification: Foundation.Notification){ 
     self.scrollView.scrollRectToVisible(self.scrollView.frame, animated: true) 
     let keyboardSize:CGSize = ((notification as NSNotification).userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue.size 
     let insets = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0) 
     self.scrollView.contentInset = insets 
    } 


    func keyboardWillDisappear(_ notification: Foundation.Notification){ 
     let beginFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue 
     let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue 
     let delta = (endFrame.origin.y - beginFrame.origin.y) 
     self.view.frame = self.originalViewCGRect! 
     self.scrollView.setContentOffset(CGPoint(x: 0, y: max(self.scrollView.contentOffset.y - delta, 0)), animated: true) 
    } 

总之,这将调整滚动视图的内容插入时被示出和/或隐藏的键盘。

希望这会有所帮助。

编辑 固定调整

+0

嗨,非常感谢您花时间回复我的帖子。不过,我确实发现,我已经解决了比您发布的代码更多的问题。我只需要解决能够平移和返回的额外问题(如果用户点击返回,我的代码将滚动到屏幕的上半部分(如有必要),因此平移是我需要解决的唯一问题)。 –

+0

嗨,你有没有尝试添加'UITextFieldDelegate'到你的ViewController并添加函数'textFieldShouldReturn'? – Kiester

相关问题