如何使用Corona Enterprise在iOS上原生更改窗口颜色



我是Corona Enterprise和Objective C的新手,但在没有Corona的情况下,我已经得到了我想要的XCode。现在,当我试图把它放在Corona项目中时,它没有很好地工作。。。

我把我的代码减少到了最小值,只把窗口颜色设置为红色。结果是,窗口变成了红色,但在XCode模拟器上只持续了不到一秒钟。之后,它又变成了黑色。。。

这是我的AppDelegate代码:

showLoading( lua_State *L )
{
    void *platformContext = CoronaLuaGetContext( L ); // lua_touserdata( L, lua_upvalueindex( 1 ) );
    id<CoronaRuntime> runtime = (id<CoronaRuntime>)platformContext;
    runtime.appWindow.backgroundColor = [UIColor redColor];
    // Return 0 since this Lua function does not return any values.
    return 0;
}
- (void)willLoadMain:(id<CoronaRuntime>)runtime
{
    lua_State *L = runtime.L;
    const luaL_Reg kFunctions[] =
    {
        "showLoading", showLoading,
        NULL, NULL
    };
    luaL_register( L, "window", kFunctions );
    lua_pop( L, 1 );
}

在main.lua上,我只打电话给:

window.showLoading()

我做错了什么?我只需要理解这一点,以便在窗口中显示我想要的内容。我尝试了runtime.appViewController.view.backgroundColor,但屏幕仍然显示黑色而不是红色…

好的,我在等待答案时发现了问题所在。以下是AppDelegate中的新代码:

showLoading( lua_State *L )
{
    void *platformContext = CoronaLuaGetContext( L );
    id<CoronaRuntime> runtime = (id<CoronaRuntime>)platformContext;
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    if (!runtime.appViewController.view)
        runtime.appViewController.view=[[UIView alloc] initWithFrame:screenRect];
    runtime.appViewController.view.backgroundColor = [UIColor redColor];
    // Return 0 since this Lua function does not return any values.
    return 0;
}

一开始,我以为runtime.appViewController.view已经初始化了。现在我发现事实并非如此。但是,如果我向main.lua添加任何元素,它就会被初始化,因此,如果它仍然是nil,就需要if来初始化它。

最新更新