bko*_*cik 12 iphone keyboard ipad ios landscape-portrait
我意识到有许多类似的解决方案,例如TPKeyboardAvoiding,Apple着名的解决方案,以及涉及使用UIScrollView的各种建议.在我的情况下,我需要调整视图大小以适应键盘而不是滚动或移动它.这个解决方案最接近我想要达到的目标,所以这是我的基础.但是我在横向模式下工作时遇到了问题.键盘出现时调整视图大小的方法是:
- (void)keyboardWillShow:(NSNotification *)note {
NSDictionary *userInfo = note.userInfo;
NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGRect keyboardFrame = [[self textField].superview convertRect:[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] fromView:nil];
CGRect statusBarFrame = [[self textField].superview convertRect:[UIApplication sharedApplication].statusBarFrame fromView:nil];
CGRect bounds = [self textField].superview.bounds;
CGRect newFrame = CGRectMake(0.0, 0.0, bounds.size.width, keyboardFrame.origin.y + statusBarFrame.size.height);
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
[self textField].superview.frame = newFrame;
} completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
这在肖像模式下完美运行.

但是,在横向模式下,视图会从左到右或从右到左调整大小,具体取决于设备旋转的方向,而不是从下向上.

显然我使用坐标有些不对劲,而且在横向模式下,某些参考框架并不是我认为的那样,但我有一点时间整理出如何解决它.我尝试用-convertRect转换各种各样的东西:但是我正在尝试的任何东西都让我无处可去.
我真的希望有人不会对所有这些矩形感到困惑,以及当方向变化时他们如何改变可以发现我做错了什么以及我需要做些什么才能做到这一点.作为参考,我创建了一个项目,显示了最简单的情况,可以重现我遇到的问题.
Vit*_*erg 27
我不建议您调整视图控制器的根视图,您可以创建contentView并添加到视图控制器的视图.你可以改变这个contentView的大小如下(我不使用autolayouting):
- (void)keyboardWillShow:(NSNotification *)note {
NSDictionary *userInfo = note.userInfo;
NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil];
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
self.contentView.frame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y);
} completion:nil];
}
- (void)keyboardWillHide:(NSNotification *)note {
NSDictionary *userInfo = note.userInfo;
NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil];
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
self.contentView.frame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y);
} completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
Vitaliy B的答案很快.我有一个名为templateHeaderContentView的视图,我创建了一个函数并在那里配置了视图高度.您可以使用自己的视图并相应地更改高度.
func keyboardWillShow(notification: NSNotification) {
keyboardShowOrHide(notification)
}
func keyboardWillHide(notification: NSNotification) {
keyboardShowOrHide(notification)
}
private func keyboardShowOrHide(notification: NSNotification) {
guard let userInfo = notification.userInfo else {return}
guard let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey]else { return }
guard let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] else { return }
guard let keyboardFrameEnd = userInfo[UIKeyboardFrameEndUserInfoKey] else { return }
let curveOption = UIViewAnimationOptions(rawValue: UInt(curve.integerValue << 16))
let keyboardFrameEndRectFromView = view.convertRect(keyboardFrameEnd.CGRectValue, fromView: nil)
UIView.animateWithDuration(duration.doubleValue ?? 1.0,
delay: 0,
options: [curveOption, .BeginFromCurrentState],
animations: { () -> Void in
self.templateHeaderContentView.configureView(keyboardFrameEndRectFromView.origin.y)
}, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20685 次 |
| 最近记录: |