Button above NSScrollView using autoLayout - cocoa

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]];

Solved by setting wantsLayer to YES on the scrollView itself.

Related

OSX AVPlayerView resizing when play starts

I have an AVPlayerView as the 2nd NSSplitViewItem in a NSSplitViewController. The 1st split item is a list of videos.
When I select a video, it plays in the AVPlayerView.
The problem is, after I call [self.videoView.player play]; my MediaViewController's view resizes depending on the size of the video being played.
I'm using StoryBoards and my MediaViewController on the canvas has the AVPlayerView Top/Bottom/Trailing/Leading set to hug the superview. This seems to work fine by letting the window be resized and the video adjusts size accordingly.
I don't understand at what point the view decides to resize itself and what to do to stop it.
I have set self.videoView.videoGravity = AVLayerVideoGravityResizeAspect;
What am I missing?
Thanks
EDIT ---------
It seems the problem is not with the video size, but an image overlay I have added to the videoPlayer contentOverlayView.
I add the overlay like this:
if (self.videoView.contentOverlayView.subviews.count == 0)
{
// Add an NSImageView subview to use for overlays
NSImageView *overlayIV = [[NSImageView alloc] initWithFrame:self.videoView.frame];
[overlayIV setTranslatesAutoresizingMaskIntoConstraints:NO];
[overlayIV setImageScaling:NSImageScaleProportionallyUpOrDown];
[self.videoView.contentOverlayView addSubview:overlayIV];
// Set constraints to hug parent
[self.videoView addConstraint:[NSLayoutConstraint constraintWithItem:overlayIV
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.videoView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0]];
[self.videoView addConstraint:[NSLayoutConstraint constraintWithItem:overlayIV
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.videoView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0]];
[self.videoView addConstraint:[NSLayoutConstraint constraintWithItem:overlayIV
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.videoView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0]];
[self.videoView addConstraint:[NSLayoutConstraint constraintWithItem:overlayIV
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.videoView
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0]];
}
Then when I add a new image overlay it sometimes resizes the window:
NSImageView *overlayIV = [self.videoView.contentOverlayView.subviews firstObject];
if (overlayIV)
{
if (image)
{
overlayIV.image = image;
}
else
{
overlayIV.image = nil;
}
}

uiscrollview autolayout refresh after move scroll

I have IBOutlet 2 property, scrollview and contentview, i create my view via xib, I Set all consraint inside contentview, now I need to put contentview inside scrollview, I try to do something like this:
[_scrollView addSubview:_scrollViewContent];
[_scrollView setContentSize:_scrollViewContent.frame.size];
_scrollView.translatesAutoresizingMaskIntoConstraints = NO;
_scrollViewContent.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *views = NSDictionaryOfVariableBindings(_scrollView, _scrollViewContent);
NSArray *scrollViewLabelConstraints = [NSLayoutConstraint constraintsWithVisualFormat:#"H:|[_scrollViewContent(_scrollView)]" options:0 metrics:nil views:views];
[_scrollView addConstraints:scrollViewLabelConstraints];
initially not work, contentview is a bit larger then scrollview, when I scroll horizontally I see the view in the correct way.
Where is the error?

Using Autolayout with NSScrollview

My application contains a main container view in which three different views(subviewA, subviewB and subviewC) are swapped in and out. Both subviewA and subviewB are to resize with the window and I do this as follows
NSView *myCurrentView = [_myCurrentViewController view];
if ([[_myCurrentViewController title] isEqualToString:subviewA] ||
[[_myCurrentViewController title] isEqualToString:subViewB]) {
[_myScrollView setHidden:YES];
[myCurrentView setTranslatesAutoresizingMaskIntoConstraints:NO];
[_mainWindowView addSubview: [_myCurrentViewController view]];
[_mainWindowView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"H:|[myCurrentView]|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(myCurrentView)]];
[_mainWindowView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"V:|[myCurrentView]|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(myCurrentView)]];
}
This works fine, however for subviewC i would like to have the horizontal size of the subview, resize with the window, but use scrolling for the vertical. So, in IB I added a NSScrollView as a subview of my mainWindowView. I hide the scroll view if subViewA or subviewB are selected, but I don't know how to properly configure the constraints for subviewC. I figured I could add horizontal constraints simliar to subviewA or subviewB, since I woulk like the view to resize with the window, however I don't know what to add for constraints in the vertical to allow scrolling, so far I have this
[[_myCurrentViewController view] setTranslatesAutoresizingMaskIntoConstraints:YES];
[_myScrollView setHidden:NO];
[_myScrollView setDocumentView:[_myCurrentViewController view]];
[_myScrollView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"H:|[myCurrentView]|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(myCurrentView)]];
Thanks

NSTextView background not resizing appropriately

I have an NSTextView inside an NSScrollView. The scroll view has the auto resize masks for height and width, so it changes size with the window it's in.
The text view is set up much in the way the apple documentation recommends here.
But no matter what settings I put on the text view, I cannot get the background color to resize along with the scroll view.
Here's a picture of what I'm dealing with:
The width works, but not the height.
Here's my setup code for the textview as it is for this picture:
NSTextView *view = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, scrollview.contentSize.width, scrollview.contentSize.height) textContainer:textContainer];
[view setMinSize:NSMakeSize(0.0, scrollview.contentSize.height)];
[view setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
[view setVerticallyResizable:YES];
[view setHorizontallyResizable:NO];
[view setAllowsDocumentBackgroundColorChange:YES];
[view setDrawsBackground:YES];
[view setAutomaticLinkDetectionEnabled:YES];
[view setAutoresizingMask:NSViewWidthSizable];
The white background colour isn’t that of the NSTextView, but rather that of its enclosing NSScrollView.
To change it, either select the NSScrollView in Interface Builder/Xcode 4. Or, to do it programmatically, use -[NSView encosingScrollView]:
NSScrollView *scrollView = [textView enclosingScrollView];
[scrollView setBackgroundColor:[NSColor blackColor]];

Use NSScrollView together with Cocos2d-mac (NSOpenGLView)

Has anybody used a NSScrollView to control scrolling using the cocos2d-mac framework?
After much struggling I managed to make UIScrollView work with cocos2d-ios. Any pointers to using NSScrollView together with a NSOpenGLView would be appreciated.
I finally managed to get a NSScrollView working within my cocos2d-mac Xib.
The trick is that you have to programmatically overlay the OpenGLView over the NSScrollView main view (leaving room for the scroll bars) by first setting up a fake view as the scrollView's documentView, and then removing the openGLView from its parent view and adding it again (so the OpenGLView is drawn over the ScrollView). You can do it as follows:
appDelegate = [[NSApplication sharedApplication] delegate];
// Set up NSScrollView with dummy empty view as big as the layer
NSRect rect = NSMakeRect(0, 0, myLayer.width, myLayer.height);
NSView *view = [[NSView alloc] initWithFrame:rect];
appDelegate.scrollView.documentView = view;
[view release];
// Push OpenGLView back to front
[appDelegate.glView removeFromSuperview];
[appDelegate.splitLeftView addSubview:appDelegate.glView];
And then, you should use the scroll bars events to update the myLayer position accordingly.

Resources