在NSSplitView中嵌入的NSScrollView中的NSView的自动布局 [英] Auto Layout of a NSView in a NSScrollView embedded in a NSSplitView

查看:380
本文介绍了在NSSplitView中嵌入的NSScrollView中的NSView的自动布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚如何获取NSScrollView内的内容来正确缩放。我已经介绍了一些描述一般方法,但没有具体的参考。

I can't seem to figure out how to get the content inside a NSScrollView to scale properly. I have come across a few references which describe general approaches, but nothing concrete.

模糊布局

这似乎表明NSScrollView和Auto Layout不起作用很好在一起。它有点说明,iOS& OSX Auto布局指南示例是UIScrollView而不是NSScrollView:

This seems to indicate that NSScrollView and Auto Layout don't play nicely together. It is somewhat telling that both the iOS & OSX Auto Layout guide examples are UIScrollView instead of NSScrollView:

自动布局示例

我举一个例子。主窗口包含一个右侧有一些固定内容的分割视图,以及将在左侧以编程方式填充的视图。 (我会张贴图片,但我还没有所需的声誉)。运行下面的代码,任何移动分割条的尝试都会使其返回原始位置。

I threw together an example. The main window contains a split view with some fixed content on the right and a view that will be filled in programmatically on the left. (I would post a picture, but I don't have the required reputation yet). Running the code below, any attempt to move the splitter will result in it snapping back to the original position.

AppDelegate.h

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (weak) IBOutlet NSScrollView *scrollView;
@property (strong, nonatomic) NSView *containerView;
@property (strong, nonatomic) NSMutableDictionary *views;
@property (assign) IBOutlet NSWindow *window;

@end

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize scrollView;
@synthesize containerView;
@synthesize views;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

-(void)awakeFromNib {

// Create a container to hold all the subviews
containerView = [[NSView alloc] initWithFrame:NSZeroRect];
[containerView setTranslatesAutoresizingMaskIntoConstraints:NO];
[scrollView setDocumentView:containerView];

views = [[NSMutableDictionary alloc] init];

for (int i=0; i<8; i++) {

    NSBox *box = [[NSBox alloc] init];
    NSString *title = [NSString stringWithFormat:@"Box%@", [[NSNumber numberWithInt:i] stringValue]];
    box.title = title;
    [views setObject:box forKey:title];
    [box setTranslatesAutoresizingMaskIntoConstraints:NO];
    [containerView addSubview:box];
}

long height = 160;

NSArray *sortedKeys = [[views allKeys] sortedArrayUsingSelector: @selector(compare:)];
for(NSString *viewName in sortedKeys) {

    [containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[[NSString alloc] initWithFormat:@"|-20-[%@]-20-|", viewName] options:0 metrics:nil views:views]];
    [containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[[NSString alloc] initWithFormat:@"V:[%@(==%ld)]",viewName, height] options:0 metrics:nil views:views]];
}

// Build the subview-to-subview constraint string
NSString *constraintString = @"V:|";
for(NSString *viewName in sortedKeys) {
    constraintString = [constraintString stringByAppendingString:[NSString stringWithFormat:@"-20-[%@]",viewName]];
}

// Subview-to-subview constraints
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraintString options:0 metrics:nil views:views]];

// Container view constraints
NSDictionary *topLevelDictionary = NSDictionaryOfVariableBindings(containerView);
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[containerView]-0-|" options:0 metrics:nil views:topLevelDictionary]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[containerView]" options:0 metrics:nil views:topLevelDictionary]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[[NSString alloc] initWithFormat:@"V:|[containerView(==%ld)]", (long)(20 + height) * sortedKeys.count + 20] options:0 metrics:nil views:topLevelDictionary]];
}

@end

推荐答案

我有同样的问题。自动布局和 NSSplitView 非常好地在一起玩,你只需要牺牲正确的鸡。

I had this same issue. Autolayout and NSSplitView play very nicely together, you just need to sacrifice the right kind of chicken.

优先级略高于相关分割部分。图片显示我的最右侧分割将优先。

Try setting holding priority slightly higher on the relevant split portion. The image shows that my rightmost split will take priority.

另一个考虑因素。是否相关拆分宽度的内容受限?您可能需要在其宽度上设置大于或等于的约束,例如 | -0- [mysplitcontent(> = minwidth)] - 0- | code>

Also another consideration. Is the content of the relevant split width constrained? You may need to set a greater-than-or-equal constraint on its width e.g |-0-[mysplitcontent(>=minwidth)]-0-|

这篇关于在NSSplitView中嵌入的NSScrollView中的NSView的自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆