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

我使用的是XCode 6.1.1,使用Swift并启用了自动收费。所以,在图片中,我已经设置了这样的布局。注意,在滚动视图中,我将UITableView与另一个包含UITextField的UIView放在一起。
我也已经实现了这一点: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我怎么才能做到最好的方法?
发布于 2015-01-09 04:35:39
好吧,我自己想出来的。
以下是供每个人使用的代码:
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()
})
}很好分享;)干杯,
禤浩焯
https://stackoverflow.com/questions/27845397
复制相似问题