[英]Confused to why my UISCrollView's frame is null
我從一個空項目開始,並添加了一個視圖控制器及其筆尖。 在筆尖內部,有一個帶有UIScrollView的視圖作為該視圖的子視圖。 在滾動視圖中,我有多個資源,例如文本字段,標簽和按鈕。
我正要在鍵盤顯示時我的滾動視圖向上移動。 我查看了stackoverflow上的操作方法。 我基本上復制了代碼並試圖理解它。 但是,視圖仍然沒有上升。 所以是時候來一些調試語句了。
我的UIScrollView已連接(IBOutlet)到RootViewController,並且可以從那里訪問。 我嘗試打印出scrollView(實例名稱),然后得到了一個對象。 但是,當我嘗試打印scrollView.frame ...時,我得到了空值...有人對此有任何想法嗎?
這是一些調試語句的代碼片段
- (void) moveScrollView:(NSNotification *) notification up: (BOOL) upFlag{
NSDictionary * userInfo = [notification userInfo];
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
CGRect keyboardEndFrame;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
CGRect newFrame = scrollView.frame;
CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];
NSLog(@"scrollView: %@", scrollView);
NSLog(@"frame: %@", scrollView.frame);
NSLog(@"Old Height: %@", scrollView.frame.size.height);
newFrame.size.height -= keyboardFrame.size.height * (upFlag ? 1 : -1);
NSLog(@"New Height: %@", newFrame.size.height);
scrollView.frame = newFrame;
[UIView commitAnimations];
}
- (void) keyboardShow:(NSNotification *) notification{
NSLog(@"Keyboard Show");
[self moveScrollView: notification up:YES];
}
您無法使用%@打印框架。 %@打印目標C對象; frame不是Objective C對象。 它是一個C結構,CGRect。 像這樣單獨打印其組件:
NSLog(@"frame: (%0f %0f; %0f %0f)",
scrollView.frame.origin.x, scrollView.frame.origin.y,
scrollView.frame.size.width, scrollView.frame.size.height);
我認為使用調試器更有效。 在調試器中可以使用
po object
打印Objective C對象,以及
print (CGRect)[scrollView frame]
打印框架。
順便說一句,在Apple文檔中調整鍵盤滾動視圖的推薦方法是設置內容插圖,而不是更改框架。 我也開始更換框架,遇到了奇怪的問題。
像打印框架
NSLog(@"%@", NSStringFromRect(frame));`
您確定在加載視圖時調用了moveScrollView方法嗎?
嘗試在-viewDidLoad方法中設置一個斷點,並確保在moveScrollView方法之前調用該斷點。
希望對您有幫助,文森特
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.