Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Turns out that when in Landscape on the iPad it switches up the origin (x is now y and width is now height.</p> <p>Here is the code to make it work:</p> <pre><code>- (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 &lt; 0.0) { heightFraction = 0.0; } else if (heightFraction &gt; 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]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload