我在 Xcode 11 beta 中遇到了一个问题。
问题是我没有window在AppDelegate文件中声明默认变量。
有人面临同样的问题吗?
升级 Xcode 后,我的应用程序的一个关键部分停止工作。
当我的应用程序启动时,我运行一个函数来检查布尔标志并设置正确的 rootViewController。
但是我用来设置它的代码现在已经停止工作了
class func setLoginAsInitialViewContoller(window:UIWindow) {
print("SET LOGIN")
let storyboard = UIStoryboard(name: "Login", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "LoginViewController")
controller.modalPresentationStyle = .overFullScreen
window.rootViewController = controller
window.makeKeyAndVisible()
}
Run Code Online (Sandbox Code Playgroud)
特别是当应用程序获得倒数第二行时,window.rootViewController = controller它会因libc++abi.dylib: terminating with uncaught exception of type NSException错误而崩溃。
上面的函数在一个被Utilities.swift调用的类中,我正在从我的内部调用该函数AppDelegate.swift,如下所示:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var storyboard: UIStoryboard? = nil
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.isIdleTimerDisabled = true
Utilities.decideInitialViewController(window: self.window!) …Run Code Online (Sandbox Code Playgroud) 将游戏移植到 macOS Catalyst,但窗口很小。是否可以全屏启动?
这只是一个测试应用程序,只有一个AppDelegate类来创建我所做的就是创建一个基于Window的应用程序,将支持的方向设置为仅在info.plist中的格局,然后添加以下代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[application setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
// Override point for customization after application launch.
UIAlertView *test = [[UIAlertView alloc] initWithTitle:@"hu" message:@"hui" delegate:nil cancelButtonTitle:@"hi" otherButtonTitles:nil];
[test show];
[window makeKeyAndVisible];
NSLog(@"win %f - %f", window.bounds.size.width, window.bounds.size.height);
return YES;
}
Run Code Online (Sandbox Code Playgroud)
如果没有设置状态栏方向的第一行,即使界面的其余部分处于横向左侧,警报视图也会以纵向显示.
无论如何,Log仍然给出了这个:
win 768.000000 - 1024.000000
Run Code Online (Sandbox Code Playgroud)
这是错误的方式(因此当我在我的真实应用程序中添加子视图时,框架不正确)
苹果似乎已经在界面旋转方面变得非常糟糕,因为我只有问题,我不记得在iPhone上发生过这种情况,所以请有人告诉我如何解决这个问题.
我会给那些能够至少解释为什么会发生这种情况并希望提供解决方案的人提供500点声誉(这几乎不是我的声誉).
我正在构建一个使用多种类型屏幕的应用程序 - 所有这些都保证了自己的自定义视图控制器.我成功视图控制器,并通过重新分配的主要及其相关视图之间切换window的rootViewController,在我的应用程序代理类似下面的方法:
- (void)changeRootViewController:(NSString *)controllerName
{
if (controllerName == @"book") {
rootViewController = (UIViewController *)[[BookViewController alloc] init];
[self.window setRootViewController:rootViewController];
} else if (controllerName == @"something_else") {
// Use a different VC as roowViewController
}
}
Run Code Online (Sandbox Code Playgroud)
然而,我这样做的方式似乎不是最好的做法.我也不想使用a UINavigationController或a UITabBarController作为rootViewController.这是否是这样做的错误方式,如果是这样,我应该如何以不同的方式接近它?
我以为这会被覆盖在某个地方,但是(我觉得好像)我用Google搜索了它,找了相关的问题等等.抱歉,如果我错过了什么!
我创建了一个简单的模块,我将子视图添加到UIWindow.在模拟器ios 7(Xcode 5.1.1)中,我打印了self.windows,我得到:
<UIWindow: 0x8db1670; frame = (0 0; 768 1024); autoresize = W+H; gestureRecognizers = <NSArray: 0x8db1a70>; layer = <UIWindowLayer: 0x8db17d0>>
Run Code Online (Sandbox Code Playgroud)
但在ios8(Xcode 6 beta 6)上,我得到:
<UIWindow: 0x794320d0; frame = (0 0; 1024 768); gestureRecognizers = <NSArray: 0x79437a80>; layer = <UIWindowLayer: 0x7943c400>>
Run Code Online (Sandbox Code Playgroud)
问题是:为什么帧属性存在差异?
当我在iOS9中创建自定义UIWindow时,窗口会在屏幕上显示,但状态栏突然消失.
当窗口隐藏时,状态栏会再次出现.
下面是我在iOS9上使用Xcode7 beta5获得的2个截图.
这是我正在使用的代码(在iOS8上运行良好):
#define DEBUG_SHOWENV_HEIGHT 20.0f
@interface AppDelegate ()
@property (nonatomic) UIWindow* envWindow;
@end
-(UIWindow*)envWindow
{
if (_envWindow == nil)
{
// Create the window
_envWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0.0f, self.window.frame.size.height, self.window.frame.size.width, DEBUG_SHOWENV_HEIGHT)];
_envWindow.rootViewController = [[UIViewController alloc] init]; // added since iOS9 to avoid the assertion
_envWindow.userInteractionEnabled = NO;
_envWindow.windowLevel = UIWindowLevelStatusBar;
_envWindow.backgroundColor = [UIColor colorWithRed:0.243 green:0.471 blue:0.992 alpha:0.8];
// Make a label
UILabel* labelEnv = [[UILabel alloc] initWithFrame:CGRectMake(8.0f, 0.0f, _envWindow.bounds.size.width - 16.0f, DEBUG_SHOWENV_HEIGHT)];
labelEnv.font …Run Code Online (Sandbox Code Playgroud) 我正在学习如何在没有Interface Builder(即故事板,xib等)的情况下创建iOS应用程序.下面是我正在使用的基本应用程序委托类.问题是,显示时UIWindow不会耗尽设备的全屏(参见附件截图).这发生在我测试的所有设备和模拟器上.为什么不使用全屏?
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
lazy var window: UIWindow? = {
debugPrint("AppDelegate: Creating Window")
let screen: UIScreen = UIScreen.mainScreen()
let window: UIWindow = UIWindow.init(frame: screen.bounds)
window.backgroundColor = UIColor.whiteColor()
return window
}()
lazy var rootViewController: ShapesViewController = {
debugPrint("AppDelegate: Creating Root View Controller")
let rootViewController = ShapesViewController.init(nibName: nil, bundle: nil)
return rootViewController
}()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
debugPrint("AppDelegate: applicationWillEnterForeground")
window?.rootViewController = rootViewController
window?.makeKeyAndVisible()
return true
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用一个大约有 7/8 年历史的旧 Xcode 项目,我已将其迁移到 Objective-C 2.0 并支持 ARC。在我开始工作之前,该项目不支持自动布局(这是在发布之前)。我设法清除了在更现代的 Xcode IDE (10.2.1) 中运行它时遇到的许多错误/问题。出于测试目的,我已将此代码实现到AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height)];
[self.window setBackgroundColor:[UIColor redColor]];
self.window.rootViewController = [UIViewController new];
[self.window makeKeyAndVisible];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
这出于某种原因给了我这个:

这是当我在 iPhone X、iPhone XR 或 iPhone XS Max 上运行时的结果,看起来屏幕的顶部和底部边缘(安全区域)没有被拾取。当我调试视图层次结构时,它们也没有被选中,它只显示红色空间?当我将此代码放入新项目时,它工作正常并填满所有空间。我想知道因为这是一个旧项目,所以可能有一个构建设置限制了UIWindow扩展以允许支持新设备?
另外作为旁注,我确保该UIWindow对象没有在其他地方被操纵。
如何在用户调整窗口大小时收到通知:
NotificationCenter.default.addObserver(self, selector: #selector(function), name: NSWindowDidResizeNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)
在 Mac Catalyst 中不可用
uiwindow ×10
ios ×6
swift ×3
appdelegate ×2
ipad ×2
iphone ×2
mac-catalyst ×2
objective-c ×2
uikit ×2
xcode ×2
fullscreen ×1
interface ×1
ios13 ×1
ios7 ×1
ios8 ×1
ios9 ×1
rootview ×1
statusbar ×1
uiscreen ×1
xcode6 ×1