I'm need advice on how to do this (best practice way):
I'm using XCode 6.1.1 using Swift with autolayout ENABLED.
So in the picture I already set the layout that way.
Note that in the scrollview, I put UITableView
along with another UIView
which contain a UITextField
in it
I also already implement this: How to make a UITextField move up when keyboard is present?
So here's my code:
func registerForKeyboardNotifications()
{
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidShow:", name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidHide:", name: UIKeyboardDidHideNotification, object: nil)
}
func keyboardDidShow(aNotification: NSNotification)
{
var info: NSDictionary = aNotification.userInfo!
var kbSize: CGSize = (info.objectForKey(UIKeyboardFrameBeginUserInfoKey)?.CGRectValue() as CGRect!).size
var contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0)
svBody.contentInset = contentInsets
svBody.scrollIndicatorInsets = contentInsets
self.view.layoutIfNeeded()
}
func keyboardDidHide(aNotification: NSNotification)
{
var contentInsets: UIEdgeInsets = UIEdgeInsetsZero
svBody.contentInset = contentInsets
svBody.scrollIndicatorInsets = contentInsets
self.view.layoutIfNeeded()
}
The problem is when I run it, I can also scroll my viewBottom
behind the keyboard which I don't want that.
What I'm trying to achieve are:
- I need the
viewBottom
to stick on top of the keyboard - Only
myTable
that can be scrolled
How can I do this the best way?