【问题标题】:Moving dialog up when keyboard present works except when ipad is turned around当键盘存在时向上移动对话框,除非 ipad 转身
【发布时间】:2013-04-19 12:06:06
【问题描述】:

模式对话框在键盘出现时向上移动,在键盘消失时向下移动。

在我旋转 iPad 之前一切都很好。在除标准之外的任何其他方向上它都不起作用。当 iPad 转动时,模式对话框在键盘出现时向下移动而不是向上移动,当键盘消失而不是向下移动时向上移动。

这是我用来在键盘出现/消失时定位模式对话框的代码。

- (void)textFieldDidBeginEditing:(UITextField *)textField {


        self.view.superview.frame = CGRectMake(self.view.superview.frame.origin.x, 140, self.view.superview.frame.size.width, self.view.superview.frame.size.height);

        }      
    }];

}


-(void)textFieldDidEndEditing:(UITextField *)textField {

    [UIView animateWithDuration:0.4 animations:^ {

        self.view.superview.frame = CGRectMake(self.view.superview.frame.origin.x, 212, self.view.superview.frame.size.width, self.view.superview.frame.size.height);
        }
    }];

}

【问题讨论】:

  • 代码看起来很眼熟。不就是根据设备方向调整Y值的问题吗?

标签: ios objective-c ipad orientation modal-dialog


【解决方案1】:

不要设置框架,而是使用 CGAffineTransformTranslate,例如像这样:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.view.superview.transform = CGAffineTransformTranslate(self.view.superview.transform,0,72);
    }      
}];
}


-(void)textFieldDidEndEditing:(UITextField *)textField {
[UIView animateWithDuration:0.4 animations:^ {
    self.view.superview.transform = CGAffineTransformTranslate(self.view.superview.transform,0,-72);
    }
}];
}

【讨论】:

  • 那行得通。只需要交换 72 和 -72。你知道什么方法可以改变高度和宽度吗?
  • 我猜你可以使用 CGAffineTransformScale。除了 Apple 自己的参考资料 (developer.apple.com/library/ios/ipad/#documentation/…),我会推荐这篇关于 CGAffineTransform 的好帖子,它对我有很大帮助:iphonedevelopment.blogspot.com/2008/10/…
  • 当我在 UIViewController 中包含模态对话框时,这在 iOS8 上不起作用。导航控制器只是向上跳了一下然后又向下跳。
【解决方案2】:

您应该尝试使用键盘通知:

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

然后在选择器中调整框架。不在 textFieldDidBeginEditing/textFieldDidEndEditing 中。

- (void)keyboardWasShown:(NSNotification *) notification {
    NSDictionary *info = [notification userInfo];
    NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;
    keyboardHeight = MIN(keyboardSize.height, keyboardSize.width);
    // set new frame based on keyboard size 

- (void)keyboardWillBeDismissed: (NSNotification *) notification{
    [UIView animateWithDuration:0.4 animations:^{
    // original frame 
    }];
}

【讨论】:

  • keyboardHeight 是什么类型?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-26
  • 2015-03-29
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
相关资源
最近更新 更多