Skip to content

Instantly share code, notes, and snippets.

@ChrisBuchholz
Last active August 29, 2015 14:14
Show Gist options
  • Save ChrisBuchholz/2c7a5d85e03e85d26dd5 to your computer and use it in GitHub Desktop.
Save ChrisBuchholz/2c7a5d85e03e85d26dd5 to your computer and use it in GitHub Desktop.
interactive keyboard dismiss bug
import UIKit
class DetailViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
var textView: UITextView?
var detailItem: AnyObject?
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidShow:", name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if let scrollView = scrollView {
textView = UITextView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
let content = "fj fdslkf jfsdl kfjsdfl ksjfsdlkjf s " +
"flkf jsf sdlf jdsflksdj flskj flsdkfj k" +
"lfds jflsdkfj lsdfj flkdsjf lskdfj slkjfd sdlfk" +
"flkf jsf sdlf jdsflksdj flskj flsdkfj k" +
"lfds jflsdkfj lsdfj flkdsjf lskdfj slkjfd sdlfk" +
"flkf jsf sdlf jdsflksdj flskj flsdkfj k"
textView?.text = content
scrollView.addSubview(textView!)
var newScrollViewContentSize = CGSize(width: textView!.frame.width, height: textView!.frame.height)
scrollView.contentSize = newScrollViewContentSize
var newInsets = UIEdgeInsets(top: topLayoutGuide.length, left: 0, bottom: 0, right: 0)
scrollView.contentInset = newInsets
scrollView.scrollIndicatorInsets = newInsets
}
}
func keyboardDidShow(notification: NSNotification) {
println("did show")
if let scrollView = scrollView {
let info: NSDictionary = notification.userInfo!
let kbSize = info.objectForKey(UIKeyboardFrameEndUserInfoKey)!.CGRectValue().size
var newInsets = UIEdgeInsets(top: topLayoutGuide.length, left: 0, bottom: kbSize.height, right: 0)
scrollView.contentInset = newInsets
scrollView.scrollIndicatorInsets = newInsets
}
}
func keyboardWillHide(notification: NSNotification) {
println("will hide")
if let scrollView = scrollView {
var newInsets = UIEdgeInsets(top: topLayoutGuide.length, left: 0, bottom: 0, right: 0)
scrollView.contentInset = newInsets
scrollView.scrollIndicatorInsets = newInsets
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment