我需要有关如何做到这一点的建议(最佳实践方式):
我使用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
,我不想要它。
我想要实现的目标是:
viewBottom
贴在键盘顶部myTable
我怎样才能以最好的方式做到这一点?
答案 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()
})
}
很高兴分享;) 欢呼声,
阿德里安