ios - 键盘打开时更改 View 位置 - objective-c

标签 ios objective-c uiview

我正在开发一个聊天应用程序。该应用程序运行良好,但我在 UI 设计中遇到了一些问题。

我在 UIview 中有 UiTextField,每当用户想要输入一条消息时,键盘就会打开,UIView 会向上移动。

然后当用户按下返回按钮时,我想把它带回原来的位置。

我试过了,但它看起来像这样

我希望它支持所有设备。

enter image description here

这是我的代码

-(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 。您还需要相应地设置约束。

https://stackoverflow.com/a/11282535/6438500

关于ios - 键盘打开时更改 View 位置 - objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41712427/

相关文章:

ios - '在 swift 3 的 'try' 表达式 [警告] 中没有调用抛出函数

ios - 选中单元格时隐藏圆形uiview

iphone - UIMenuController和响应者链: what's going on?

ios - iPad 应用程序左右两侧的空格

ios - 将单元格信息从一个 View Controller 发送到另一个 Swift 和 Parse

ios - 如何检查 iOS 中的 "Use Mobile Data"设置?

ios - 影响瓷砖 map 中特定瓷砖的正确有效方法

objective-c - Apple Mach-O 框架错误

objective-c - 创建全向 ScrollView

iphone - Objective-C - for 循环中的未识别错​​误