【问题标题】:Cocoa resize window to fit screenCocoa 调整窗口大小以适应屏幕
【发布时间】:2015-07-02 17:19:52
【问题描述】:

我有 2 台显示器,我想让我的 videoWindow 被放置并缩放到第二台显示器的大小。我想以编程方式执行此操作,因为第二个显示器分辨率可能会改变。我能够将窗口放置在第二台显示器的左下角,但我无法将其缩放以适应。

这一行的警告:

[self.videoWindow setFrame: screenRect];

是:“NSWindow”可能无法响应“setFrame”

// inside my .h file
@property (assign) IBOutlet NSWindow *videoWindow;


// inside my .m file
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
    [super windowControllerDidLoadNib:aController];

    NSRect videoPreviewScreenRect;
    NSArray *screenArray = [NSScreen screens];

    //Using index of 1, to get secondary monitor
    NSScreen *videoPreviewScreen = [screenArray objectAtIndex: 1];

    NSRect screenRect = [videoPreviewScreen frame];
    videoPreviewScreenRect = [videoPreviewScreen visibleFrame];

    // Get and set the screen origin based on the second monitors origin
    NSPoint videoScreenOrigin ;
    videoScreenOrigin.x = videoPreviewScreenRect.origin.x;
    videoScreenOrigin.y = videoPreviewScreenRect.origin.y;
    [self.videoWindow setFrameOrigin: videoScreenOrigin];

    // **** THIS LINE DOESN'T WORK ****
    [self.videoWindow setFrame: screenRect];

    [self.videoWindow setBackgroundColor: NSColor.blackColor];
    [self.videoWindow display];
    [self.videoWindow makeKeyAndOrderFront:nil];
}

【问题讨论】:

    标签: objective-c macos cocoa


    【解决方案1】:

    我能够找出问题所在。

    [self.videoWindow setFrame: screenRect];
    

    需要改成这样:

    [[self videoWindow] setFrame:screenRect display:YES animate:NO]; 
    

    【讨论】:

      猜你喜欢
      • 2023-01-12
      • 2011-09-01
      • 2011-02-03
      • 2015-08-31
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      • 2020-06-07
      相关资源
      最近更新 更多