在NSSplitView中嵌入的NSScrollView中自动布局NSView

 网络营销论坛闯天涯 发布于 2023-02-07 18:47

我似乎无法弄清楚如何使NSScrollView内的内容正确扩展.我看到了一些描述一般方法的参考文献,但没有具体的内容.

模糊的布局

这似乎表明NSScrollView和Auto Layout不能很好地协同工作.有点说明iOS和OSX自动布局指南示例都是UIScrollView而不是NSScrollView:

自动布局示例

我把一个例子拼凑在一起.主窗口包含一个拆分视图,右侧有一些固定内容,一个视图将在左侧以编程方式填充.(我会张贴一张照片,但我还没有所需的声誉).运行下面的代码,任何移动分割器的尝试都会导致它回到原始位置.

AppDelegate.h

#import 

@interface AppDelegate : NSObject 
@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

任何方向将不胜感激.

1 个回答
  • 我有同样的问题.Autolayout和NSSplitView非常好地一起玩,你只需要牺牲正确的鸡肉.

    尝试在相关的分割部分设置稍高的保持优先级.图像显示我最右边的分割将优先考虑.

    在此输入图像描述

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

    2023-02-07 18:50 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有