当前位置:  开发笔记 > IOS > 正文

键盘出现时向上移动屏幕,具体取决于textView.迅速

如何解决《键盘出现时向上移动屏幕,具体取决于textView.迅速》经验,为你挑选了1个好方法。

当键盘出现时,我试图将屏幕向上移动.我成功完成了它但我需要知道如何仅在UITextView单击底部时滚动视图,而不是顶部,TextView因为键盘只是触及TextView下面.

应用是这样的:

我们的观点

class viewControllerCuatro: UIViewController, UITextViewDelegate {

 @IBOutlet weak var textViewInteriorUno: UITextView!
@IBOutlet weak var textViewInteriorDos: UITextView!

override func viewDidLoad() {


self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Atrás", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)


**NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: self.view.window)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: self.view.window)**




}


 **func keyboardWillShow(sender: NSNotification) {**



// 1
let userInfo: [NSObject : AnyObject] = sender.userInfo!
// 2
let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size
let offset: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue.size
// 3
if keyboardSize.height == offset.height {
    UIView.animateWithDuration(0.1, animations: { () -> Void in
        self.view.frame.origin.y -= keyboardSize.height
    })
} else {
    UIView.animateWithDuration(0.1, animations: { () -> Void in
        self.view.frame.origin.y += keyboardSize.height - offset.height
    })
    }


}

**func keyboardWillHide(sender: NSNotification)** {
let userInfo: [NSObject : AnyObject] = sender.userInfo!
let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size
self.view.frame.origin.y += keyboardSize.height
}

**override func viewWillDisappear(animated: Bool)** {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: self.view.window)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: self.view.window)
}


  override func touchesBegan(touches: Set, withEvent event: UIEvent?) {

if textViewInteriorUno.text == ""  || textViewInteriorUno.text.isEmpty == true || textViewInteriorDos.text == "" || textViewInteriorDos.text.isEmpty == true {


    textViewInteriorUno.resignFirstResponder()


    textViewInteriorDos.resignFirstResponder()
}
else{

    self.textViewInteriorUno.resignFirstResponder()

    self.textViewInteriorDos.resignFirstResponder()
}
}

   func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool{

var shouldReplace = true // For most cases, return true, only modify if we have a special case

// If the textView is 'textViewInteriorUno'
if textView.isEqual(textViewInteriorUno) {

let newLength = textViewInteriorUno.text.utf16.count + text.utf16.count - range.length
shouldReplace = newLength <= 12           // will be true if the length is <= 12

}

else if textView.isEqual(textViewInteriorDos) {

let newLength = textViewInteriorDos.text.utf16.count + text.utf16.count - range.length
shouldReplace = newLength < 13 // Will be true if the length > 6


}

return shouldReplace
}

Matthew Zour.. 11

IQKeyboardManager对我来说是个天赐之物!

步骤1:

您所要做的就是从https://github.com/hackiftekhar/IQKeyboardManager下载该项目,并将IQKeyboardManagerSwift目录从演示项目移动到您的项目中.

移动后,确保IQKeyboardManagerSwift目录是项目中的正确位置.

第2步:

然后在AppDelegate.swift中,只需在应用程序函数中启用IQKeyboardManager.

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
  {

  // This is the only line of code you need to manually add
  // in order for IQKeybaordManager to work. Thats it.
  IQKeyboardManager.sharedManager().enable = true

  return true
  }
}

而已!现在,您的键盘将向上滑动并相应地移动视图.对这个框架的创建者的道具,使我们的生活更容易.



1> Matthew Zour..:

IQKeyboardManager对我来说是个天赐之物!

步骤1:

您所要做的就是从https://github.com/hackiftekhar/IQKeyboardManager下载该项目,并将IQKeyboardManagerSwift目录从演示项目移动到您的项目中.

移动后,确保IQKeyboardManagerSwift目录是项目中的正确位置.

第2步:

然后在AppDelegate.swift中,只需在应用程序函数中启用IQKeyboardManager.

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
  {

  // This is the only line of code you need to manually add
  // in order for IQKeybaordManager to work. Thats it.
  IQKeyboardManager.sharedManager().enable = true

  return true
  }
}

而已!现在,您的键盘将向上滑动并相应地移动视图.对这个框架的创建者的道具,使我们的生活更容易.

推荐阅读
围脖上的博博_771
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有