我有一个相当简单的自定义视图类,它包含在一个NSSplitView中。该视图类通过添加一个NSMatrix和几个虚拟单元来实现自身。真正奇怪的是,尽管它的大小可以填充它的superview,但实际上它比superview要高得多,而且比superview要宽得多。我试着记录帧和边界,结果也在下面,但我很困惑。为什么这个尺寸不像我预期的那样是超级视图呢?


- (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}}发布于 2014-02-15 21:22:21
您还没有设置自动退出约束或老式弹簧-n-struts来使矩阵保持在其父视图的边缘。
我怀疑您正在运行状态恢复,因此在启动时父视图被创建为一个大小,然后状态恢复和窗口/ splitView调整大小,子视图不再匹配其父视图。
通常,如果您自己创建一个视图,立即添加约束(如果不使用autolayout,则调用-setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable )。
https://stackoverflow.com/questions/21803125
复制相似问题