Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Weird issue when moving a view up with textFields

troop231troop231 Posts: 54Registered Users
Hello, I am trying to slide my view up when a uitextfield is active, and it works perfectly on landscapeLeft mode on the iPad, but not landscapeRight.

Here is a picture of the issue (after the code)

It doesn't move at the same interval up as landscapeLeft, and I get this black bar on the bottom of the screen on the third textField.

If you can help me I'll greatly appreciate it.

My code is as follows:

CGFloat animatedDistance;

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 322;

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
{
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];

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.x + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.x
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;

if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}

animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);

CGRect viewFrame = self.view.frame;

if (orientation == UIInterfaceOrientationLandscapeLeft)
{
viewFrame.origin.x -= animatedDistance;
}
if (orientation == UIInterfaceOrientationLandscapeRight)
{
viewFrame.origin.x += animatedDistance;
}

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];


CGRect viewFrame = self.view.frame;

if (orientation == UIInterfaceOrientationLandscapeLeft)
{
viewFrame.origin.x += animatedDistance;
}
if (orientation == UIInterfaceOrientationLandscapeRight)
{
viewFrame.origin.x += animatedDistance;
}

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

[self.view setFrame:viewFrame];
[UIView commitAnimations];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == nameField) {
[textField resignFirstResponder];
[emailField becomeFirstResponder];
} else if (textField == emailField) {
[phoneField becomeFirstResponder];
} else if (textField == phoneField) {
[phoneField resignFirstResponder];
}
return YES;
}


image
Post edited by troop231 on

Replies

  • troop231troop231 Posts: 54Registered Users
    So I sortof solved it with this code: However, when I rotate from landscapeLeft or landscapeRight, to the other landscape mode, it cant remember the frame, and it doesn't look right.

    Can anyone please help me? Thank you! :)

    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
    UIInterfaceOrientation orientation =
    [[UIApplication sharedApplication] statusBarOrientation];

    if (orientation == UIInterfaceOrientationLandscapeLeft ||
    orientation == UIInterfaceOrientationLandscapeRight)
    {

    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;
    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;

    if (heightFraction < 0.0)
    {
    heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
    heightFraction = 1.0;
    }

    animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);

    CGRect viewFrame = self.view.frame;

    if (orientation == UIInterfaceOrientationLandscapeLeft)
    {
    viewFrame.origin.x -= animatedDistance;
    }
    if (orientation == UIInterfaceOrientationLandscapeRight)
    {
    viewFrame.origin.x += animatedDistance;
    }

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];
    [UIView commitAnimations];
    }
    }
Sign In or Register to comment.