objective-c - 键盘遮挡了输入字段

标签 objective-c ios three20

当我想将文本放入我的 UITextField 时,键盘会显示在它的顶部,并覆盖该字段。如何防止这种情况?我刚读过:this solution但我不知道在哪里调整大小?这是个好办法吗?

我的代码是:

UITextField * passwordTextField = [[[UITextField alloc] init] autorelease];
passwordTextField.keyboardType = UIKeyboardTypeDefault;
passwordTextField.delegate = self;   
passwordTextField.placeholder = NSLocalizedString(@"somestr", @"");   
passwordTextField.secureTextEntry = YES;
[_controls setObject:passwordTextField forKey:keyPassword]; 

然后我通过以下方式在 Three20 中展示它:

...@"",
 [NSArray arrayWithObjects:
  [TTTableControlItem itemWithCaption:nil control:passwordTextField],          
  nil],...

最佳答案

您只需实现 UITextField textFieldDidBeginEditing 和 textFieldDidEndEditing 的委托(delegate)。在调用 textFieldDidBeginEditing 时将父 View 的框架向上移动某个值,并在调用委托(delegate)方法 textFieldDidEndEditing 时将其恢复为相同的值。

另一种方法是为 UIKeyboardDidShowNotification 添加一个观察者,并在 UIKeyboardDidHideNotification 触发时向上移动框架和向下移动框架。

示例:-

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:)name:UIKeyboardDidShowNotification object:nil];   

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil];

并更改选择器中的框架:-

- (void)keyboardDidShow:(NSNotification *)aNotification 
{     
if ( keyBDidShow)         
return;    
 NSTimeInterval animationDuration = 0.300000011920929;         
CGRect frame = self.view.frame;         
frame.origin.y -= 60;                
[UIView beginAnimations:@"Rollback" context:nil];         
[UIView setAnimationDuration:animationDuration];         
self.view.frame = frame;         
[UIView commitAnimations];  
viewUp= YES;             
}      
keyBDidShow= YES; 
}  
- (void)keyboardWasHidden:(NSNotification *)aNotification 
{     
if ( viewUp) 
{                  
NSTimeInterval animationDuration = 0.300000011920929;         
CGRect frame = self.view.frame;         
frame.origin.y += 60;                 
[UIView beginAnimations:@"Rollback" context:nil];         
[UIView setAnimationDuration:animationDuration];         
self.view.frame = frame;        
[UIView commitAnimations];          
viewUp = NO;     
}      
keyBDidShow= NO; 
} 

同样可以为文本字段代理完成。

关于objective-c - 键盘遮挡了输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10636046/

相关文章:

ios - 适用于 iOS arm64 架构的支持 OpenSSL FIPS 的库

iphone - plist 到数组 "Collection was mutated while being enumerated"异常

iphone - 如何将字符串赋值给宏?

iphone - 在模拟器上流式传输视频

ios - 如何快速设置 CAShapeLayer 的 strokeColor

ios - UIBarButton 不执行操作

ios - 检测图像 ios 上的黑线

iphone - 让 TTPickerTextField 工作?

objective-c - 集成 Three20 库后 libxml/tree.h 没有该文件或目录

iphone - 静态库依赖项在 Xcode 3.2.3 上的模拟器中编译,在设备上失败