为什么Swift不能正确地加载此视图,而Objective-C却可以呢?

时间:2018-12-13 10:03:52

标签: swift uiview scale frame uiwindow

我尝试在Swift中编程,但是我无法执行一个简单的程序。只需几行代码即可创建一个具有空视图的窗口。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.


    self.window = UIWindow(frame: CGRect(x: 0.0, y: 0.0, width: 640.0, height: 960.0))

    let viewController = UIViewController()

    let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 640.0, height: 960.0))

    view.backgroundColor = UIColor.white

    viewController.view = view

    self.window?.rootViewController = viewController

    self.window?.makeKeyAndVisible()

    return true

}

此代码产生的视图无法填充屏幕。不幸的是,我尝试了屏幕,框架和比例尺的界限。

但是当我在Objective-C中尝试以下操作时,它按预期运行:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0.0, 0.0,640.0,960.0)];

UIViewController *viewController = [[UIViewController alloc] init];

UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0,640.0,960.0)];

[view setBackgroundColor:[UIColor whiteColor]];

viewController.view = view;

[self.window setRootViewController:viewController];

[self.window makeKeyAndVisible];

return YES;

}

1 个答案:

答案 0 :(得分:0)

我无法解释为什么Objective-C代码能按您期望的那样工作。但是我确实知道Swift代码有什么问题:

  

只需几行代码即可创建一个具有空视图的窗口

但是您要做的不是如何做到;这是您的代码,带有一些注解:

<Results>
    <ResultSet fetchSize="10">
        <Row rowNumber="1">
            <CUSTOMER_ID>1963</CUSTOMER_ID>
            <ID>444</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>vitae consectetuer eget rutrum</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
        <Row rowNumber="2">
            <CUSTOMER_ID>4389</CUSTOMER_ID>
            <ID>6008</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>neque</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
        <Row rowNumber="3">
            <CUSTOMER_ID>8836</CUSTOMER_ID>
            <ID>5304</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>lectus suspendisse potenti</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
        <Row rowNumber="4">
            <CUSTOMER_ID>7360</CUSTOMER_ID>
            <ID>8279</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>at turpis a</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
        <Row rowNumber="5">
            <CUSTOMER_ID>823</CUSTOMER_ID>
            <ID>235</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>non interdum</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
        <Row rowNumber="6">
            <CUSTOMER_ID>5430</CUSTOMER_ID>
            <ID>4278</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>leo maecenas pulvinar</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
        <Row rowNumber="7">
            <CUSTOMER_ID>9209</CUSTOMER_ID>
            <ID>9974</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>ut suscipit</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
        <Row rowNumber="8">
            <CUSTOMER_ID>8135</CUSTOMER_ID>
            <ID>8296</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>lectus pellentesque eget</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
        <Row rowNumber="9">
            <CUSTOMER_ID>8247</CUSTOMER_ID>
            <ID>4569</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>vulputate nonummy</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
        <Row rowNumber="10">
            <CUSTOMER_ID>7156</CUSTOMER_ID>
            <ID>836</ID>
            <COMM_PRODUCT_ID>229</COMM_PRODUCT_ID>
            <DESCRIPTION>adipiscing molestie</DESCRIPTION>
            <STATUS>1</STATUS>
        </Row>
    </ResultSet>
</Results>

因此,根据需要进行修改:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    self.window = UIWindow(frame: CGRect(x: 0.0, y: 0.0, width: 640.0, height: 960.0))
    // wrong; there is no need to frame the window
    // and if you do frame it, it must be framed to the actual screen size

    let viewController = UIViewController()
    let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 640.0, height: 960.0))
    view.backgroundColor = UIColor.white
    viewController.view = view
    // wrong; you must never assign a view controller a view like this...
    // except in the view controller's own `loadView` method

    self.window?.rootViewController = viewController
    self.window?.makeKeyAndVisible()
    return true
}

可以看到,一个没有故事板的最小正确构建的空窗口应用程序(或者至少,如果有故事板,它将忽略它)。