Ios Swift 4:键盘显示时移动uiview
我有一个Uiviewcontroller,在这个viewcontroller中我放置了一个名为categUIV的uiview。categUIV包含uitexfields和一个按钮。这样做的目的是在键盘打开时将孔向上移动 我使用下面的代码Ios Swift 4:键盘显示时移动uiview,ios,swift,Ios,Swift,我有一个Uiviewcontroller,在这个viewcontroller中我放置了一个名为categUIV的uiview。categUIV包含uitexfields和一个按钮。这样做的目的是在键盘打开时将孔向上移动 我使用下面的代码 override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
self.categUIV.frame.origin.y -= keyboardSize.height
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
self.categUIV.frame.origin.y += keyboardSize.height
}
}
现在,当我点击一个UITExted时,键盘弹出,categUIV正确弹出,问题是当我开始键入categUIV时,categUIV会自动返回到初始位置
我不知道为什么,但我想知道的是,可能是因为categUIV的约束?问题在于框架布局无法按预期工作,请尝试自动布局,将categUIV的底部约束挂接为IBOutlet,然后执行此操作
@objc func handleKeyboardDidShow (notification: NSNotification)
{
let keyboardRectAsObject = notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
var keyboardRect = CGRect.zero
keyboardRectAsObject.getValue(&keyboardRect)
self.categUIVBotcon.constant = -1 * keyboardRect.height
UIView.animate(withDuration: 0.5,animations: {
self.view.layoutIfNeeded()
})
}
@objc func handleKeyboardWillHide(notification: NSNotification)
{
self.categUIVBotcon.constant = 0
UIView.animate(withDuration: 0.5,animations: {
self.view.layoutIfNeeded()
})
}
问题是框架布局的事情没有按预期工作,请尝试自动布局,将categUIV的底部约束挂接为IBOutlet并执行此操作
@objc func handleKeyboardDidShow (notification: NSNotification)
{
let keyboardRectAsObject = notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
var keyboardRect = CGRect.zero
keyboardRectAsObject.getValue(&keyboardRect)
self.categUIVBotcon.constant = -1 * keyboardRect.height
UIView.animate(withDuration: 0.5,animations: {
self.view.layoutIfNeeded()
})
}
@objc func handleKeyboardWillHide(notification: NSNotification)
{
self.categUIVBotcon.constant = 0
UIView.animate(withDuration: 0.5,animations: {
self.view.layoutIfNeeded()
})
}
要实现此功能,您需要将视图放到滚动视图中,以便层次结构如下所示:
- View Controller
- View
- Scroll View
- Content View
- categUIV
您还必须将滚动视图和categUIV连接到您的代码:
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var categUIV: UIView!
然后,您可以注册键盘观察员:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
具有以下功能:
func keyboardWasShown(notification: NSNotification) {
self.keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
self.scrollView.isScrollEnabled = true
let contentInsets = UIEdgeInsetsMake(0.0, 0.0, self.keyboardSize.height, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
var rect = self.view.frame
rect.size.height -= self.keyboardSize.height
if !rect.contains(self.categUIV.frame.origin) {
self.scrollView.scrollRectToVisible(self.categUIV.frame, animated: true)
}
}
func keyboardWillBeHidden(notification: NSNotification) {
let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
let contentInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize.height, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
self.scrollView.isScrollEnabled = false
}
要实现此功能,您需要将视图放到滚动视图中,以便层次结构如下所示:
- View Controller
- View
- Scroll View
- Content View
- categUIV
您还必须将滚动视图和categUIV连接到您的代码:
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var categUIV: UIView!
然后,您可以注册键盘观察员:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
具有以下功能:
func keyboardWasShown(notification: NSNotification) {
self.keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
self.scrollView.isScrollEnabled = true
let contentInsets = UIEdgeInsetsMake(0.0, 0.0, self.keyboardSize.height, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
var rect = self.view.frame
rect.size.height -= self.keyboardSize.height
if !rect.contains(self.categUIV.frame.origin) {
self.scrollView.scrollRectToVisible(self.categUIV.frame, animated: true)
}
}
func keyboardWillBeHidden(notification: NSNotification) {
let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
let contentInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize.height, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
self.scrollView.isScrollEnabled = false
}
在UIview中没有成员常量,我在这一行self.categUIV.constantnot中有一个错误,在故事板中,您为视图设置了一个底部约束,您希望它向上选择它并在swift文件中拖动,因为IBMOutlet此约束具有常量属性,即它与其他视图之间的距离是您的权利,但是现在视图变为左视图而不是向上视图确保它是底部约束而不是引导谢谢,它的工作方式与我所希望的一样我只是修改了self.categUIVBotcon.constant=-1*keyboardRect.height/2,因为视图向上视图在UIview中没有成员常量,我在这行self.categUIV.constantnot上有一个错误,在视图中,在故事板中,您为视图设置了一个底部约束,您希望它向上选择它并在swift文件中拖动,因为IBM此约束具有常量属性,即它与其他视图之间的距离是您的权利,但是现在视图变为左视图而不是向上视图确保它是底部约束而不是引导谢谢,它的工作方式就像我想要的一样我只是修改了self.categUIVBotcon.constant=-1*keyboardRect.height/2,因为视图要高很多