Oka*_*kan 17 ios autolayout swift
我正在构建一个聊天应用程序.键盘出现时我必须移动文本字段.我使用以下代码执行此操作:
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
kbHeight = keyboardSize.height
self.animateTextField(true)
}
}
}
func keyboardWillHide(notification: NSNotification) {
self.animateTextField(false)
}
func animateTextField(up: Bool) {
var movement = (up ? -kbHeight : kbHeight)
UIView.animateWithDuration(0.3, animations: {
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
})
}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用此代码时,第一条消息不显示.我想我必须调整tableview的大小.
以下是键盘出现之前和之后的屏幕截图:
http://imgim.com/iossimulatorscreenshot7haz2015114958.png http://imgim.com/iossimulatorscreenshot7haz2015115000.png
我正在使用自动布局.
我该如何解决这个问题?
Een*_*dje 15
您可以创建表视图底部自动布局约束的出口.
然后只需使用此代码:
func keyboardWillShow(sender: NSNotification) {
let info = sender.userInfo!
var keyboardSize = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height
bottomConstraint.constant = keyboardSize - bottomLayoutGuide.length
let duration: TimeInterval = (info[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
UIView.animate(withDuration: duration) { self.view.layoutIfNeeded() }
}
func keyboardWillHide(sender: NSNotification) {
let info = sender.userInfo!
let duration: TimeInterval = (info[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
bottomConstraint.constant = 0
UIView.animate(withDuration: duration) { self.view.layoutIfNeeded() }
}
Run Code Online (Sandbox Code Playgroud)
如果您在创建底部约束时遇到问题:
在故事板中
|-[]-|.现在,您可以将其拖动到视图控制器并将其添加为插座.
另一种解决方案是设置tableView.contentInset.bottom.但我以前没有那样做过.如果您愿意,我可以尝试解释一下.
使用插入:
func keyboardWillShow(sender: NSNotification) {
let info = sender.userInfo!
let keyboardSize = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height
tableView.contentInset.bottom = keyboardSize
}
func keyboardWillHide(sender: NSNotification) {
tableView.contentInset.bottom = 0
}
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用此代码来设置插图.我自己还没试过,但应该是这样的.
编辑:根据nacho4d的建议更改持续时间
Fat*_*tie 11
基于Eendje的概念.........
1)将KUIViewController复制并粘贴到您的项目中,
2)创建约束 - 它只是您内容的"底部约束"
3)不要忘记简单地将约束id拖动到 bottomConstraintForKeyboard
那么"你应该调整哪种观点?"
注意,您无法使用.view- 视图控制器的主视图 - 遗憾的是,您无法.view在iOS中调整大小!
所以,简单地创建一个名为"holder"的UIView.
它坐在里面.view.
把你的一切都放在持有人手中.
持有者当然会有四个简单的约束上/下/左/右.view.
对于"持有者"的底部约束确实bottomConstraintForKeyboard在这里的类中.
(当然,通常情况下,当皮艇抵达时,你想要"一切"压扁.在不寻常的情况下,你可能更喜欢让其他特定的东西向上移动,而剩下的就是其他东西.如果你想这样做,你就有了使用该KUIViewController方法时的灵活性.您可以将约束附加到您想要的任何内容.当然,几乎总是它只是"整个屏幕" - 因此只需要一个包含所有内容的"持有者",并附加到那个上.)
通常在现代用户体验中,如果单击"背景"(所以:远离任何按钮,输入字段等),它应该关闭键盘.
该bottomConstraintForKeyboard课程完全自动化了这一需求.你什么都不做,这是自动的.
class KUIViewController: UIViewController {
// KBaseVC is the KEYBOARD variant BaseVC. more on this later
@IBOutlet var bottomConstraintForKeyboard: NSLayoutConstraint!
@objc func keyboardWillShow(sender: NSNotification) {
let i = sender.userInfo!
let k = (i[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height
bottomConstraintForKeyboard.constant = k - bottomLayoutGuide.length
let s: TimeInterval = (i[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
UIView.animate(withDuration: s) { self.view.layoutIfNeeded() }
}
@objc func keyboardWillHide(sender: NSNotification) {
let info = sender.userInfo!
let s: TimeInterval = (info[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
bottomConstraintForKeyboard.constant = 0
UIView.animate(withDuration: s) { self.view.layoutIfNeeded() }
}
@objc func clearKeyboard() {
view.endEditing(true)
}
func keyboardNotifications() {
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillShow),
name: UIResponder.keyboardWillShowNotification,
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardWillHide),
name: UIResponder.keyboardWillHideNotification,
object: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
keyboardNotifications()
let t = UITapGestureRecognizer(target: self, action: #selector(clearKeyboard))
view.addGestureRecognizer(t)
t.cancelsTouchesInView = false
}
}
Run Code Online (Sandbox Code Playgroud)
您可以
class AddCustomer: KUIViewController, SomeProtocol {
class CustomerPicker: KUIViewController {
Run Code Online (Sandbox Code Playgroud)
在那些屏幕上:
.view当键盘在那里时,页面(您的.视图)将自动调整大小以允许键盘.它会很好地动画.
当用户点击"关闭"(点击"背景")时,它将关闭键盘.
细节 - (不幸的是)点击您的内容也会关闭键盘.(他们都得到了这个事件.)然而:确实,这通常是苹果风格的正确行为:试一试.没有简单的方法可以避免这种情况.请务必阅读@iPruch的答案.
| 归档时间: |
|
| 查看次数: |
11640 次 |
| 最近记录: |