I've tried to move the screen up when keyboard appears. I did it with success but I need to know how to scroll the view only when de bottom UITextView
is clicked, and not with the top TextView
because keyboard just touches the TextView
below.
App is this:
Our view
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<UITouch>, 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
}
IQKeyboardManager was a godsend for me!
Step 1:
All you have to do is download the project from https://github.com/hackiftekhar/IQKeyboardManager and move the IQKeyboardManagerSwift directory from the demo project to your project.
Make sure the IQKeyboardManagerSwift directory is the right location in your project once you move it.
Step 2:
Then In AppDelegate.swift, just enable IQKeyboardManager in your application function.
@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
}
}
That's it! Now your keyboard will slide up and move the view accordingly. Props to the creator of this framework, makes our lives a lot easier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With