简体   繁体   English

出现键盘时,如何正确设置UITextView的contentInset

[英]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. 我试图在显示键盘时更改UITextViewcontentInsets属性,以使其不会隐藏在键盘后面。 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.

相关问题 键盘在出现时会隐藏UITextView - Keyboard hides UITextView when it appears 键盘出现时调整UITextView的大小 - Resize the UITextView when keyboard appears 出现键盘时调整 UITextView 的大小 - Resize UITextView when keyboard appears 创建UITextView时,如何在Live View中显示的Swift Playgrounds(在Xcode上)的UI中隐藏键盘? - How can I hide the keyboard in the UI in Swift Playgrounds (on Xcode) that appears in the Live View when I create a UITextView? iPad上的UIModalPresentationFormSheet。 键盘出现时如何调整UITextView高度 - UIModalPresentationFormSheet on iPad. How to adjust UITextView height when keyboard appears 当用户单击UITextView并出现键盘时,如何使背景变暗/着色? - How to darken/tint background when user clicks UITextView and keyboard appears? 迅速出现键盘时滚动UITextView - Scroll UITextView when keyboard appears in swift UITextView ContentInset - UITextView ContentInset 如何在scrollViewDidZoom中动态调整contentInset?我最初在UIScrollview中设置了UIImageView的contentInset - How can I dynamically adjust contentInset in scrollViewDidZoom? I initially set contentInset of UIImageView in UIScrollview 当键盘显示UITextView而不是同一视图上的UITextField时,滚动视图 - Scroll view when keyboard appears for UITextView, but not for the UITextField that's on the same view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM