简体   繁体   中英

Simulator's behavior different from the device's?

In my app at one point I would like to move a button up if the keyboard appears such that the button is alway visible, and move it back when the keyboard would dismiss:

- (void) keyboardDismiss :(NSNotification*)notification{
    NSDictionary* keyboardInfo = [notification userInfo];
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
    UIView animateWithDuration:0.4 animations:^{
        footer.frame = CGRectMake(X(footer), Y(footer)+keyboardFrameBeginRect.size.height-10, WIDTH(footer), HEIGHT(footer));
    }];
}

- (void) keyboardShow:(NSNotification*)notification{
    NSDictionary* keyboardInfo = [notification userInfo];
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
    [UIView animateWithDuration:0.4 animations:^{
        footer.frame = CGRectMake(X(footer), Y(footer)-keyboardFrameBeginRect.size.height+10, WIDTH(footer), HEIGHT(footer));
    }];
}

So I am modifying the y coordinate by keyboardFrameBeginRect.size.height-10 always. What I would expect is that the code should behave the same way on both the simulator and the actual device. It was tested on an iPhone 4s and works as expected. It was tested in the simulator on iPhone 5s and works as expected, now the interesting part: when I deploy the code via testflight, after the call to keyboardDismiss: the footer is not visible anymore (tested on iPad2 and iPhone 5s and the problem is only with the iPhone). What I did was to add an alert right before keyboardDismiss: will return that would print the y coordinate of the footer , the results are 446 in the simulator and 506 in the device version (iPhone 5s). What could be the reason of different results?

Edits:

footer initialisation: CGRectMake((kWidth - kHSeparator - kButtonSize), (kHeight - kVSeparator- kButtonSize), kButtonSize, kButtonSize) where kWidth and kHeight are screen width and height, other are just constants.

What I would expect is that the code should behave the same way on both the simulator and the actual device.

In general, this is a safe assumption. But differences between the simulator and device are a fact of life with iOS development and it's important to do actual device testing to find these kinds of issues.

But I would imagine that you would want to use UIKeyboardFrameEndUserInfoKey instead of UIKeyboardFrameBeginUserInfoKey if you want to move a view to a new position based off of the keyboard size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM