[英]Simulator's behavior different from the device's?
在我的應用程序中,有一點我想在鍵盤出現時向上移動按鈕,使得該按鈕始終可見,然后在鍵盤消失時將其移回:
- (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:
之前添加了警報keyboardDismiss:
將返回,將打印footer
的y
坐標,結果是在模擬器中為446
,在設備版本(iPhone 5s)中為506
。 結果不同的原因可能是什么?
編輯:
footer
初始化: CGRectMake((kWidth - kHSeparator - kButtonSize), (kHeight - kVSeparator- kButtonSize), kButtonSize, kButtonSize)
,其中kWidth
和kHeight
是屏幕的寬度和高度,其他只是常量。
我希望代碼在模擬器和實際設備上的行為應相同。
通常,這是一個安全的假設。 但是,模擬器和設備之間的差異是iOS開發中不可或缺的事實,進行實際的設備測試以發現此類問題非常重要。
但是我想如果要根據鍵盤大小將視圖移動到新位置,則要使用UIKeyboardFrameEndUserInfoKey而不是UIKeyboardFrameBeginUserInfoKey。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.