gpt4 book ai didi

ios - 如何根据键盘管理 ScrollView (向上和向下)在 Objective-C 中使用 NSNotificationCenter 在屏幕中显示和隐藏

转载 作者:行者123 更新时间:2023-11-29 11:49:53 24 4
gpt4 key购买 nike

考虑我有一个屏幕,在这个屏幕上我有 10 个文本字段,现在我的问题是当键盘打开时键盘在我的 3 个文本字段上。那么我如何使用 UIScrollView 和 NSNotification Center 来管理它呢?

最佳答案

是的,我有一个代码,我相信它绝对适合你。

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

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
MyScrollView.contentInset = contentInsets;
MyScrollView.scrollIndicatorInsets = contentInsets;
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
MyScrollView.contentInset = contentInsets;
MyScrollView.scrollIndicatorInsets = contentInsets;
}

关于ios - 如何根据键盘管理 ScrollView (向上和向下)在 Objective-C 中使用 NSNotificationCenter 在屏幕中显示和隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41604713/

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