我在子视图中有两个文本域作为viewLogin。当试图输入文本时,键盘正在隐藏它们。我通过移动viewLogin解决了这个问题。 代码是......
- (void)textFieldDidBeginEditing:(UITextField *)textField {
UIDeviceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
viewLogin.frame = CGRectMake((1024-viewLogin.frame.size.width)/2, (768-viewLogin.frame.size.height)/9, viewLogin.frame.size.width, viewLogin.frame.size.height);
}
else {
viewLogin.frame = CGRectMake((768-viewLogin.frame.size.width)/2, (1024-viewLogin.frame.size.height)/2, viewLogin.frame.size.width, viewLogin.frame.size.height);
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
UIDeviceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
viewLogin.frame = CGRectMake((1024-viewLogin.frame.size.width)/2, (768-viewLogin.frame.size.height)/2.5, viewLogin.frame.size.width, viewLogin.frame.size.height);
}
else {
viewLogin.frame = CGRectMake((768-viewLogin.frame.size.width)/2, (1024-viewLogin.frame.size.height)/2, viewLogin.frame.size.width, viewLogin.frame.size.height);
}
}
我面临的问题是,如果我首先将方向更改为横向并单击textField它的工作正常但是如果我首先在纵向模式下单击textField然后更改为横向左/右它不起作用..
可以帮助我解决这个问题....
提前致谢
答案 0 :(得分:1)
这是Apple使用的(看看KeyboardAccessory example):首先,在viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
并确保您在viewDidUnload
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
然后将这些方法添加到您的实现中:
- (void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
NSValue *keyboardEndFrameValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [keyboardEndFrameValue CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
CGFloat keyboardTop = keyboardRect.origin.y;
CGRect newTextViewFrame = self.view.bounds;
newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[UIView animateWithDuration:animationDuration
animations:^{
self.textView.frame = newTextViewFrame;
}];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[UIView animateWithDuration:animationDuration
animations:^{
self.textView.frame = self.view.bounds;
}];
}
我正在使用它,我对方向更改没有任何问题。
答案 1 :(得分:0)
因为你的textFieldDidBeginEditing方法稍后会在第一种情况下调用,而在第二种情况下会在调用之前调用,所以你必须在你的文本字段编辑的shouldChangeCharactersInRange中编码
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
UIDeviceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
viewLogin.frame = CGRectMake((1024-viewLogin.frame.size.width)/2, (768-viewLogin.frame.size.height)/2.5, viewLogin.frame.size.width, viewLogin.frame.size.height);
}
else {
viewLogin.frame = CGRectMake((768-viewLogin.frame.size.width)/2, (1024-viewLogin.frame.size.height)/2, viewLogin.frame.size.width, viewLogin.frame.size.height);
}
return YES;
}
答案 2 :(得分:0)
您可以使用以下方法解决此问题...不要进入UIDeviceOrientation ..使用
UIInterfaceOrientation
- (void)textFieldDidBeginEditing:(UITextField *)textField {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
viewLogin.frame = CGRectMake((1024-viewLogin.frame.size.width)/2, (768-viewLogin.frame.size.height)/9, viewLogin.frame.size.width, viewLogin.frame.size.height);
}
else {
viewLogin.frame = CGRectMake((768-viewLogin.frame.size.width)/2, (1024-viewLogin.frame.size.height)/2, viewLogin.frame.size.width, viewLogin.frame.size.height);
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
viewLogin.frame = CGRectMake((1024-viewLogin.frame.size.width)/2, (768-viewLogin.frame.size.height)/2.5, viewLogin.frame.size.width, viewLogin.frame.size.height);
}
else {
viewLogin.frame = CGRectMake((768-viewLogin.frame.size.width)/2, (1024-viewLogin.frame.size.height)/2, viewLogin.frame.size.width, viewLogin.frame.size.height);
}
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
viewLogin.frame = CGRectMake((1024-viewLogin.frame.size.width)/2, (768-viewLogin.frame.size.height)/9, viewLogin.frame.size.width, viewLogin.frame.size.height);
}
else {
viewLogin.frame = CGRectMake((768-viewLogin.frame.size.width)/2, (1024-viewLogin.frame.size.height)/2, viewLogin.frame.size.width, viewLogin.frame.size.height);
}
return YES;
}
答案 3 :(得分:0)
注册到UIKeyboardWillShowNotification,并将此代码移动到抛出通知时调用的方法。因此,如果您首先在纵向模式下单击textField然后更改为横向,则首先调用此方法。 在更改框架之前不要忘记检查方向。(即,仅在方向为横向时才进行)
编辑 - 添加代码
在您的视图中加载(或在适当的地方),
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(keyBoardWillShow:) name:@"UIKeyboardWillShowNotification" object:nil];
方法keyBoardWillShow:
-(void)keyBoardWillShow:(NSNotification *)noti {
UIDeviceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
viewLogin.frame = CGRectMake((1024-viewLogin.frame.size.width)/2, (768-viewLogin.frame.size.height)/9, viewLogin.frame.size.width, viewLogin.frame.size.height);
}
else {
viewLogin.frame = CGRectMake((768-viewLogin.frame.size.width)/2, (1024-viewLogin.frame.size.height)/2, viewLogin.frame.size.width, viewLogin.frame.size.height);
}
}