Question

I have a Cocoa project using Autolayout, and I want to put a button (actually an NSPathControl) over a NSScrollView so that it stays in the same place on the screen even when the scrollView is scrolled.

Whenever I add the button to the scrollView's parent, it ends up behind the scrollView (even if I explicitly use addSubview:positioned:relativeTo:. How do I get it to float above the scrollView?

My fallback is to put it inside of the scrollView, turn on translatesAutoresizingMaskIntoConstraints and update the frame as the scrollView is scrolled. I would strongly prefer to use autolayout if possible though...

Edit: Here is the layout code for the button (the layout for content in the scrollView is quite complex):

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]];
Was it helpful?

Solution

Solved by setting wantsLayer to YES on the scrollView itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top