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

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

问题描述

我似乎无法弄清楚如何让里面的内容一个NSScrollView正常规模。我所遇到的,它描述的一般方法的几个引用,但没有具体的。

暧昧布局

这似乎表明,NSScrollView和自动布局不很好地一起玩。这在某种程度上讲,无论是iOS和放大器; OSX自动布局指南的例子是UIScrollView的,而不是NSScrollView:

<一个href=\"https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/AutolayoutPG/AutoLayoutbyExample/AutoLayoutbyExample.html#//apple_ref/doc/uid/TP40010853-CH5-SW1\"相对=nofollow>自动布局

我扔在一起的例子。主窗口包含在右边一些固定的内容,这将在左边编程填写美景的拆分视图。 (我会张贴图片,但我没有必要的声誉还)。运行低于code,任何试图移动至分离器是会导致其回弹到原来的位置。

  AppDelegate.h#进口&LT;可可/ Cocoa.h&GT;@interface的AppDelegate:NSObject的&LT; NSApplicationDelegate&GT;
@property(弱)IBOutlet中NSScrollView *滚动视图;
@property(强,非原子)的NSView * containerView;
@property(强,非原子)的NSMutableDictionary *意见;
@property(分配)IBOutlet中NSWindow *窗口;@结束AppDelegate.m#进口AppDelegate.h@implementation的AppDelegate@synthesize滚动视图;
@synthesize containerView;
@synthesize意见; - (无效)的applicationDidFinishLaunching:(NSNotification *)aNotification
{
    //将code这里初始化应用程序
} - (无效){awakeFromNib//创建一个容器来容纳所有的子视图
containerView = [[的NSView页头] initWithFrame:方法NSZeroRect];
[containerView setTranslatesAutoresizingMaskIntoConstraints:NO];
[滚动视图setDocumentView:containerView];意见= [[的NSMutableDictionary的alloc]初始化];的for(int i = 0; I&LT; 8;我++){    NSBox *盒= [[NSBox的alloc]初始化];
    * NSString的标题= [的NSString stringWithFormat:@框%@,[[NSNumber的numberWithInt:我] stringValue的];
    box.title =称号;
    [观点的setObject:箱forKey:标题]
    [盒setTranslatesAutoresizingMaskIntoConstraints:NO];
    [containerView addSubview:箱];
}长个儿= 160;NSArray的*又如sortedKeys = [[观点allKeys] sortedArrayUsingSelector:@selector(比较:)]。
对于(的NSString *的viewName在又如sortedKeys){    [containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[[的NSString页头] initWithFormat:@| -20 - [%@] - 20- |的viewName]选项:0的指标:无观点:观点]];
    [containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[[的NSString页头] initWithFormat:@V:[%@(==%LD)的viewName,高度]选项:0的指标:无观点:观点]];
}//构建子视图到子视图约束字符串
* NSString的= constraintString @V:|;
对于(的NSString *的viewName在又如sortedKeys){
    constraintString = [constraintString stringByAppendingString:[的NSString stringWithFormat:@ - 20 - [%@]的viewName]];
}//子视图到子视图限制
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraintString选项:0的指标:无观点:观点]];//容器视图限制
*的NSDictionary = topLevelDictionary NSDictionaryOfVariableBindings(containerView);
[滚动视图addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@| -0- [containerView] -0- |选项​​:0的指标:无意见:topLevelDictionary]];
[滚动视图addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@V:| -0- [containerView]选项:0的指标:无意见:topLevelDictionary]];
[滚动视图addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[[的NSString页头] initWithFormat:@V:| [containerView(==%LD),(长)(20 +高)* sortedKeys.count + 20]选项:0指标:无意见:topLevelDictionary]];
}@结束

任何方向将大大AP preciated。


解决方案

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

尝试对有关拆分部分设置保持优先级略高。上图显示的最右边我的拆分将采取优先考虑。

还有另一个考虑。是相关分裂宽度的内容的限制?您可能需要设置的大于或 - 等于的约束在其宽度如 | -0- [mysplitcontent(大于=了minWidth)] - 0- |

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.

Ambiguous Layout

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:

Auto Layout Examples

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

Any direction would be greatly appreciated.

解决方案

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.

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

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

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