gpt4 book ai didi

swift - 为什么 Swift 没有正确加载这个 View ,而 Objective-C 却可以?

转载 作者:行者123 更新时间:2023-11-30 10:57:42 24 4
gpt4 key购买 nike

我尝试用 Swift 编程,但未能执行一个简单的程序。只需几行代码即可创建一个具有空 View 的窗口。

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

}

此代码生成的 View 未填满屏幕。我尝试了屏幕、框架和比例的边界,不幸的是失败了。

但是当我在 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;

}

最佳答案

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

Just few lines of code to create a window with an empty view

但是你所做的并不是如何去做,而是如何去做。这是您的代码,其中包含一些注入(inject)的注释:

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
}

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

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow()
let viewController = UIViewController()
viewController.view.backgroundColor = .white
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
return true
}

你看,这是一个最小的正确构造的空窗口应用程序,没有 Storyboard(或者至少,如果它有 Storyboard,它会忽略它)。

关于swift - 为什么 Swift 没有正确加载这个 View ,而 Objective-C 却可以?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53759307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com