如何基于使用可可自动布局调整的优先级两个子视图? [英] How to resize two subviews based on priorities using Cocoa Autolayout?

查看:98
本文介绍了如何基于使用可可自动布局调整的优先级两个子视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我玩自动布局可可有些事情并不清楚我。

我有一个窗口2次。每个视图的宽度是父窗口的1/2的宽度。

  | | |
| | |
|视图1 |视图2 |
| | |
| | |

如果我调整窗口大小我想要的视图2先调整大小。

  | | |
| | |
|视图1 |视图2 |
| | |
| | |

在视图2达到其最小尺寸我想视图1被调整到它的最小尺寸。

  | | |
| | |
|视图1 |视图2 |
| | |
| | |

我怎么能这样做?


解决方案

布局似乎有点不确定。什么时候开始视图2,而不是萎缩配套厂景的大小?我假设,直到达到厂景软最小的意见应该是相同的大小。在这一点上,视图2调整大小,直到它到达其最小值,然后厂景大小调整,直到达到其最小值。

我们可以通过增加优先级约束上这种行为。按重要性排序,我们有:


  1. 厂景和视图2> =最小

  2. 厂景> = view1SoftMinimum

  3. 厂景==视图2

约束实现1必须在窗口调整大小优先以上。我们可以把它需要的(这是默认值)。

2的约束必须大于约束实现3,但低于NSLayoutPriorityDragThatCannotResizeWindow。我们将使它480。

3约束实现必须低于约束实现2,所以我们使它479.

我们可以前preSS都在一个视觉格式字符串的约束上,你可以添加

<$p$p><$c$c>|[view1(>=view1Minimum,>=view1SoftMinimum@480,==view2@479)][view2(>=view2Minimum)]|

下面是我测试过的code:

 的NSView *厂景= [[NSTextView页头] initWithFrame:方法NSZeroRect];
*的NSView =视图2 [NSTextView页头] initWithFrame:方法NSZeroRect];[厂景setTranslatesAutoresizingMaskIntoConstraints:NO];
[视图2 setTranslatesAutoresizingMaskIntoConstraints:NO];*的NSView =内容查看[self.window内容查看][内容查看addSubview:视图1];
[内容查看addSubview:视图2];*的NSDictionary = viewsDictionary NSDictionaryOfVariableBindings(视图1,视图2);[内容查看addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@V:| [视图1] |选项​​:NSLayoutConstraintOrientationVertical指标:空的观点:viewsDictionary]];
[内容查看addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@V:| [视图2] |选项​​:NSLayoutConstraintOrientationVertical指标:空的观点:viewsDictionary]];
*的NSDictionary指标= [NSDictionary的dictionaryWithObjectsAndKeys:
                         [NSNumber的numberWithFloat:300],@view1SoftMinimum
                         [NSNumber的numberWithFloat:150],@view1Minimum
                         [NSNumber的numberWithFloat:150],@view2Minimum零][内容查看addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@\"|[view1(>=view1Minimum,>=view1SoftMinimum@480,==view2@479)]-[view2(>=view2Minimum)]|\"选项​​:0指标:指标的观点:viewsDictionary]];

I am playing with Autolayout in Cocoa and some things are not clear for me.

I have 2 views on a window. The width of each view is 1/2 width of the parent window.

|           |           |
|           |           |
|   View1   |   View2   |
|           |           |
|           |           |

If I resize a window I want the View2 to resize first.

|           |     |
|           |     |
|   View1   |View2|
|           |     |
|           |     |

When View2 reaches its minimal size I want View1 to be resized to its minimal size.

|     |     |
|     |     |
|View1|View2|
|     |     |
|     |     |

How can I do that?

解决方案

The layout seems a bit unspecified. When does view2 start shrinking instead of matching view1's size? I am assuming that the views should be the same size until view1 reaches a soft minimum. At that point, view2 resizes until it reaches its minimum, and then view1 resizes until it reaches its minimum.

We can have this behavior by adding priorities to contraints. In order of importance we have:

  1. view1 and view2 >= minimum
  2. view1 >= view1SoftMinimum
  3. view1 == view2

Contraint 1 must be above the window resizing priority. We can make it required (which is the default).

Constraint 2 must be above contraint 3, but below NSLayoutPriorityDragThatCannotResizeWindow. We will make it 480.

Contraint 3 must be below contraint 2, so we make it 479.

We can express all of those contraints in one visual format string, which you can add

|[view1(>=view1Minimum,>=view1SoftMinimum@480,==view2@479)][view2(>=view2Minimum)]|

Here is the code I tested with:

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,>=view1SoftMinimum@480,==view2@479)]-[view2(>=view2Minimum)]|" options:0 metrics:metrics views:viewsDictionary]];

这篇关于如何基于使用可可自动布局调整的优先级两个子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆