我從一個空的項目開始,並添加了一個視圖控制器及其筆尖。在筆尖內部,有一個UIScrollView作爲視圖的孩子。在滾動視圖中,我擁有多個資源,如文本框,標籤和按鈕。混淆爲什麼我的UISCrollView的框架爲空
我在想讓我的scrollview在鍵盤出現時上移的點。我查看了如何做到這一點的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];
}
它註冊到keyboardWIllShow通知 – denniss