[英]Resize the UITextView when keyboard appears
我想在鍵盤出現時調整文本視圖的大小。 我的代碼如下。 我有自動布局,因此使用了來自superview的textView-> bottom space的約束,並通過IBOutlet distanceFromBottom引用它。
- (void)keyboardWillShow:(NSNotification *)notification
{
[UIView animateWithDuration:0.3 animations:^{
NSDictionary* d = [notification userInfo];
CGRect r = [d[UIKeyboardFrameEndUserInfoKey] CGRectValue];
r = [textView convertRect:r fromView:Nil];
if(IS_IPHONE_6||IS_IPHONE_6P)
distanceFromBottom.constant = r.origin.y+78;
else if(IS_IPHONE_5)
distanceFromBottom.constant = r.origin.y+183;
}];
}
上面的代碼非常完美。 我不明白為什么我需要為iPhone6添加+78或為iPhone5添加183。 這兩個值我帶來了反復試驗。 如果我不添加這些,textView擴展到鍵盤下方。 請幫我解開這個謎。
在viewWillAppear
方法中,添加以下內容:
- (void) viewWillAppear:(BOOL)paramAnimated{
[super viewWillAppear:paramAnimated];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(handleKeyboardDidShow:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(handleKeyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
}
然后實現通知中心的兩種方法,如下所示:
- (void) handleKeyboardDidShow:(NSNotification *)paramNotification{
NSValue *keyboardRectAsObject =
[[paramNotification userInfo]
objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = CGRectZero;
[keyboardRectAsObject getValue:&keyboardRect];
yourTextView.contentInset =
UIEdgeInsetsMake(0.0f,
0.0f,
keyboardRect.size.height,
0.0f);
}
而另一個像:
- (void) handleKeyboardWillHide:(NSNotification *)paramNotification{
yourTextView.contentInset = UIEdgeInsetsZero;
}
它適用於所有設備;)
Swift /修改版
使用上面的內容,我做了一些調整,以便在鍵盤顯示和隱藏時使用NSLayoutConstraint
更改高度constant
屬性。 如果您旋轉設備,這也適用。
1.設置TextView約束
然后控制從您的高度約束拖出一個出口到該類。
2.添加以下內容
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(SkillDescriptionViewController.keyboardWillShowHandle(_:)), name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(SkillDescriptionViewController.keyboardWillHideHandle), name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWillShowHandle(note:NSNotification) {
guard let keyboardRect = note.userInfo![UIKeyboardFrameEndUserInfoKey] as? NSValue else { return }
let kbFrame = keyboardRect.CGRectValue()
tvHeight.constant = -kbFrame.height
view.layoutIfNeeded()
}
func keyboardWillHideHandle() {
tvHeight.constant = 0
view.layoutIfNeeded()
}
基於以上答案的Swift 5解決方案
Notification API已更改(20190707)以及如何使用#selector
當鍵盤pop覆蓋UITextView導致用戶無法看到已編輯的文本時,可以使用此處的解決方案。
import UIKit
class DCNodeLogEntryViewController: UIViewController {
@IBOutlet weak var textViewNodeLogEntry: UITextView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(
self,
selector: #selector(DCNodeLogEntryViewController.handleKeyboardDidShow(notification:)),
name: UIResponder.keyboardDidShowNotification,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(DCNodeLogEntryViewController.handleKeybolardWillHide),
name: UIResponder.keyboardWillHideNotification,
object: nil
)
}
@objc func handleKeyboardDidShow(notification: NSNotification) {
guard let keyboardRect = notification
.userInfo![UIResponder.keyboardFrameEndUserInfoKey]
as? NSValue else { return }
let frameKeyboard = keyboardRect.cgRectValue
textViewNodeLogEntry.contentInset = UIEdgeInsets(
top: 0.0,
left: 0.0,
bottom: frameKeyboard.size.height,
right: 0.0
)
view.layoutIfNeeded()
}
@objc func handleKeybolardWillHide() {
textViewNodeLogEntry.contentInset = .zero
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.