我正在开发一个聊天应用程序。该应用程序运行良好,但我在 UI 设计中遇到了一些问题。
我在 UIview 中有 UiTextField,每当用户想要输入一条消息时,键盘就会打开,UIView 会向上移动。
然后当用户按下返回按钮时,我想把它带回原来的位置。
我试过了,但它看起来像这样
我希望它支持所有设备。
这是我的代码
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
[self.view endEditing:YES];
return YES;
}
- (void)keyboardDidShow:(NSNotification *)notification
{
// Assign new frame to your view
[self.view setFrame:CGRectMake(0,-145,320,460)]; //here taken -110 for example i.e. your view will be scrolled to -110. change its value according to your requirement.
}
-(void)keyboardDidHide:(NSNotification *)notification
{
[self.view setFrame:CGRectMake(0,0,320,460)];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.tv_Message resignFirstResponder];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"selected index : %ld", (long)indexPath.row);
}
- (BOOL)textFieldShouldReturn:(UITextField *)aTextField {
[aTextField resignFirstResponder];
[self.view setFrame:CGRectMake(0,0,320,460)];
return YES;
}
谁能帮我解决这个问题。
最佳答案
您可以使用此链接根据设备管理您的 View 。您还需要相应地设置约束。
关于ios - 键盘打开时更改 View 位置 - objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41712427/