【发布时间】: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: 将返回之前添加一个警报,它将打印footer 的y 坐标,结果是模拟器中的446 和设备版本中的506(iPhone 5s) .不同结果的原因可能是什么?
编辑:
footer 初始化:CGRectMake((kWidth - kHSeparator - kButtonSize), (kHeight - kVSeparator- kButtonSize), kButtonSize, kButtonSize) 其中kWidth 和kHeight 是屏幕宽度和高度,其他只是常量。
【问题讨论】:
-
你可以添加代码来计算页脚吗?肯定是 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