0
我將按鈕和圖像分配到相同的位置(0,0),但它們繪製在不同的位置。爲什麼會發生?NSImageView中的圖像和按鈕位置錯誤
這裏是我的代碼:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
[window makeKeyWindow];
[window setFrame:NSMakeRect(0, 0, 500, 468) display:YES];
//[window setFrame:[[NSScreen mainScreen]frame] display:YES];
[window setBackgroundColor:[NSColor clearColor]];
[window center];
[[window contentView] setAutoresizesSubviews:YES];
NSImageView *subView = [[NSImageView alloc]initWithFrame:NSMakeRect(0, 0, 500, 468)];
[subView setImage:[NSImage imageNamed:@"Flowers.jpg"]];
[subView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
[subView setAutoresizesSubviews:YES];
NSButton *subButton = [[NSButton alloc]initWithFrame:NSMakeRect(0, 0, 100, 40)];
[subButton setTitle:@"testing"];
[subButton setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable|NSViewMaxXMargin|NSViewMaxYMargin|NSViewMinXMargin|NSViewMinYMargin];
[subView addSubview:subButton];
[window setContentView:subView];
}
,這裏是建成後的結果:
你說「圖像沒有填滿圖像視圖的整個高度,因爲它具有不同的寬高比」,那麼我可以計算寬高比變成相同嗎? – user718408 2011-06-07 07:53:45
當然,儘管通過增加因子來劃分圖像尺寸可能會更簡單,從1開始(無變化),直到達到適合屏幕的尺寸。請記住考慮框架矩形和內容矩形之間的區別。框架矩形稍大。 NSWindow有方法在框架和內容矩形之間進行轉換。 – 2011-06-07 09:38:39
感謝您的幫助。我有另一個問題。現在(0,0)座標在左下角,我想要改變左上角的(0,0)座標。我可否? – user718408 2011-06-08 04:12:54