2011-11-06 40 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

相关问题