[英]iOS 7 iPad Orientation issue, when view moves upside after keyboard appears on UITextField
[英]Sliding UITextField on iPad when Keyboard Shows
我有來自 Cocoa 的代碼,當鍵盤出現時,它會滾動 UITextField,這樣鍵盤就不會覆蓋 UITextField。
這是代碼:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
// Issue is that numerator isn't big enough for bottom 3rd of the screen
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
NSLog(@"Midline: %g Fraction: %g / %g", midline, numerator, denominator);
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
出於調試目的,我 output 中線等,看看發生了什么。 這是從上到下的制表符。 您可以看到大約第 6 個分子變為負數,因為為其父視圖轉換的 UITExtField.size.height 是一個低數字。 我無法弄清楚為什么這個數字會是負數。 高度應該像所有其他人一樣向上,就像您在視圖中的 go 一樣。
2011-05-24 09:36:08.600 Baby Bloom[27794:207] 中線 = 246 + 0.5 * 167
2011-05-24 09:36:08.601 Baby Bloom[27794:207] 中線:329.5 分數:22.3 / 409.6
2011-05-24 09:36:09.535 Baby Bloom[27794:207] 中線 = 246 + 0.5 * 167
2011-05-24 09:36:09.536 Baby Bloom[27794:207] 中線:329.5 分數:22.3 / 409.6
2011-05-24 09:36:09.929 Baby Bloom[27794:207] 中線 = 246 + 0.5 * 246
2011-05-24 09:36:09.930 Baby Bloom[27794:207] 中線:369 分數:61.8 / 409.6
2011-05-24 09:36:10.313 Baby Bloom[27794:207] 中線 = 246 + 0.5 * 246
2011-05-24 09:36:10.314 Baby Bloom[27794:207] 中線:369 分數:61.8 / 409.6
2011-05-24 09:36:10.793 Baby Bloom[27794:207] 中線 = 246 + 0.5 * 97
2011-05-24 09:36:10.794 Baby Bloom[27794:207] 中線:294.5 分數:-12.7 / 409.6
2011-05-24 09:36:11.785 Baby Bloom[27794:207] 中線 = 246 + 0.5 * 148
2011-05-24 09:36:11.786 Baby Bloom[27794:207] 中線:320 分數:12.8 / 409.6
事實證明,當在 iPad 上的景觀中時,它會切換原點(x 現在是 y,寬度現在是高度。
這是使其工作的代碼:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
float origin;
float textFieldHeight;
float viewOrigin;
float viewHeight;
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
textFieldHeight = textFieldRect.size.height;
origin = textFieldRect.origin.y;
viewOrigin = viewRect.origin.y;
viewHeight = viewRect.size.height;
}
else
{
textFieldHeight = textFieldRect.size.width;
viewOrigin = viewRect.origin.x;
viewHeight = viewRect.size.width;
origin = viewHeight - textFieldRect.origin.x;
}
CGFloat midline = origin + 0.5 * textFieldHeight;
// Take the current middle y location of the textfield
// If it is in the top 20% of the view don't move it
// If it is in the middle 60% move it by a fraction of the keyboard size
// If it is in the bottom 20% move it by the whole size of the keyboard
CGFloat numerator = midline - viewOrigin - MINIMUM_SCROLL_FRACTION * viewHeight;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewHeight;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
//NSLog(@"Denom: %g", denominator);
//NSLog(@"Distance: %g", animatedDistance);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
origin = viewHeight - textFieldRect.origin.x;
我認為你不需要這個,因為它會產生不正確的字段原點和非常大的中間值,這會在不需要的地方執行滾動。 所以通常你應該只使用 x 原點作為 y。
origin = textFieldRect.origin.x;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.