聊天屏幕/窗口布局设计

时间:2015-01-08 16:52:22

标签: ios xcode swift

我需要有关如何做到这一点的建议(最佳实践方式):

chat screen design[][1]

我使用XCode 6.1.1使用Swift和autolayout ENABLED。 所以在图片中我已经设置了这种布局。 请注意,在滚动视图中,我将UITableView与其中包含UIView的其他UITextField放在一起

我也已经实现了这个:How to make a UITextField move up when keyboard is present?

所以这是我的代码:

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()
}

问题是,当我运行它时,我还可以滚动键盘后面的viewBottom,我不想要它。

result[][2]

我想要实现的目标是:

  • 我需要viewBottom贴在键盘顶部
  • 只能滚动myTable

我怎样才能以最好的方式做到这一点?

1 个答案:

答案 0 :(得分:1)

好的,我自己想出来了。

以下是每个人都可以使用的代码:

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()
    })
}

很高兴分享;) 欢呼声,

阿德里安