Bố cục có vẻ hơi không xác định. Khi nào view2 bắt đầu co lại thay vì khớp với kích thước của view1? Tôi giả định rằng các khung nhìn phải có cùng kích thước cho đến khi view1 đạt đến mức tối thiểu mềm. Tại thời điểm đó, view2 thay đổi kích thước cho đến khi nó đạt đến mức tối thiểu, và sau đó view1 thay đổi kích thước cho đến khi đạt đến mức tối thiểu.
Chúng tôi có thể có hành vi này bằng cách thêm các ưu tiên vào các hạn chế. Theo thứ tự tầm quan trọng chúng ta có:
- view1 và View2> = tối thiểu
- view1> = view1SoftMinimum
- view1 == View2
contraint 1 phải trên các ưu tiên cửa sổ thay đổi kích thước. Chúng tôi có thể làm cho nó được yêu cầu (đó là mặc định).
Ràng buộc 2 phải cao hơn 3, nhưng dưới NSLayoutPriorityDragThatCannotResizeWindow. Chúng tôi sẽ làm cho nó 480.
contraint 3 phải nằm dưới contraint 2, vì vậy chúng tôi làm cho nó 479.
Chúng tôi có thể bày tỏ tất cả những contraints trong một chuỗi định dạng hình ảnh, bạn có thể thêm
|[view1(>=view1Minimum,>[email protected],[email protected])][view2(>=view2Minimum)]|
Đây là mã tôi đã thử nghiệm với:
NSView *view1 = [[NSTextView alloc] initWithFrame:NSZeroRect];
NSView *view2 = [[NSTextView alloc] initWithFrame:NSZeroRect];
[view1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[view2 setTranslatesAutoresizingMaskIntoConstraints:NO];
NSView *contentView = [self.window contentView];
[contentView addSubview:view1];
[contentView addSubview:view2];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(view1, view2);
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view1]|" options:NSLayoutConstraintOrientationVertical metrics:NULL views:viewsDictionary]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view2]|" options:NSLayoutConstraintOrientationVertical metrics:NULL views:viewsDictionary]];
NSDictionary *metrics = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:300], @"view1SoftMinimum",
[NSNumber numberWithFloat:150], @"view1Minimum",
[NSNumber numberWithFloat:150], @"view2Minimum", nil];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[view1(>=view1Minimum,>[email protected],[email protected])]-[view2(>=view2Minimum)]|" options:0 metrics:metrics views:viewsDictionary]];