Mos*_*she 5 objective-c frame uitextview ios cgrectmake
我正在尝试在键盘显示时调整UITextView的大小.在iPhone上它工作得很漂亮.当系统调度键盘通知时,文本视图会调整大小.当它完成编辑后,我调整它以填充初始空间.(是的,我假设编辑停止时键盘已经消失.我应该改变它.但是,我认为这不是我的问题.)
当我在iPad上调整textview的大小时,框架会正确调整大小,但应用程序似乎会将框架的Y值重置为零.这是我的代码:
- (void) keyboardDidShowWithNotification:(NSNotification *)aNotification{
//
// If the content view being edited
// then show shrink it to fit above the keyboard.
//
if ([self.contentTextView isFirstResponder]) {
//
// Grab the keyboard size "meta data"
//
NSDictionary *info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
//
// Calculate the amount of the view that the keyboard hides.
//
// Here we do some confusing math voodoo.
//
// Get the bottom of the screen, subtract that
// from the keyboard height, then take the
// difference and set that as the bottom inset
// of the content text view.
//
float screenHeightMinusBottom = self.contentTextView.frame.size.height + self.contentTextView.frame.origin.y;
float heightOfBottom = self.view.frame.size.height - screenHeightMinusBottom;
float insetAmount = kbSize.height - heightOfBottom;
//
// Don't stretch the text to reach the keyboard if it's shorter.
//
if (insetAmount < 0) {
return;
}
self.keyboardOverlapPortrait = insetAmount;
float initialOriginX = self.contentTextView.frame.origin.x;
float initialOriginY = self.contentTextView.frame.origin.y;
[self.contentTextView setFrame:CGRectMake(initialOriginX, initialOriginY, self.contentTextView.frame.size.width, self.contentTextView.frame.size.height-insetAmount)];
}
Run Code Online (Sandbox Code Playgroud)
为什么这会在iPhone上运行,而不能在iPad上运行?此外,我的自动调整掩码可以进行意外更改吗?
就像 @bandejapaisa 所说,我发现方向是一个问题,至少在我的测试期间是这样。
第一件事是关于误导性的使用kbSize.height,因为在横向方向上它代表键盘的宽度。因此,由于您的代码位于 a 中,UIViewController您可以这样使用它:
float insetAmount = (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)?kbSize.height:kbSize.width) - heightOfBottom;
Run Code Online (Sandbox Code Playgroud)
给出self.interfaceOrientation界面的方向(可以与设备方向不同),如果给定的方向是纵向(顶部或底部),则宏UIInterfaceOrientationIsPortrait返回。因此,由于界面为纵向时YES键盘高度为,而界面为横向时,我们只需测试方向即可获得良好的值。kbSize.heightkbSize.width
但这还不够,因为我发现了同样的问题self.view.frame.size.height。所以我使用了相同的解决方法:
float heightOfBottom = (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)?self.view.frame.size.height:self.view.frame.size.width) - screenHeightMinusBottom;
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助...
| 归档时间: |
|
| 查看次数: |
1128 次 |
| 最近记录: |