开发者

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:

UIToolbar on top of keyboard: Change frame?

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:

UIToolbar on top of keyboard: Change frame?

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?


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


Instead of Calculating the Origins and sizes you can use Autosizing to render the Controls during rotation.

See this tutorial http://www.bogotobogo.com/XcodeSDK-Chapter4.html

I think this can give you a hint.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜