2012-05-26 238 views
0

我想设置一个GLKView的初始大小(基本上,我希望它完全是屏幕的大小,尽管存在tabbar)。 因为我使用故事板,所以GLKView是在GLKViewController init方法中自动创建的(不确定,它没有真正记录,但我认为是这样)。在GLKViewController我只有viewDidLoad方法,那就是:设置GLKView的初始大小与故事板

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 
    if (!self.context) { 
     NSLog(@"Failed to create ES context"); 
    } 

    GLKView *view = (GLKView *)self.view; 
    view.context = self.context; 
} 

所以,如果我不使用的故事板,我会写这样的事:

EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 
GLKView *view = [[GLKView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
view.context = context; 
view.delegate = self; 

GLKViewController * viewController = [[GLKViewController alloc] initWithNibName:nil bundle:nil]; 
viewController.view = view; 

用故事板的时候可是,我没有机会做到这一点:

GLKView *view = [[GLKView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

我试图改变现有的视图的框架,创建一个新的GLKView,然后将其分配给viewController.view - 没有结果。

是否可以在不同的方法等中设置或更改GLKView的大小?

回答

0

找到解决方案。

第一,我们应该将故事板中的GLKViewController的“想要全屏”属性更改为true。这将允许有条件地隐藏状态栏不调整GLKView,像这样:

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade]; 

第二,我们应该使用TabBar的子视图的框架改变为全屏帧。看来这只是一种工作方法,在下提供的标签栏。该方法应放置在TabViewController子类中。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    CGRect tabbarFrame = CGRectZero; 
    for (UIView *view in self.view.subviews) 
    { 
     if ([view isKindOfClass:[UITabBar class]]) 
     { 
      tabbarFrame = view.frame; 
      break; 
     } 
    }  

    for (UIView *view in self.view.subviews) 
    { 
     if (![view isKindOfClass:[UITabBar class]]) 
     { 
      view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, 
            view.frame.size.width, view.frame.size.height + tabbarFrame.size.height); 
     } 
    } 
} 

再次,这将允许隐藏标签栏在需要的时候,用这短短的一段代码:

for (UIView *view in self.view.subviews) 
{ 
    if ([view isKindOfClass:[UITabBar class]]) 
    { 
     [UIView animateWithDuration:0.4f 
         animations:^{ 
          [view setAlpha:0.f]; 
         } 
         completion:nil]; 
     break; 
    } 
}