【问题标题】:Simulator's behavior different from the device's?模拟器的行为与设备的不同?
【发布时间】:2014-11-25 15:44:16
【问题描述】:

在我的应用程序中,如果键盘出现时,我想向上移动一个按钮以使该按钮始终可见,并在键盘关闭时将其移回:

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

所以我总是用keyboardFrameBeginRect.size.height-10 修改y 坐标。我期望的是代码在模拟器和实际设备上的行为方式应该相同。它在 iPhone 4s 上进行了测试,并按预期工作。它在 iPhone 5s 上的模拟器中进行了测试,并按预期工作,现在有趣的部分是:当我通过 testflight 部署代码时,在调用 keyboardDismiss: 之后,footer 不再可见(在 iPad2 和 iPhone 5s 和问题仅出在 iPhone 上)。我所做的是在keyboardDismiss: 将返回之前添加一个警报,它将打印footery 坐标,结果是模拟器中的446 和设备版本中的506(iPhone 5s) .不同结果的原因可能是什么?

编辑:

footer 初始化:CGRectMake((kWidth - kHSeparator - kButtonSize), (kHeight - kVSeparator- kButtonSize), kButtonSize, kButtonSize) 其中kWidthkHeight 是屏幕宽度和高度,其他只是常量。

【问题讨论】:

  • 你可以添加代码来计算页脚吗?肯定是 446 和 506。不是 442 和 506。
  • 你是什么意思“计算”我只是输出footer.origin.y
  • 什么是页脚?底部的视图,当您启动它时,它有一个 footer.frame = CGRectMake(0,self.view.bounds.size.heigh-70,....它只知道有关页脚的信息。
  • 您是否也意识到您创建的这些宏已经存在? CGRectGetHeight, CGRectGetWidth... developer.apple.com/library/IOs/documentation/GraphicsImaging/…
  • @KerrM 您还意识到您的宏应用于CGRect 对象,而我的宏应用于具有CGRect 的对象?

标签: ios objective-c xcode


【解决方案1】:

我希望代码在模拟器和实际设备上的行为方式相同。

一般来说,这是一个安全的假设。但是模拟器和设备之间的差异是 iOS 开发中的一个事实,进行实际设备测试以发现此类问题非常重要。

但我想如果你想根据键盘大小将视图移动到新位置,你会想使用 UIKeyboardFrameEndUserInfoKey 而不是 UIKeyboardFrameBeginUserInfoKey。

【讨论】:

  • 你没有回答我的问题。
  • 使用“UIKeyboardFrameEndUserInfoKey”而不是“UIKeyboardFrameBeginUserInfoKey”是答案。这没有用吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-16
  • 2013-03-26
  • 2018-03-09
  • 1970-01-01
  • 2023-04-06
  • 2012-09-01
  • 2010-12-11
相关资源
最近更新 更多