gpt4 book ai didi

cocoa - 如何使用 Cocoa Auto Layout 使自定义 View 随窗口调整大小?

转载 作者:行者123 更新时间:2023-12-03 16:00:15 25 4
gpt4 key购买 nike

我有一个窗口,其中有一个自定义 View ,我希望自定义 View 随窗口一起调整大小,以便它随时完全填满窗口。如果我写:

NSView *contentView = [self.window contentView];
CustomView *customView = [[CustomView alloc] initWithFrame:[contentView bounds]];
[contentView addSubview:customView];
[contentView addConstraint:
[NSLayoutConstraint constraintWithItem:customView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeWidth
multiplier:1
constant:0]];
[contentView addConstraint:
[NSLayoutConstraint constraintWithItem:customView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeHeight
multiplier:1
constant:0]];

然后窗口不允许我调整它的大小。
如果我添加:

[customView setTranslatesAutoresizingMaskIntoConstraints:NO];

然后自定义 View 不会出现(drawRect:似乎永远不会被调用)。我尝试了不同的方法(包括视觉格式 @"|[customview]|"),但我总是遇到同样的问题。我知道可以使用旧的自动调整大小系统来完成:

[customView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];

但我想使用 Cocoa Auto Layout 系统,并且我想将它用于更复杂的情况(例如始终填充窗口的几个自定义 View )。

有谁知道出了什么问题以及我应该如何使用自动布局系统来获得我想要的结果?

最佳答案

使用自动布局,(至少)有三种可能的方法来约束 View ,使其占据整个窗口的内容 View ,并在适当时调整大小。

关于 super View 的视觉格式约束

NSView *contentView = [_window contentView];
MyView *customView = [[MyView alloc] initWithFrame:[contentView bounds]];
[customView setTranslatesAutoresizingMaskIntoConstraints:NO];

[contentView addSubview:customView];

NSDictionary *views = NSDictionaryOfVariableBindings(customView);

[contentView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[customView]|"
options:0
metrics:nil
views:views]];

[contentView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[customView]|"
options:0
metrics:nil
views:views]];

边缘的编程约束

(这应该等同于上面的视觉格式)

+ (void)addEdgeConstraint:(NSLayoutAttribute)edge superview:(NSView *)superview subview:(NSView *)subview {
[superview addConstraint:[NSLayoutConstraint constraintWithItem:subview
attribute:edge
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:edge
multiplier:1
constant:0]];
}

NSView *contentView = [_window contentView];
MyView *customView = [[MyView alloc] initWithFrame:[contentView bounds]];
[customView setTranslatesAutoresizingMaskIntoConstraints:NO];

[contentView addSubview:customView];

[[self class] addEdgeConstraint:NSLayoutAttributeLeft superview:contentView subview:customView];
[[self class] addEdgeConstraint:NSLayoutAttributeRight superview:contentView subview:customView];
[[self class] addEdgeConstraint:NSLayoutAttributeTop superview:contentView subview:customView];
[[self class] addEdgeConstraint:NSLayoutAttributeBottom superview:contentView subview:customView];

大小的编程约束

NSView *contentView = [_window contentView];
MyView *customView = [[MyView alloc] initWithFrame:[contentView bounds]];
[customView setTranslatesAutoresizingMaskIntoConstraints:NO];

[contentView addSubview:customView];

[contentView addConstraint:
[NSLayoutConstraint constraintWithItem:customView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeWidth
multiplier:1
constant:0]];
[contentView addConstraint:
[NSLayoutConstraint constraintWithItem:customView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeHeight
multiplier:1
constant:0]];
<小时/>

第三种方法是问题中列出的方法,如果有进一步的限制,它可能不起作用。例如,没有:

[customView setTranslatesAutoresizingMaskIntoConstraints:NO];

还应用了原始的自动调整大小掩码,这导致了问题中描述的行为:窗口未调整大小。

正如 Regexident 提到的,您可以使用:

[_window visualizeConstraints:[contentView constraints]];

调试自动布局。还值得检查控制台输出。

关于cocoa - 如何使用 Cocoa Auto Layout 使自定义 View 随窗口调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8156799/

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