Skip to content

Mac: size of window does not take into account title bar height, view cut off at top #1393

Description

@LearnCocos2D

To reproduce, create a new project, add a label that's top-left aligned with position 0,0 and anchorpoint 0,1 .. something like this:

screen shot 2015-03-01 at 14 47 38

Run Mac build, first two lines are cut off in this example:
screen shot 2015-03-01 at 14 49 06

The problem is with how the AppDelegate initializes window and view with the same size:

    CGSize windowSize = CGSizeMake(480.0f, 320.0f);
    [_window setFrame:CGRectMake(0, 0, windowSize.width, windowSize.height) display:true animate:false];
    [_glView setFrame:_window.frame];

This does not take into account that the window creates a title bar which covers the top area of the window, so any view must be "titlebar height" smaller than the window, respectively the window's height must be increased by that amount.

Commonly title bar height is determined like so:

- (float) titleBarHeight
{
    NSRect frame = NSMakeRect (0, 0, 200, 200);
    NSRect contentRect;
    contentRect = [NSWindow contentRectForFrameRect:frame styleMask:NSTitledWindowMask];
    return (frame.size.height - contentRect.size.height);
}

To fix this, change the initialization of window and view to this:

    CGSize windowSize = CGSizeMake(480.0f, 320.0f);
    [_window setFrame:CGRectMake(0, 0, windowSize.width, windowSize.height + [self titleBarHeight]) display:true animate:false];
    [_glView setFrame:CGRectMake(0, 0, windowSize.width, windowSize.height)];

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions