ios - 显示键盘时隐藏 UITextField

标签 ios objective-c iphone

在我的应用程序中,我有这个登录屏幕:

enter image description here

在第一个测试字段上方我有一个图像,Interface Builder 中的配置如下:

enter image description here

现在,当我点击 UITextField 时,它们应该向上移动,否则在 iPhone 4/4s 中,该字段将被键盘覆盖。 现在我使用以下代码:

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.35f];
    CGRect frameView = self.view.frame;
    CGRect frameImage = self.imageViewLogo.frame;
    CGRect frameTextFieldUsername = self.textFieldUsername.frame;
    CGRect frameTextFieldPassword = self.textFieldPassword.frame;
    CGRect frameButtonLogin = self.buttonLogin.frame;
    frameView.origin.y = -100;
    frameImage.origin.y = -100;
    frameTextFieldUsername.origin.y = -100;
    frameTextFieldPassword.origin.y = -100;
    frameButtonLogin.origin.y = -100;
    [self.view setFrame:frameView];
    [self.imageViewLogo setFrame:frameImage];
    [self.textFieldUsername setFrame:frameTextFieldUsername];
    [self.textFieldPassword setFrame:frameTextFieldPassword];
    [self.buttonLogin setFrame:frameButtonLogin];
    [UIView commitAnimations];
}

当我尝试在模拟器或真实设备上运行该应用程序时, View 会向上滚动 100,但图像、文本字段和按钮不会向上滚动...我认为问题取决于约束,你能帮我解决这个问题吗? 谢谢

最佳答案

您尝试执行此操作的方式会给您带来麻烦。当您给出框架的硬编码值时。

尝试使用keyboard avoiding library这是更好、更安全的方法。

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

相关文章:

iphone - iOS 开发 : How can I force a UIWebView to load the non-mobile version of Facebook?

ios - 重置所有配置文件

iphone - JSON 框架没有隐藏的 API?

iOS 8 [UIApplication sharedApplication].scheduledLocalNotifications 为空

ios - SceneKit - 如何在另一个节点内反弹一个节点(鱼缸里的鱼)

iphone - 如何读取 IBAction 中的 UITextField 值。我正在以编程方式创建 UITextField

iphone - 使用 Objective-C 分割输入字符串

iphone - 如何阅读 iOS 4 崩溃日志?

iOS 6 + phonegap -- 启动画面在没有交互的情况下旋转

iphone - 在没有 Webview 的情况下从 URL 接收 Passbook 的 .pkpass