我一直在尝试添加一些代码来在键盘出现时移动我的视图,但是,我在尝试将Objective-C示例转换为Swift时遇到了问题.我取得了一些进展,但我被困在一条特定的路线上.
这些是我一直关注的两个教程/问题:
当Keypad使用Swift出现时,如何向上移动UIViewController的内容 http://www.ioscreator.com/tutorials/move-view-when-keyboard-appears
这是我目前的代码:
override func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func keyboardWillShow(notification: NSNotification) {
var keyboardSize = notification.userInfo(valueForKey(UIKeyboardFrameBeginUserInfoKey))
UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
let frame = self.budgetEntryView.frame
frame.origin.y = frame.origin.y - keyboardSize
self.budgetEntryView.frame = frame
}
func keyboardWillHide(notification: NSNotification) {
//
}
Run Code Online (Sandbox Code Playgroud)
目前,我在这一行收到错误:
var keyboardSize = notification.userInfo(valueForKey(UIKeyboardFrameBeginUserInfoKey))
Run Code Online (Sandbox Code Playgroud)
如果有人能让我知道这行代码应该是什么,我应该设法自己弄清楚其余部分.
在以下情况下如何隐藏keyboard使用SwiftUI?
情况1
我有TextField,我需要keyboard在用户单击return按钮时隐藏。
情况二
我有TextField,keyboard当用户在外面轻按时,我需要隐藏。
我该如何使用SwiftUI呢?
注意:
我尚未提出有关的问题UITextField。我想使用SwifUI(TextField)来做。
我试着做一个简单的敲击手势,我无法弄明白.我想为手势添加一个目标,简单的选择器.
这是我的代码:
var panGesture : UIGestureRecognizer = UITapGestureRecognizer.addTarget(<#UIGestureRecognizer#>)
Run Code Online (Sandbox Code Playgroud)
我该如何设置选择器?