我有一个自定义视图包含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];}
非常感谢。
您是否为自定义视图设置了自动调整掩码?你可以在Interface Builder中或者通过' - [NSView setAutoresizingMask:]'来编程。 – 2011-06-09 06:54:40
我已经为自定义视图myImageView和myButton设置了自动调整掩码,但myButton在自动调整时看起来像不像原始的那样 – user718408 2011-06-09 08:52:16
我不确定我明白你的意思。也许截图会让你更容易理解你的问题。 – 2011-06-09 08:56:28