Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Custom keyboard's frame shrinks 20 points if status bar increases during recording or phone call?

I have a problem with Keyboard Extension on iOS (real) device during recoding or phone call (iOS Simulator doesn't have red status bar). Because of the status bar increases 20 points, the custom keyboard also moves 20 points in Y-axis and decreases its height to 196 points (it should be 216 points). However, when I print the view.frame, it shows {{0.0, 0.0}, {320.0, 196.0}}.

Here is the screenshot.

If I use storyboard instead of programmatically adding views, it works fine. First I thought it's because of topLayoutGuide.length, but it shows 0.0 in the debug area.

I has tried to find solution or any topics related to this problem but it seems like me alone facing it. :(

like image 724
João Oliveira Avatar asked Jan 08 '23 10:01

João Oliveira


1 Answers

A workaround that works to me:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    if (CGRectGetMinY(self.view.superview.frame) > 0.f) {
        CGRect frame = self.view.superview.frame;
        frame.origin.y = 0.f;
        [self.view.superview setFrame:frame];
    }
}

Basically, we're looking for superview's frame and I "adjust" it if it gets shifted.

Edit:

As TomSawyer mentioned, there is an issue with the previous solution. This one should solve them both.

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    CGRect frame = self.view.superview.frame;
    CGFloat dy = CGRectGetMinY(frame);
    if (dy > 0.f) {
        frame.origin.y = 0.f;
        frame.size.height += dy;
        [self.view.superview setFrame:frame];
    }
}
like image 72
thelvis Avatar answered Jan 29 '23 11:01

thelvis