Search code examples
iphoneuitoolbar

UIToolbar on top of keyboard: Change frame?


I have a UIToolbar set to the inputAccessoryView of a UITextView. This shows the toolbar on top of the keyboard, when editing. I have, however, encountered a problem when rotating the device: the toolbar should become a little less heigh (32 pixels vs 44). When I just update the height of the toolbar, I get a little white space between the toolbar and the keyboard:

image

So I used -didRotateFromInterfaceOrientation: to also update the origin, and then it works when rotating from portrait to landscape. When rotating back up, however, the toolbar is 12 pixel too low and overlapping the keyboard:

image

By then, the origin is (0,0) again and setting a negative value didn't help. Any suggestions of how to make it work so that the toolbar changes its size and never overlaps?


Solution

  • I fixed it by calling [textView resignFirstResponder] in -willRotateToInterfaceOrientation:duration: and [textView becomeFirstResponder] in -didRotateFromInterfaceOrientation:. The system seems to adjust the frame properly.

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        didShowKeyboard = [self.textView isFirstResponder];
    
        [self.textView resignFirstResponder];
    }
    
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
        CGRect toolbarFrame = [self.navigationController.toolbar bounds];
        self.keyboardToolbar.frame = toolbarFrame;
    
        if (didShowKeyboard)
            [self.textView becomeFirstResponder];
    }