使用情節提要設置 GLKView 的初始大小 (Set an initial size of GLKView with storyboard)


問題描述

使用情節提要設置 GLKView 的初始大小 (Set an initial size of GLKView with storyboard)

I'm trying to set an initial size of GLKView (basically, I want it to be exactly the size of the screen, despite the presence of tabbar). Because I'm using storyboard, GLKView is created automatically within GLKViewController init method (not sure, it is not really documented, but I think so). In GLKViewController I have only viewDidLoad method, here it is:

- (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;
}

So, if I don't use storyboard, I'd write something like this:

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;

But when using storyboard, I don't have an opportunity to do this:

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

I have tried to change the frame of the existing view, create a new GLKView and then assign it to viewController.view - with no result.

Is it maybe possible to set up or change the size of the GLKView in different method, etc.?


參考解法

方法 1:

Found the solution.

The first, we should change "Wants Full Screen" property for GLKViewController in storyboard to true. This will allow to conditionally hide status bar without resizing GLKView, like this:

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

The second, we should change the frame of the tabbar's subview to fullscreen frame. It seems that this is only one working method to present something under the tab bar. This method should be placed in a TabViewController child class.

- (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);
        }
    }
}

And again, this will allow to hide the tab bar when needed, with this short piece of code:

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

(by dmirkitanovdmirkitanov)

參考文件

  1. Set an initial size of GLKView with storyboard (CC BY-SA 3.0/4.0)

#storyboard #iOS #opengl-es-2.0 #glkit






相關問題

如何在 XAML 中為資源設置動畫? (How to animate a resource in XAML?)

使用情節提要設置 GLKView 的初始大小 (Set an initial size of GLKView with storyboard)

在情節提要中為“平鋪”創建更大的反面視圖 (Creating a larger flip-side view for 'tile' in storyboard)

XNA/XAML 鼠標懸停時動畫邊框顏色 (XNA/XAML Animate border color on mouseover)

帶有自定義按鈕的故事板 (Storyboard with Custom Buttons)

添加子視圖控制器並更改其標籤 (Adding a child view controller and changing its label)

Thêm các nút vào Ô Chế độ xem Bảng tùy chỉnh trong Lớp Bảng Xem tùy chỉnh (Adding buttons to custom Table View Cell under custom TableView Class)

ios在表格視圖單元格中視覺調整圖像大小 (ios visually resize image in a table view cell)

如何解決“在類型'ControlTemplate'中找不到可附加屬性'觸發器'”? (How to resolve "The attachable property 'Triggers' was not found in type 'ControlTemplate'"?)

Xcode 7.3、Swift 2.2 故事板極慢 (Xcode 7.3, Swift 2.2 Storyboards Extremely Slow)

Xcode Storyboard 警告未提供上下文:不支持的約束屬性配置 (Xcode Storyboard Warning not providing context: Unsupported Configuration of constraint attributes)

推送視圖控制器在 Swift 中不起作用 (Push view controller not working in Swift)







留言討論