0

I'm need advice on how to do this (best practice way):

chat screen design[][1]

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.

result[][2]

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?

Community
  • 1
  • 1
Adrian
  • 434
  • 6
  • 16

1 Answers1

1

Ok I figured it out by myself.

Here's the code for everyone to use:

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

    UIView.animateWithDuration(0.2, animations: { () -> Void in
        var contentInsets: UIEdgeInsets = UIEdgeInsetsMake(kbSize.height, 0, 0, 0)
        self.myTable.contentInset = contentInsets
        self.myTable.scrollIndicatorInsets = contentInsets

        var contentOffsetSV: CGPoint = CGPointMake(0, kbSize.height)
        self.svBody.contentOffset = contentOffsetSV

        self.view.layoutIfNeeded()
    })

}

func keyboardDidHide(aNotification: NSNotification)
{
    UIView.animateWithDuration(0.2, animations: { () -> Void in
        var contentInsets: UIEdgeInsets = UIEdgeInsetsZero
        self.myTable.contentInset = contentInsets
        self.myTable.scrollIndicatorInsets = contentInsets

        var contentOffsetSV: CGPoint = CGPointMake(0, 0)
        self.svBody.contentOffset = contentOffsetSV

        self.view.layoutIfNeeded()
    })
}

Good to share ;) Cheers,

Adrian

Adrian
  • 434
  • 6
  • 16