gpt4 book ai didi

xcode - 每次调整大小后,基于自动布局的 cocoa 分割 View 会恢复到旧大小

转载 作者:行者123 更新时间:2023-12-03 17:10:54 25 4
gpt4 key购买 nike

为了演示我的问题,我使用界面生成器创建了一个简单的测试应用程序,它是默认窗口,附加了默认 View Controller (链接到 XCode 中的 ViewController.m 类),并且 View Controller (在界面生成器中)包含垂直分割 View 。然后,该分割 View 的右 subview 链接到“IBOutlet NSView *right;”在ViewController.h:ViewController接口(interface)中。

最后,在 ViewController.m 中,我有以下代码:

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

- (void)viewDidLoad {
[super viewDidLoad];

// Do any additional setup after loading the view.
int numObjects = 3;
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0; i < numObjects; i++) {
NSTextField *label = [[NSTextField alloc] init];
[label setEditable:NO];
label.bezeled = NO;
label.editable = NO;
label.drawsBackground = NO;
[label setTranslatesAutoresizingMaskIntoConstraints:NO];
[label setAutoresizesSubviews:YES];
[label setStringValue:[NSString stringWithFormat:@"%d", i]];
[array addObject:label];
}

NSStackView *stackView = [NSStackView stackViewWithViews:array];
[stackView setTranslatesAutoresizingMaskIntoConstraints:NO];
[stackView setAutoresizesSubviews:YES];
stackView.orientation = NSUserInterfaceLayoutOrientationVertical;
[right addSubview:stackView];
[[self class] addConstraint:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual superview:right subview:stackView];
[[self class] addConstraint:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual superview:right subview:stackView];
for (int i = 0; i < [array count]; i++) {
NSView *view = [array objectAtIndex:i];
[[self class] addConstraint:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual superview:stackView subview:view];
[[self class] addConstraint:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual superview:stackView subview:view];
}
}

使用分割 View 的 handle 栏调整大小是可行的,万岁!现在,问题是,一旦我更改“int numObjects = 3;”对于任何大于 3 的数字(例如:“int numObjects = 4;”),调整大小行为会中断。现在,如果我拖动左/右半部分之间的分割 handle ,它会在我的鼠标按钮仍然按下时适当调整大小;但是,如果我松开鼠标按钮,分割 View 将恢复到其旧的(调整大小前)配置,右半部分是可能的最小尺寸。

我做错了什么?

最佳答案

我最终通过 google-fu 弄清楚了这一点:尝试找到正确的关键字来找到正确的 stackoverflow 解决方案。 This帖子解释了如何解决这个问题,基本上是通过将 Split View中右 Pane 的“保留优先级”从 250 更改为 251。

关于xcode - 每次调整大小后,基于自动布局的 cocoa 分割 View 会恢复到旧大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29564026/

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