NSMatrix绘制外界

时间:2014-02-15 20:20:36

标签: objective-c cocoa-touch cocoa

我有一个相当简单的自定义视图类,它包含在NSSplitView中。该视图类通过添加NSMatrix和几个虚拟单元来实现。真正奇怪的是,尽管它的大小足以填满它的超级视图,但实际上它比superview更高更宽。我试图记录框架和边界,结果也在下面,但我很难过。为什么不像我期望的那样对它的超视图进行调整?

Launches like this Looks like this when I resize

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        NSImageCell *prototypeCell = [[NSImageCell alloc] init];
        [prototypeCell setBordered:YES];

        _matrix = [[NSMatrix alloc] initWithFrame:self.bounds
                                             mode:NSHighlightModeMatrix
                                        prototype:prototypeCell
                                     numberOfRows:1
                                  numberOfColumns:1];

        [_matrix setCellSize:NSMakeSize(50, 50)];

        [_matrix setBackgroundColor:[NSColor redColor]];
        _matrix.drawsBackground = YES;

        [_matrix addRow];

        NSCell *cell = [_matrix cellAtRow:0 column:0];
        [cell setImage:[NSImage imageNamed:@"block.png"]];

        cell = [_matrix cellAtRow:1 column:0];
        [cell setImage:[NSImage imageNamed:@"block.png"]];

        [self addSubview:_matrix];

        NSLog(@"%@", NSStringFromRect(self.bounds));
        NSLog(@"%@", NSStringFromRect(self.frame));
        NSLog(@"%@", NSStringFromRect(_matrix.bounds));
        NSLog(@"%@", NSStringFromRect(_matrix.frame));
    }
    return self;
}

Output:
2014-02-15 15:16:23.458 Temp[5970:303] {{0, 0}, {45, 331}}
2014-02-15 15:16:23.459 Temp[5970:303] {{500, 0}, {45, 331}}
2014-02-15 15:16:23.459 Temp[5970:303] {{0, 0}, {45, 331}}
2014-02-15 15:16:23.459 Temp[5970:303] {{0, 0}, {45, 331}}

1 个答案:

答案 0 :(得分:0)

您尚未设置autolayout约束或旧式spring-n-struts以保持矩阵贴在其父视图的边缘。

我怀疑你是在运行状态恢复,所以在启动时,父视图正在创建一个大小,然后状态恢复,窗口/ splitView调整大小,子视图不再匹配其父级。

一般情况下,如果您自己创建视图,请立即添加约束(或者如果您不使用自动布局,请调用-setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable [在这种情况下]。)