gpt4 book ai didi

ios - 向上推软键盘时如何改变UIScrollView内容的高度

转载 作者:行者123 更新时间:2023-11-29 12:01:33 25 4
gpt4 key购买 nike

我有一个带有 TextView 和按钮的 View ,就像在任何典型的消息传递应用程序中一样。当出现键盘时,textview 和按钮也被向上推,但内容被向上推离屏幕,我看不到它的顶端,但是当我用力向下滚动时,我看到它向下爬行并向上弹起当我放手时。我已经用草图说明了我遇到的问题,第三个屏幕是所需的行为。

我在 .xib 文件中有这个 View 。我有一个 View -> ScrollView -> contentView(它有一个表格、textview 和按钮)

enter image description here

我注册了键盘通知,这里是显示/隐藏方法的代码

- (void)registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification
object:nil];

}

- (void)keyboardWasShown:(NSNotification *) aNotification {
NSDictionary *info = [aNotification userInfo];

CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

// the hardcoded 49 is the height of the uitabbar
UIEdgeInsets contentInsets = UIEdgeInsetsMake(-keyboardSize.height+49, 0.0, keyboardSize.height-49, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;

[self.view addGestureRecognizer:self.tapRecognizer];

}


- (void)keyboardWillBeHidden:(NSNotification *) aNotification {
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;

[self.view removeGestureRecognizer:self.tapRecognizer];
}

最佳答案

我在 keyboardWasShown 方法中看到了这一行

UIEdgeInsets contentInsets = UIEdgeInsetsMake(-keyboardSize.height+49, 0.0, keyboardSize.height-49, 0.0);

顶部边缘插入的位置是 -ve,因为 49 - keyboardSize(约为 256)。将其替换为:

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.height-49), 0.0);

玩最适合您的计算。希望这项工作:)

关于ios - 向上推软键盘时如何改变UIScrollView内容的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36805503/

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