Re: Merging scrolling/clipping with NSStackView
Re: Merging scrolling/clipping with NSStackView
- Subject: Re: Merging scrolling/clipping with NSStackView
- From: Jonathan Mitchell <email@hidden>
- Date: Thu, 19 Jan 2017 14:53:01 +0000
> On 19 Jan 2017, at 14:39, Daryle Walker <email@hidden> wrote:
>
> Right now, my window has a table view sitting on top of a text view. Those templates from Interface Builder have those NSViews surrounded by a clip view surrounded by a scroll view. But I want to flip that around; put the table and text views together in a stack view, and surround that with clip and scroll views as necessary.
>
> The stack view should be as wide as the window. The table and text views should be as wide as their stack view. The inner views should be as tall as they need to be. (The text view should be one line high if there's no text.) I'm thinking of putting a horizontal line between the inner views
>
> Any ideas how to do this?
>
Adding the subviews to the NSStackView should be standard fare.
However, I always find getting the constraints correctly configured painful.
The trick with stack views and multiple subviews, IMHO, is to ensure that the subviews correctly constrain their height.
You may also need to constrain the bottom of the last subview to the bottom of the stackview, depending on how you want things to lay out.
For scrolling I use an NSStackView subclass (https://github.com/mugginsoft/TSStackView) that includes the following to embed it in a scroll view.
Looks like I had to use a flipped NSClipView to get things to work out.
The subclass is getting on a bit so parts of it may be redundant if you are targeting the more recent versions of MacOS.
#pragma mark -
#pragma mark Embedding
- (NSScrollView *)scrollViewContainer
{
NSScrollView *scrollView = nil;
if (self.scrollViewAllocated) {
scrollView = [self enclosingScrollView];
}
else {
self.scrollViewAllocated = YES;
// allocate scroll view
scrollView = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
// allocate flipped clip view
TSClipView *clipView = [[TSClipView alloc] initWithFrame:scrollView.contentView.frame];
scrollView.contentView = clipView;
NSAssert(scrollView.contentView.isFlipped, @"ScrollView contenView must be flipped? Use TSClipView");
// configure the scrollview
scrollView.borderType = NSNoBorder;
scrollView.hasHorizontalScroller = YES;
scrollView.hasVerticalScroller = YES;
scrollView.autohidesScrollers = YES;
// stackview is the document
scrollView.documentView = self;
// constrain stackview to match dimension of scrollview
NSDictionary *viewsDict = NSDictionaryOfVariableBindings(self);
NSString *vfl = nil;
if (self.orientation == NSUserInterfaceLayoutOrientationVertical) {
vfl = @"H:|-0-[self]-0-|";
} else {
vfl = @"V:|-0-[self]-0-|";
}
self.stackViewConstraints = [NSLayoutConstraint constraintsWithVisualFormat:vfl options:0 metrics:nil views:viewsDict];
[scrollView addConstraints:self.stackViewConstraints];
}
return scrollView;
}
@interface TSClipView : NSClipView
@end
@implementation TSClipView
- (BOOL)isFlipped
{
return YES;
}
@end
HTH
Jonathan
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden