Tom
Tom

Reputation: 47

How to position views with CCUIViewWrapper

I am using the code from http://www.cocos2d-iphone.org/forum/topic/6889 to add a UIView to the screen in a Cocos2d game. It works great in non-retina resolution (everything is positioned correctly), but in retina resolution the UIView is placed in a different location. The code I am using to add the view to the game is basically the same as the code from the website:

 UIView *myView = [[[UIView alloc] init] autorelease];
 myView.backgroundColor = [UIColor redColor];
 CCUIViewWrapper *wrapper = [CCUIViewWrapper wrapperForUIView:myView];
 wrapper.contentSize = CGSizeMake(100, 100);
 wrapper.position = ccp(50,50);
 [self addChild:wrapper];

The view appears 50px from the bottom and left corner in non retina and 0px from left and 100px up in retina.

Upvotes: 1

Views: 699

Answers (2)

Tom
Tom

Reputation: 47

The positioning for the wrapper in retina mode is ccp(320,0). No idea why, but it works.

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES && [[UIScreen mainScreen] scale] == 2.00) {
        wrapper.position = ccp(320,0); //Retina
    } else {
        wrapper.position = ccp(160,240); //Normal
    }

Upvotes: 1

CodeSmile
CodeSmile

Reputation: 64477

The CCUIViewWrapper (at least in the form from the forum thread) does not use CC_CONTENT_SCALE_FACTOR(). That means it's not compatible with Retina displays.

You may be able to get around this by supplying properly scaled size and position, this may or may not work:

wrapper.contentSize = CGSizeMake(100 * CC_CONTENT_SCALE_FACTOR(), 
                                 100 * CC_CONTENT_SCALE_FACTOR());
wrapper.position = ccp(50 * CC_CONTENT_SCALE_FACTOR(), 
                       50 * CC_CONTENT_SCALE_FACTOR());

Upvotes: 0

Related Questions