gpt4 book ai didi

objective-c - 当键盘出现在 iOS 中时,将 UIView 上移

转载 作者:IT老高 更新时间:2023-10-28 11:26:06 26 4
gpt4 key购买 nike

我有一个 UIView,它不在 UIScrollView 里面。当键盘出现时,我想上移我的 View 。在我尝试使用此解决方案之前:How can I make a UITextField move up when the keyboard is present? .

工作正常。但是在文本字段中插入数据后,它会将我带到另一个 View ,当我回到这个页面时,它只是在跳跃,我看不到我的文本字段。有没有更好的办法解决这个问题?

最佳答案

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark - keyboard movements
- (void)keyboardWillShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = -keyboardSize.height;
self.view.frame = f;
}];
}

-(void)keyboardWillHide:(NSNotification *)notification
{
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = 0.0f;
self.view.frame = f;
}];
}

关于objective-c - 当键盘出现在 iOS 中时,将 UIView 上移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11282449/

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