[英]How should I correctly set contentInset for UITextView when Keyboard appears
I am attempting to change the contentInsets
property of a UITextView
upon they keyboard showing, so that it is not hidden behind the keyboard. 我试图在显示键盘时更改
UITextView
的contentInsets
属性,以使其不会隐藏在键盘后面。 I have looked at a number of questions concerning this task, and have tried following the approved or highly voted answers, and have tried following Apple's recommended approach: 我查看了有关此任务的许多问题,并尝试遵循已批准或投票表决很高的答案,并尝试遵循Apple推荐的方法:
https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW7 https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW7
My code is currently as follows, in ViewDidLoad(): 我的代码目前在ViewDidLoad()中如下所示:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
the function that is indicated by the selector is: 选择器指示的功能是:
func keyboardWillShow(notification : NSNotification){
let info :NSDictionary = notification.userInfo! as NSDictionary;
if let keyboardRect = info.objectForKey(UIKeyboardFrameEndUserInfoKey){
var inset = commentsText.contentInset;
inset.bottom = keyboardRect.size.height
UIView.animateWithDuration(0.25, animations: {
self.commentsText.contentInset = inset
})
}
}
commentsText
is the name of the UITextView
I am trying to adjust the innocence of. commentsText
是我要调整其纯真性的UITextView
的名称。
The problem I am having is that when attempting to access keyboardRect.size.heigh
I have a fatal error of attempting to unwrap an unexpected nil value. 我遇到的问题是,当尝试访问
keyboardRect.size.heigh
我遇到致命错误,即试图解开意外的nil值。 I really am not sure how to fix this, because as far as I can tell I am following accepted answers and documentation (The animation block is an addition to Apple's documentation, but it is from an accepted answer, How do I resize views when keyboard pops up using Auto Layout , so I figure it probably isn't the source of the problem. 我真的不确定如何解决此问题,因为据我所知,我正在遵循公认的答案和说明文件(动画块是Apple文档的补充,但这是来自公认的答案,“ 如何在键盘操作时调整视图大小)使用“自动布局”弹出 ,所以我认为这可能不是问题的根源。
I have also attempted using the UIKeyboardDidShowNotification
as well as using the UIKeyboardFrameBeginUserInfoKey
, but neither of those substitutions changed the error. 我也尝试过使用
UIKeyboardDidShowNotification
以及UIKeyboardFrameBeginUserInfoKey
,但是这些替换都没有改变错误。 The documentation from Apple and most questions/answers on this topic are in Objective-C, and while I think I correctly "translated" them into Swift for my purposes, it is entirely possible I made a mistake somewhere, if this is the case please let me know what my error was. Apple的文档以及有关此主题的大多数问题/答案都在Objective-C中,尽管我认为出于我的目的将它们正确地“翻译”成了Swift,但如果是这种情况,我很可能在某个地方犯了一个错误。让我知道我的错误是什么。
Any help/suggestions would be appreciated. 任何帮助/建议,将不胜感激。
Thanks in advance. 提前致谢。
PS I am currently working in Xcode 7 Beta 6 PS我目前在Xcode 7 Beta 6中工作
Here's how to get the keyboard rect (a CGRect) out of the NSNotification: 这是从NSNotification中获取键盘rect(CGRect)的方法:
let info = notification.userInfo!
let keyboardRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.