2011-11-06 60 views
0

我從一個空的項目開始,並添加了一個視圖控制器及其筆尖。在筆尖內部,有一個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]; 
} 

Nib

回答

0

確實,當你的看法加載moveScrollView方法被調用?

嘗試在-viewDidLoad方法中設置斷點並確保在moveScrollView方法之前調用斷點。

希望這有助於 文森特

+0

它註冊到keyboardWIllShow通知 – denniss

3

不能打印與%@框架。 %@打印Objective C對象;框架不是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 

打印目標C對象,

print (CGRect)[scrollView frame] 

打印的框架。

順便說一下,在Apple文檔中調整鍵盤滾動視圖的推薦方法是設置內容嵌入,而不是更改框架。我也開始改變框架,並有奇怪的問題。

+0

感謝這麼多的指針。我會看到我用這個得到的。 – denniss

相關問題