gpt4 book ai didi

ios - 隐藏键盘时Blackview来了

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

这是我为在用户开始在 TextView 中输入时移动 TextView 而编写的一些代码。

这是代码,

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
if(textView == _textViewMsg || textView == _subjectView ){
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
// return YES;
}
return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
if(textView == _textViewMsg || textView == _subjectView ){
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

[self.view endEditing:YES];
}
return YES;
}


- (void)keyboardDidShow:(NSNotification *)notification
{

[self.view setFrame:CGRectMake(0,-50,320,460)];
}

-(void)keyboardDidHide:(NSNotification *)notification
{
[self.view setFrame:CGRectMake(0,60,320,520)];
}

enter image description here

当我在移动中单击 TextView (在主题中) View 时,我可以在 TextView 中键入内容。它工作正常。 (图 1)

当我点击完成按钮时,同时隐藏,即在键盘隐藏时,一个黑色 View 出现(检查图像 2)几秒钟,然后再次出现正常 View 。

编辑:

解决方案:@michael 刚刚给我解决方案

UIKeyboardDidHideNotification 更改为 UIKeyboardWillHideNotification 它对我有用。

出现新问题:当我开始输入前 2 个 TextView 时,即请求者、名字和姓氏......我无法输入它,因为它正在向上移动。

最佳答案

这是因为当系统动画完成时,您在键盘消失后更改了 View 的框架。快速修复:将 UIKeyboardDidHideNotification 更改为 UIKeyboardWillHideNotification

但如果您不介意的话,我想指出您代码中的其他几个项目。

1:每次用户开始编辑时,您都在订阅通知。这意味着当用户第二次开始输入时,您的代码将被触发两次。在 viewDidAppear 上订阅通知和在 viewWillDisappear 上取消订阅更合适:

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear: animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardDidHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

2:然后您将框架更改为恒定值。但现在苹果支持不同尺寸的不同键盘,更不用说键盘尺寸在不同设备和不同可能模式下有所不同。因此,从通知中获取键盘的大小更为明智:

- (void)keyboardDidShow:(NSNotification *)aNotification
{
CGRect keyboardFrame = [[aNotification.userInfo valueForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect overlap = CGRectIntersection( self.view.frame, keyboardFrame);
// Setup new frmae according to overlap, for example reduce size by half of overlap, and move up by another half
CGRect newFrame = self.view.frame;
newFrame.origin.y -= overlap.size.height / 2.0
newFrame.size.height -= overlap.size.height / 2.0
self.view.frame = newFrame;
}

3:动画:您也可以从通知中获取键盘动画持续时间:UIKeyboardAnimationDurationUserInfoKey:

NSTimeInterval duration = [[aNotification.userInfo  valueForKey: UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:duration animations:^{
self.view.frame = newFrame;
}];

4由于您似乎在使用 ScrollView (UITableView 和 UICollectioView 是 ScrollView ),因此您可以不更改框架,而是更改内容插入(向底部 ScrollView 添加空白区域)

[self.view setContentInset:UIEdgeInsetsMake(0, 0, overlap.size.height, 0)];

5考虑使用自动布局。

关于ios - 隐藏键盘时Blackview来了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48182614/

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