2011-06-09 23 views
0

我有一个自定义视图包含NSImageView(myImageView)和NSButton(myButton)。当用户调整窗口大小时(使用鼠标单击并拖动窗口的右下角),myImageView和myButton被自动调整大小。但自定义视图与原始视图不同。如何解决它?请帮帮我。如何自动调整按钮

这里是我的代码:

在appDelegate.m

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { 
// Insert code here to initialize your application 
[window makeKeyWindow]; 
[window setFrameTopLeftPoint:NSMakePoint(0, 576)]; 
[window setFrame:NSMakeRect(0, 0, 768, 576) display:YES]; 

[window setBackgroundColor:[NSColor clearColor]]; 
[window center]; 

TestView *testView = [[TestView alloc]initWithFrame:NSMakeRect(0, 0, 768, 576)]; 
[testView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable]; 
[testView setAutoresizesSubviews:YES]; 
[window setContentView:testView];} 

在TestView.m

- (void)drawRect:(NSRect)dirtyRect { 
// Drawing code here. 
NSImageView *subView = [[NSImageView alloc]initWithFrame:NSMakeRect(0, 0, 768, 576)]; 
[subView setImage:[NSImage imageNamed:@"Copenhagen.jpg"]]; 
[subView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable]; 
[subView setImageScaling:NSImageScaleProportionallyDown]; 
[self addSubview:subView]; 
[subView release]; 

NSButton *subButton = [[NSButton alloc]initWithFrame:NSMakeRect(70, 100, 100, 40)]; 
[subButton setTitle:@"Click"]; 
[subButton setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable|NSViewMaxXMargin|NSViewMaxYMargin|NSViewMinXMargin|NSViewMinYMargin]; 
[self addSubview:subButton]; 
[subButton release]; 
[window setContentView:temp];} 

非常感谢。

+0

您是否为自定义视图设置了自动调整掩码?你可以在Interface Builder中或者通过' - [NSView setAutoresizingMask:]'来编程。 – 2011-06-09 06:54:40

+0

我已经为自定义视图myImageView和myButton设置了自动调整掩码,但myButton在自动调整时看起来像不像原始的那样 – user718408 2011-06-09 08:52:16

+0

我不确定我明白你的意思。也许截图会让你更容易理解你的问题。 – 2011-06-09 08:56:28

回答

0

您不希望左侧边距(NSViewMinXMargin)和底部边距(NSViewMinYMargin)变得更加灵活,所以您应该从按钮的自动调整掩码中移除这些边界。你可能不希望按钮本身被调整大小,因此,实际上,只使用

NSViewMaxXMargin | NSViewMaxYMargin 

作为自动调整掩码。

View Programming Guide是学习自动调整口罩的很好的参考。

+0

我已将其移除。但后来我调整了窗口的大小,它变成这样:[image](http://img01.imagecanon.com/img.php?view=231ff92b0178ab6b1fc3be653709b23a) – user718408 2011-06-09 10:13:39

+0

@user你想让按钮被调整大小吗?如果没有,从autoresizing掩码中删除'NSViewWidthSizable'和'NSViewHeightSizable'。 – 2011-06-09 10:15:14