Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7006515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:29:16+00:00 2026-05-27T21:29:16+00:00

In Cocos2d, I have read that [[[[CCDirector sharedDirector] openGLView] window] addSubview:someButton]; will add a

  • 0

In Cocos2d, I have read that

[[[[CCDirector sharedDirector] openGLView] window] addSubview:someButton];

will add a UIView to the main window: http://www.cocos2d-iphone.org/forum/topic/3588

Yet in my code I have this:

- (void)onEnterTransitionDidFinish {
    UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    [[[[CCDirector sharedDirector] openGLView] window] addSubview:button];
}

But no button is visible. This method is part of a CCLayerColor which is the first scene presented in the app, if that matters. What am I doing wrong here? Thanks!

Edit: I can confirm that the buttons are being added to the windows subviews, because NSLogging

[[[[[CCDirector sharedDirector] openGLView] window] subviews] count]

shows a difference when I comment/uncomment the addSubview line. So why isn’t the button showing up?

Edit 2:

Thanks to @Marine, I have found that my problem is the way I am declaring my button; using buttonWithType: solved the problem, because this code works:

UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(100, 100, 100, 100)];
[[[[CCDirector sharedDirector] openGLView] window] addSubview:button];

Can someone explain to me why using this class method works and using initWithFrame: doesn’t? Because I would rather not create lots of autoreleased objects, and also because I have some UIButton subclasses that do NOT show up when I use the buttonWithType: method.

Edit 3 (Solution):

The reason why using anything but buttonWithType: doesn’t work is that if you do not use that method to create the button, the background color of the button is not set, and by default it is clearColor. Using [button setBackgroundColor:[UIColor redColor]] or any other color fixes the problem. Also, if using a custom button class like this one, you might need to manually call [button awakeFromNib] if the button isn’t connected to a nib.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:29:17+00:00Added an answer on May 27, 2026 at 9:29 pm

    I have posted the solution to the problem as an edit to the question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Cocos2D iPhone application that requires a set of CGRects overlaid on
ok i am new to coding and cocos2d i have this shooting code that
I am doing some r &d in cocos2d.I have one backgroundLayer and add one
i have a cocos2d world, and a sprite/body that is moving fast . when
Alright, here we go. I have a cocos2d app, and there are targets that
I'm making a game with cocos2d and I have a bunch of sprites that
I have a cocos2d hierarchy of layers that is very very deep. I want
I have an Xcode project (for an iPhone application) that uses cocos2d. I have
My statusbar orientation is landscape, I have a cocos2d view that only supports portrait
I have a small app that uses cocos2d to run through four levels of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.