0

I have an app which intakes client details, built for iPad. When the user taps a UITextField towards the bottom half of the ViewController, the frame programatically shifts upwards so that the fields aren't hidden behind the keyboard. (I tried to implement a UIScrollView but just cannot seem to get it working.) The slight issue I'm having now is when the frame shifts up, you can vaguely see the black behind it. This isn't a huge issue because I have changed the animation time and the black background is barely visible, but I have a feeling there is a more elegant solution.

Here is my code to shift the frame:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    // Animate frame up if lower textfields are tapped
    if (([textField isEqual:_emailField]) || ([textField isEqual:_dobField]) || ([textField isEqual:_niNumField])) {
        [UIView beginAnimations:nil
                    context:NULL];
        [UIView setAnimationDuration:0.35f];
        CGRect frame = self.view.frame;
        frame.origin.y = -210;
        [self.view setFrame:frame];
        [UIView commitAnimations];
    } else {
        // Return frame to original position if other textfields are tapped
        [self dismissKeyboard];
    }
    return YES;
}

- (void)dismissKeyboard {
        [UIView beginAnimations:nil
                        context:NULL];
        [UIView setAnimationDuration:0.15f];
        CGRect frame = self.view.frame;
        frame.origin.y = 0;
        [self.view setFrame:frame];
        [UIView commitAnimations];
}

Here is a brief screenshot of what I'm trying to describe. Left picture is the ViewController normally, right picture is when the frame has been shifted upwards. You can see (vaguely) almost in line with the second row of characters is where the frame stops.

Left: Normal frame. Right: frame.origin.y altered

I realise this doesn't seem like an issue because it is barely visible at all, but I have had to speed up the animation hiding the keyboard or else the frame drags behind and the black background becomes visible. I am wondering: is there a way to change this colour? Or is that something we don't have access to? If anyone can suggest a more elegant method for what I am trying to do, I'd gladly take a better solution. Thanks in advance.

rosshump
  • 370
  • 1
  • 4
  • 21

1 Answers1

1

Your UIViewController's view is added to the main UIWindow. So you should be able to achieve what you want by changing the UIWindow's background color.

In the UIApplicationDelegate:

self.window.backgroundColor = [UIColor whiteColor];

However, what you're doing isn't the best way to solve the problem of the keyboard covering up the text fields. You should use a UIScrollView or a UITableView to manage this view and use content insets to shift the view up or down.

Hesham
  • 5,294
  • 3
  • 34
  • 48
  • Using your code I was able to successfully change the `window` colour to white. This solves the immediate issue, thanks! I will look into `UIScrollView` again and try to implement it. Thanks for your help :) – rosshump Sep 24 '14 at 08:36