对齐工具栏项目以拆分视图内容unsing自动布局约束 [英] Align Toolbar Item to split view content unsing Auto Layout Constraints

查看:163
本文介绍了对齐工具栏项目以拆分视图内容unsing自动布局约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将os x应用程序工具栏中的按钮与窗口中拆分视图的位置对齐,类似于Apple Mail中的删除按钮。如何使用自动布局约束?

I would like to align a button in the toolbar of an os x app to the position of a split view in the window, similar to the delete button in Apple Mail. How can this be done using auto layout constraints?

我在苹果文档中找到一个提示,根据它,应该可以使用自动布局约束(在 https://developer.apple.com/library/ios/文档/ userexperience / conceptual / AutolayoutPG / AutoLayoutConcepts / AutoLayoutConcepts.html ),其中它说,约束可以,有一些限制,跨越视图层次。在OS X中的邮件应用程序中,例如,默认情况下删除按钮在工具栏中与消息表。

I did find a hint in apples documentation according to which it should be possible to use auto layout constraints (at https://developer.apple.com/library/ios/documentation/userexperience/conceptual/AutolayoutPG/AutoLayoutConcepts/AutoLayoutConcepts.html), where it says that "Constraints can, with some restrictions, cross the view hierarchy. In the Mail app in OS X, for example, by default the Delete button in the toolbar lines up with the message table". But that is unfortunately all I could find.

我试图在左侧splitView窗格的内容和按钮之间创建一个约束:

I tried to create a constraint between the content of the left splitView pane and the button:

NSView *button = self.toolbarItemButton.view;
NSView *leftPane = self.leftSplitContent;

[self.window.contentView addConstraints:[NSLayoutConstraint
    constraintsWithVisualFormat:@"[leftPane][button]"
    options:0 metrics:nil
    views:NSDictionaryOfVariableBindings(leftPane, button)]
];

但我得到一个错误消息,似乎表明跨视图约束是不可能的:约束引用来自视图的子树外部的东西
有任何方法来做这个吗?

But I then get an error message that seems to indicate that cross-view constraints are not possible: "constraint references something from outside the subtree of the view" Is there any way to do this?

一个解决方法我发现是不使用约束,在按钮的左侧插入自定义视图(不是间隔符!)。我可以改变视图的大小来移动按钮...但是,这似乎更像是一个黑客。

A workaround I found was to not use constraints, but instead insert a custom view (not a spacer!) to the left of the button. I can then change the size of that view to move the button... However, this seems more like a hack to me.

推荐答案

p>我知道这有点老,但我有同样的问题,并找到一个答案:

I know it's a bit old, but I had the same issue and found an answer:

你必须使用 topLayoutGuide 属性。

Ex在swift中

view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormats(
        ["H:|[toolbarShadow]|", "V:[topLayoutGuide][toolbarShadow(100)]"], 
views: viewDictionnary))

当然 topLayoutGuide 必须在您的视图字典中

Of course topLayoutGuide must be in your view dictionary

您可以通过 self.topLayoutGuide

这篇关于对齐工具栏项目以拆分视图内容unsing自动布局约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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