2012-12-12 206 views
2

我正在尝试使用Mac OS X 10.7的自动布局进行设计。在Mac OS X中自动布局和调整视图大小

我的应用程序有一个固定宽度/高度的窗口。 我真正想要做的是调整窗口中的控件大小。 例如,我有一个搜索字段,如果点击该字段,它应该展开&折叠。 我在左侧定位了一个NSPathControl,如果NSSearchField展开,它应该收缩,所以它们不会折叠。

现在,它的工作如果我调整窗口的大小,但如果我在代码中设置视图的框架,它不。 Autolayout只是忽略了这一点。我只是在做[[this animator] setFrame:newFrame];

有什么我必须在自动布局中启用,还是我必须在代码中设置不同的框架?

编辑

我得到的约束通过在搜索字段设置self.translatesAutoresizingMaskIntoConstraints = YES;工作。 我不确定这是否是一个好主意。

此外,它给了我以下日志:

2012-12-12 19:41:30.958 Expanding Search[1428:303] Unable to simultaneously satisfy constraints: 
(
    "<NSAutoresizingMaskLayoutConstraint:0x101a0d800 h=&-- v=&-- H:[ITSearchField:0x10011bb20]-(1)-| (Names: '|':ITPathbar:0x10190a670)>", 
    "<NSLayoutConstraint:0x10190c520 H:[ITSearchField:0x10011bb20]-(2)-| (Names: '|':ITPathbar:0x10190a670)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x10190c520 H:[ITSearchField:0x10011bb20]-(2)-| (Names: '|':ITPathbar:0x10190a670)> 

Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens. And/or, break on objc_exception_throw to catch this in the debugger. 
2012-12-12 19:41:32.835 Expanding Search[1428:303] 74 
2012-12-12 19:41:32.854 Expanding Search[1428:303] Unable to simultaneously satisfy constraints: 
(
    "<NSAutoresizingMaskLayoutConstraint:0x101a0d800 h=&-- v=&-- H:[ITSearchField:0x10011bb20]-(14.5)-| (Names: '|':ITPathbar:0x10190a670)>", 
    "<NSLayoutConstraint:0x10190c520 H:[ITSearchField:0x10011bb20]-(2)-| (Names: '|':ITPathbar:0x10190a670)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x10190c520 H:[ITSearchField:0x10011bb20]-(2)-| (Names: '|':ITPathbar:0x10190a670)> 

Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens. And/or, break on objc_exception_throw to catch this in the debugger. 
+0

据我进入自动布局,我明白你不应该调用'-setFrame:'。你能解释你如何设置约束条件吗? Cocoa如何分割搜索和路径控件之间的空间?他们总是在相同的比例?有什么限制让他们这样? – paulmelnikow

+0

@noa我有一个简单的窗口,灵活的高度和宽度。所以你可以调整窗口大小。然后我有一个自定义搜索栏,它可以改变它的宽度,固定在窗口的右上角。另外,我还有一个按钮,就像测试一样,固定在搜索栏的左侧。现在,我现在使用'setFrame:'设置宽度,并且我还必须将'translatesAutoresizingMaskIntoConstraints'设置为'YES' – NSAddict

+0

@noa好吧,我发现你可以设置'[[constraint animator] setConstant:newWidth];'它的工作原理。现在我只剩下一个问题了,我不确定如何才能真正获得宽度约束。我用'[self constraints]'得到了所有的约束条件,这是我例子中的第一个约束条件,但当然可以改变。我可以建立一个渠道,但这看起来不太聪明。 – NSAddict

回答

2

我的解决方案


我得到了它,我发表了它在Github上正确here

我还问了另一个更具体的问题,这个问题解决了here


事情是,你实际上不应该调用setFrame:当你使用autolayout。你应该得到正确的约束并改变它。它使它更容易,您可以简单地设置宽度,并自动为您处理帧原点。

相关问题