4

プログラムでNSScrollView内にNSTableViewを作成していますが、フレームをスクロールビューに設定しようとすると、制約エラーが発生します。

Unable to simultaneously satisfy constraints:
(
    "<NSAutoresizingMaskLayoutConstraint:0x107d4ec50 h=--& v=--& V:[NSScrollView:0x10068c570(372.5)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x107d4d020 h=-&- v=-&- V:[NSClipView:0x10068d7f0]-(673)-|   (Names: '|':NSScrollView:0x10068c570 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x107d4cfc0 h=-&- v=-&- V:|-(2)-[NSClipView:0x10068d7f0]   (Names: '|':NSScrollView:0x10068c570 )>"
)

Will attempt to recover by breaking constraint 
<NSAutoresizingMaskLayoutConstraint:0x107d4d020 h=-&- v=-&- V:[NSClipView:0x10068d7f0]-(673)-|   (Names: '|':NSScrollView:0x10068c570 )>

Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens.  And/or, break on objc_exception_throw to catch this in the debugger.

自動的に作成されているようです。

これは私の作成コードです(アークを使用)私はNSViewをサブクラス化しています

-(void)addColumnsToTable{
    NSTableColumn *col = [[NSTableColumn alloc]initWithIdentifier:@"priority"];
    [col.headerCell setStringValue:@"priority"];
    [col setWidth:10];
    [col setMinWidth:1];
    [col setMaxWidth:10];
    [self.tableView addTableColumn:col];

   // col = [[NSTableColumn alloc]initWithIdentifier:@"name"];
   // [self.tableView addTableColumn:col];   
}


-(void)createTable{
    NSTableView *tableView = [[NSTableView alloc]initWithFrame:NSZeroRect];

    [tableView setDelegate:self];
    [tableView setDataSource:self];

    NSScrollView *scrollView = [[NSScrollView alloc]initWithFrame:self.bounds];
    [scrollView setBorderType:NSGrooveBorder];

    [self addSubview:scrollView];
    [scrollView setDocumentView:tableView];
    [scrollView setHasVerticalScroller:YES];
    [scrollView setAutohidesScrollers:YES];


    self.tableView = tableView;
    self.scrollView = scrollView;

    [self addColumnsToTable];

}

これが壊れているところです:

-(void)setFrame:(NSRect)frameRect{
    [super setFrame:frameRect];
    [self.scrollView setFrame:self.bounds];

}

これらの自動制約をオフにする方法はありますか?

4

2 に答える 2

7

問題は、ビューの自動サイズ変更マスクに基づいて自動制約が作成されていることです。この動作を無効にするには:

[scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];

ここで、scrollViewはNSViewのサブビューです

于 2012-10-02T21:32:38.583 に答える
0

サブクラスのメソッド内でのframeを設定するときにも、まったく同じ問題が発生しました。をNOに設定する上記の解決策は、例外を取り除きます。しかし、私の状況では、動的なウィンドウがあります。確かに、私は計算して設定している新しいフレームサイズに従わなくなりました。NSScrollViewDrawRectNSViewTranslatesAutoresizingMaskIntoConstraintstableviewNSScrollView

しばらくの間、私はついに正しい解決策を思いつきました。問題は自動サイズ変更の設定ではなかったようですが、私のサブクラスのNSScrollView設定は動的カスタムビューの正しい解決策です。AutoresizesSubviewsNSView

- (void)awakeFromNib
{

[super awakeFromNib];   
[self setAutoresizesSubviews:NO];   // <- Here is the solution

    // …
}

- (void)drawRect:(NSRect)dirtyRect
{

[super drawRect:dirtyRect];

    // From DirtyRect (dimensions of the my subclass NSView)
    // Calculate the rectangle allowable for MyScrollView
    // and setting local NSRect mRect..

    [MyScrollView setFrame:mRect];      // no more ‘exception’ here
}

NSViewまた、プログラムで他のサブクラスを作成する必要があります。これにもニーズがNSScrollViewあり、frame設定が必要です。これらのオブジェクトにはメソッドがないため、サブクラスawakeFromNibのInitメソッドのように、他の場所に設定するだけです。NSView

- (id)init:(NSInteger)aSpecialTag
{

[self setAutoresizesSubviews:NO];   // <- Here is the solution

    _mTag = aSpecialTag;

    // …
}
于 2015-06-11T01:01:41.747 に答える