gpt4 book ai didi

ios - 出现键盘时调整 UITextView 的大小

转载 作者:可可西里 更新时间:2023-11-01 03:36:41 25 4
gpt4 key购买 nike

我想在键盘出现时调整 TextView 的大小。我的代码如下。我启用了自动布局,因此使用来自 superview 的 textView->bottom 空间的约束并通过 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;
}

它适用于所有设备;)

关于ios - 出现键盘时调整 UITextView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27992591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com