我正在用帆布做游戏。该游戏因排名靠前而被剪裁,我尝试了SafeAreaLayoutGuide,但没有任何反应。请查看下面的代码,让我知道我在做什么错。

-(void) createGLView {
    //create our openglview and size it correctly
    OpenGLView *glView = [[OpenGLView alloc] initWithFrame:self.appDelegate.initFrame];

    self.view = glView;
    self.appDelegate.canvas = glView;

    core_init_gl(1);

    glView.backgroundColor = [UIColor redColor];
    glView.translatesAutoresizingMaskIntoConstraints = NO;

    [glView.leadingAnchor constraintEqualToAnchor:glView.safeAreaLayoutGuide.leadingAnchor].active = YES;
    [glView.trailingAnchor constraintEqualToAnchor:glView.safeAreaLayoutGuide.trailingAnchor].active = YES;
    [glView.topAnchor constraintEqualToAnchor:glView.safeAreaLayoutGuide.topAnchor].active = YES;
    [glView.bottomAnchor constraintEqualToAnchor:glView.safeAreaLayoutGuide.bottomAnchor].active = YES;

    int w = self.appDelegate.screenWidthPixels;
    int h = self.appDelegate.screenHeightPixels;
    tealeaf_canvas_resize(w, h);

    NSLOG(@"{tealeaf} Created GLView (%d, %d)", w, h);
}


红色进入顶部凹槽。我的意思是全屏。如何解决呢?

最佳答案

您需要具有全屏父视图。然后,您可以将OpenGLView添加为子视图,并将其约束连接到父视图的safeAreaLayoutGuide

- (void)createGLView {

    OpenGLView *glView = [[OpenGLView alloc] initWithFrame:self.appDelegate.initFrame];
    [self.view addSubview:glView];

    self.appDelegate.canvas = glView;

    core_init_gl(1);

    glView.backgroundColor = [UIColor redColor];
    glView.translatesAutoresizingMaskIntoConstraints = NO;

    [glView.leadingAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.leadingAnchor].active = YES;
    [glView.trailingAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.trailingAnchor].active = YES;
    [glView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor].active = YES;
    [glView.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor].active = YES;

    int w = self.appDelegate.screenWidthPixels;
    int h = self.appDelegate.screenHeightPixels;
    tealeaf_canvas_resize(w, h);

    NSLOG(@"{tealeaf} Created GLView (%d, %d)", w, h);
}

10-07 14:33