gpt4 book ai didi

cocoa - 使用 autoLayout 的 NSScrollView 上方的按钮

转载 作者:行者123 更新时间:2023-12-03 16:41:37 24 4
gpt4 key购买 nike

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

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

我的后备方法是将其放在scrollView内部,打开translatesAutoresizingMaskIntoConstraints并在scrollView滚动时更新框架。如果可能的话,我强烈希望使用自动布局...

编辑:这是按钮的布局代码(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]];

最佳答案

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

关于cocoa - 使用 autoLayout 的 NSScrollView 上方的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17904594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com