NSView自动调整大小以适应内容视图

时间:2012-12-06 11:29:20

标签: macos cocoa nsview

CustomView *customView = [[CustomView alloc] init];
[self.window.contentView addSubview:customView];

当窗口调整大小时,如何customView.frame.size.height始终等于contentView.frame.size.height customView.frame.size.width = 20;

2 个答案:

答案 0 :(得分:5)

设置自定义视图的 autoresizingMask

CustomView *customView = [[CustomView alloc] init];
customView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[self.window.contentView addSubview:customView];

答案 1 :(得分:4)

Swift版本,应用autoresizingMask:

    let customView = CustomView()
    customView.autoresizingMask = [.width, .height]
    self.window.contentView.addSubview(customView)

参考选项: https://developer.apple.com/documentation/appkit/nsview/autoresizingmask

相关问题