简体   繁体   English

模拟器的行为不同于设备的行为?

[英]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. 所以我总是通过keyboardFrameBeginRect.size.height-10修改y坐标。 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. 它已经在iPhone 4s上进行了测试,并且可以正常工作。 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). 它已在iPhone 5s的模拟器上进行了测试,并按预期工作,现在很有趣:当我通过testflight部署代码时,在调用keyboardDismiss: footer不再可见(在iPad2和iPhone 5s上进行了测试,问题是仅适用于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). 我所做的是在keyboardDismiss:之前添加了警报keyboardDismiss:将返回,将打印footery坐标,结果是在模拟器中为446 ,在设备版本(iPhone 5s)中为506 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. footer初始化: CGRectMake((kWidth - kHSeparator - kButtonSize), (kHeight - kVSeparator- kButtonSize), kButtonSize, kButtonSize) ,其中kWidthkHeight是屏幕的宽度和高度,其他只是常量。

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. 但是,模拟器和设备之间的差异是iOS开发中不可或缺的事实,进行实际的设备测试以发现此类问题非常重要。

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. 但是我想如果要根据键盘大小将视图移动到新位置,则要使用UIKeyboardFrameEndUserInfoKey而不是UIKeyboardFrameBeginUserInfoKey。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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