cocos2d-x 顯示IOS 上方 狀態列
https://www.v2ex.com/t/239103
需求
我現在需要讓我的 cocos2d-x 遊戲一直顯示白底黑字的狀態條。
實現
根據這篇文章,我的做法如下:
Info.plist
View controller-based status bar appearanceNOStatus bar is initially hiddenNOStatus bar styleGray style (default)
AppController.mm
在
frameworks/runtime-src/proj.ios_mac/ios/AppController.mm 中的application:didFinishLaunchingWithOptions: 添加如下代碼(完整代碼見後面)。[[UIApplication sharedApplication].statusBarStyle:UIStatusBarStyleDefault];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
// The OpenGLWindow
_window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
RootViewController.mm
- (BOOL)prefersStatusBarHidden
{
    return NO;
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}
問題
由於上提到的參考 文章 是顯示的黑底白字的狀態條,然而我的需求是白底黑字。
最終產生的結果如下:
最終產生的結果如下:
上面的黑色部分其實就是狀態條,只是背景(由於窗口被下移,沒有繪製那塊區域)也是黑色的,所以就黑成一團了。
cocos2d-x 提供的模板,是這樣創建 
_window 的:_window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
我根據上面的文章,將窗口下移,我修改後的代碼:
_window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
UI 是這樣的結構(完整代碼見『附』):
如上圖,黃色為標識區域為 StatusBar
紅色標識區域為_window
_window內部為 [_window]---rootViewController--> [viewController] ---view--> [eaglView]
eaglView 是最終 cocos2d-x 繪圖的區域,他填滿整個 _window。
現在我想要的結果是讓上圖的黑色區域底色變成白色,由於我不懂 iOS ,我猜想的方案可能是:
- 用 applicationFrame 創建窗口;新加一個和 
_window平級的白色 UIView ,放到黑色區域。 - 或者用 bounds 創建窗口,弄一個固定高度為 StatusBar 高度的 UIView 將 
eaglView『擠』下去。 - 或者能不能設置整個應用的底色為白色?
 
大致就是這些,請大家指點下我,最好有代碼。我不熟悉 iOS 開發。
感謝了!
附
application:didFinishLaunchingWithOptions 完整代碼:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    cocos2d::Application *app = cocos2d::Application::getInstance();
    app->initGLContextAttrs();
    cocos2d::GLViewImpl::convertAttrs();
    // Override point for customization after application launch.
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    // Add the view controller's view to the window and display.
    _window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
    CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [self.window bounds]
                                     pixelFormat: (NSString*)cocos2d::GLViewImpl::_pixelFormat
                                     depthFormat: cocos2d::GLViewImpl::_depthFormat
                              preserveBackbuffer: NO
                                      sharegroup: nil
                                   multiSampling: NO
                                 numberOfSamples: 0 ];
    [eaglView setMultipleTouchEnabled:YES];
    // Use RootViewController manage CCEAGLView
    viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
    viewController.wantsFullScreenLayout = YES;
    viewController.view = eaglView;
    // Set RootViewController to window
    if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
    {
        // warning: addSubView doesn't work on iOS6
        [self.window addSubview: viewController.view];
    }
    else
    {
        // use this method on ios6
        [_window setRootViewController:viewController];
    }
    [_window makeKeyAndVisible];
    // IMPORTANT: Setting the GLView should be done after creating the RootViewController
    cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView);
    cocos2d::Director::getInstance()->setOpenGLView(glview);
    app->run();
    return YES;
}
沒有留言:
張貼留言