gpt4 book ai didi

objective-c - UIWindow 创建 NSAutoresizingMaskLayoutConstraints 而不是 NSLayoutConstraint

转载 作者:搜寻专家 更新时间:2023-10-30 20:19:38 27 4
gpt4 key购买 nike

我创建了一个简单的 iOS 应用程序,它创建了一个带有 View 的 UIViewController。这是应用程序委托(delegate):

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] init];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

这是 UIViewController 中的代码:

- (void)viewDidLoad {
[super viewDidLoad];
self.view.translatesAutoresizingMaskIntoConstraints = NO;
}

- (void)viewDidAppear:(BOOL)animated {
NSLog(@"breakpoint here");
}

我在“此处断点”行上放置了一个断点。运行应用程序并在它进入调试器后输入:

(lldb) po [[UIWindow keyWindow] _autolayoutTrace]
$0 = 0x0a056d30
*<UIWindow:0x8078de0>
| *<UIView:0xa053fe0>

下面是 UIView 的水平约束:

(lldb) po [0xa053fe0 constraintsAffectingLayoutForAxis:0]
$1 = 0x0a05ba60 <__NSArrayM 0xa05ba60>(
<NSLayoutConstraint:0xa059560 H:|-(0)-[UIView:0xa053fe0](LTR) (Names: '|':UIWindow:0x8078de0 )>,
<NSLayoutConstraint:0xa0595e0 UIView:0xa053fe0.right == UIWindow:0x8078de0.right>,
<NSAutoresizingMaskLayoutConstraint:0x8571520 h=--- v=--- H:[UIWindow:0x8078de0(768)]>
)

所以这是一个简单的问题,以便我更好地理解。如果自动布局应该是新的东西而不是自动调整掩码,为什么 UIWindow 强制 NSAutoresizingMaskLayoutConstraint?我应该使用不同的初始化程序来使 UIWindow 使用常规约束吗?也许我的 UIWindow 上的 initWithFrame 是罪魁祸首?

谢谢!

最佳答案

默认情况下,UIWindow 和 Controller 的顶 View 是使用 translatesAutoresizingMaskIntoConstraints=YES 创建的,以帮助转换。来自 cocoa-dev :

By default, Xcode will set the value of translatesAutoresizingMaskIntoConstraints to YES for top level views, including those loaded for a view controller. The reason for this is also transitional: right now AppKit view subclasses usually expect subviews with translatesAutoresizingMaskIntoConstraints set to YES, and most applications are transitioning. Maybe some day in the future the default will be changed to NO.

您可以禁用使用代码,但您不能以 Apple 提供的方式更改此行为。您可以使用 swizzling 来完成它,但编写额外的行更为文明:

self.window.translatesAutoresizingMaskIntoConstraints = NO; // application:didFinishLaunchingWithOptions:
self.view.translatesAutoresizingMaskIntoConstraints = NO; // viewWillAppear:

关于objective-c - UIWindow 创建 NSAutoresizingMaskLayoutConstraints 而不是 NSLayoutConstraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15715690/

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