2

我有一个使用 Autolayout 的 Cocoa 项目,我想在 a 上放置一个按钮(实际上是一个NSPathControl),NSScrollView以便即使滚动 scrollView 时它也保持在屏幕上的同一个位置。

每当我将按钮添加到 scrollView 的父级时,它最终都会在 scrollView 后面(即使我明确使用addSubview:positioned:relativeTo:. 如何让它漂浮在 scrollView 上方?

我的后备是把它放在滚动视图中,在滚动视图滚动时打开translatesAutoresizingMaskIntoConstraints并更新框架。如果可能的话,我强烈希望使用自动布局......

编辑:这是按钮的布局代码(scrollView 中内容的布局非常复杂):

NSButton *button = [[NSButton alloc]initWithFrame:NSZeroRect];
button.translatesAutoresizingMaskIntoConstraints = NO;
button.wantsLayer = YES;//Added incase this affects ordering (it doesn't seem to make a difference)
[self.superview addSubview:button positioned:NSWindowAbove relativeTo:self];

[self.superview addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20]];
[self.superview addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:20]];
4

1 回答 1

1

通过在 scrollView 本身上将 wantLayer 设置为 YES 来解决。

于 2013-07-28T12:39:13.907 回答