我使用How to move content of UIViewController upwards as Keypad appears using Swift中的代码试图阻止键盘覆盖我的视图。
不知怎的,我无法让它工作(主要是因为我的n00b编程“技能”)
这是我得到的错误:[NSObject: AnyObject] does not have a member named 'valueForKey'
在这行代码上:let s:NSValue = sender.userInfo.valueForKey(UIKeyboardFrameBeginUserInfoKey) as NSValue;
我想我必须创建某种类型的IBAction,但我尝试的任何工作都没有。我需要做些什么来使代码工作?为什么我必须这样做? NSObject做了什么?
这是代码:
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);
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self);
}
func keyboardWillShow(sender: NSNotification) {
let s:NSValue = sender.userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as NSValue;
let rect :CGRect = s.CGRectValue();
var frame = self.textField.frame;
frame.origin.y = frame.origin.y - rect.height;
self.textField.frame = frame;
}
func keyboardWillHide(sender: NSNotification) {
let s:NSValue = sender.userInfo.valueForKey(UIKeyboardFrameBeginUserInfoKey) as NSValue;
let rect :CGRect = s.CGRectValue();
var frame = self.textField.frame;
frame.origin.y = frame.origin.y + rect.height;
self.textField.frame = frame;
}
答案 0 :(得分:3)
您必须将userInfo转换为NSDictionary
将以上功能替换为以下功能
func keyboardWillShow(sender: NSNotification) {
let dict:NSDictionary = sender.userInfo as NSDictionary
let s:NSValue = dict.valueForKey(UIKeyboardFrameEndUserInfoKey) as NSValue;
let rect :CGRect = s.CGRectValue();
var frame = self.textField.frame;
frame.origin.y = frame.origin.y - rect.height;
self.textField.frame = frame;
}
func keyboardWillHide(sender: NSNotification) {
let dict:NSDictionary = sender.userInfo as NSDictionary
let s:NSValue = dict.valueForKey(UIKeyboardFrameBeginUserInfoKey) as NSValue;
let rect :CGRect = s.CGRectValue();
var frame = self.textField.frame;
frame.origin.y = frame.origin.y + rect.height;
self.textField.frame = frame;
}
修改强>
//Add this above viewDidLoad
var selecteTextFieldOriginalY:CGFloat = 0.0;
//write in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardDidShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardDidHideNotification, object: nil);
}
//Replace these functions
func keyboardWillShow(sender: NSNotification) {
let dict:NSDictionary = sender.userInfo as NSDictionary
let s:NSValue = dict.valueForKey(UIKeyboardFrameEndUserInfoKey) as NSValue;
let rect :CGRect = s.CGRectValue();
var frame = self.textField.frame;
selecteTextFieldOriginalY = frame.origin.y;
//Adjust 80 according to your need actually if is for padding and quickTypeView
var offset = (rect.height - ((self.view.frame.height - self.textField.frame.origin.y)+self.textField.frame.size.height))+80;
print(offset)
frame.origin.y = offset>0 ? frame.origin.y - offset : frame.origin.y ;
UIView.animateWithDuration(0.3, animations:{
self.textField.frame = frame;
}
)
}
func keyboardWillHide(sender: NSNotification) {
let dict:NSDictionary = sender.userInfo as NSDictionary
let s:NSValue = dict.valueForKey(UIKeyboardFrameBeginUserInfoKey) as NSValue;
let rect :CGRect = s.CGRectValue();
var frame = self.textField.frame;
frame.origin.y = selecteTextFieldOriginalY ;
UIView.animateWithDuration(0.3, animations:{
self.textField.frame = frame;
})
}