开发者

How to autoresize the button

I have a custom view contain NSImageView (myImageView) and NSButton (myButton). When user resize window (using mouse click and drag in bottom right corner of window), myImageView and myButton is autoresized. But custom view is unlike开发者_JS百科 orginal. How to fix it? Please help me.

Here is my code:

In 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];}

In 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];}

Thanks a lot.


You don’t want the left margin (NSViewMinXMargin) and the bottom margin (NSViewMinYMargin) to be flexible, so you should remove those from the button’s autoresizing mask. You probably don’t want the button itself to be resized so, in fact, use only

NSViewMaxXMargin | NSViewMaxYMargin

as the autoresizing mask.

The View Programming Guide is a good reference to learn about autoresizing masks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜