聊天 Screen/Window 布局设计
Chat Screen/Window Layout Design
我需要有关如何执行此操作的建议(最佳实践方式):
我正在使用 XCode 6.1.1,使用 Swift 并启用了自动布局。
所以在图片中我已经这样设置了布局。
请注意,在滚动视图中,我将 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
滚动到键盘后面,我不希望这样。
我想要实现的是:
- 我需要
viewBottom
粘在键盘上
- 只有
myTable
可以滚动
我怎样才能最好地做到这一点?
好吧,我自己想出来了。
下面是供大家使用的代码:
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()
})
}
好分享 ;)
干杯,
阿德里安
我需要有关如何执行此操作的建议(最佳实践方式):
我正在使用 XCode 6.1.1,使用 Swift 并启用了自动布局。
所以在图片中我已经这样设置了布局。
请注意,在滚动视图中,我将 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
滚动到键盘后面,我不希望这样。
我想要实现的是:
- 我需要
viewBottom
粘在键盘上 - 只有
myTable
可以滚动
我怎样才能最好地做到这一点?
好吧,我自己想出来了。
下面是供大家使用的代码:
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()
})
}
好分享 ;) 干杯,
阿德里安