如何在 OS X 上以编程方式强制初始调整 ImageWell 中图像的大小,因为 ImageWell 由于 LayoutConstraints 而改变大小

How on OS X to programmatically force initial adjustment the size of image in an ImageWell, as ImageWell changes size due to LayoutConstraints

NSWindow
  NSImageWell - with constraint to all four side of the Window's containerView
    NSImage - is put in Well via NSPageControl

我使用鼠标调整 Window 的大小,ImageWell 根据需要扩展或收缩 Window。

无论 Window 大小如何,Well 中的图像都保持相同大小。

如果我用鼠标稍微拖动图像,图像会根据 Window 的大小变化正确调整大小。

如何以及在哪里发送消息来更改此尺寸"automatically"?

我是否需要使用通知来监控 Window 中的尺寸变化?

对不起,我没说清楚。我希望图像中的图像能够很好地保持其 宽高比并在每次 Window 调整大小时调整自身大小。目前,当 window 调整大小时,用户看不到图像大小的变化。但是,他可以通过在图像或 Window 内容视图上单击拖动来正确调整图像大小。我希望用户看到这个 立即调整大小,无需使用鼠标。例如,纵横比为 4/3 的图像。如果 window 是 4" x 4" ,那么当添加图像时,它显示为 4" x 3"。如果 window 变为 8" x 8",则图像应自动变为 8" x 6"。

Guilherme 非常好,将他的演示 NSView 应用程序发给我。如果像在他的应用程序中一样,我在 IB 中设置了图像,我的应用程序不仅可以成功调整 ImageView 的大小,还可以成功调整图像本身的大小。

问题似乎是因为我通过 NSPageController 将图像放入 ImageView。调整图像大小的唯一方法是在 window.contentView

中用两根手指拖动

调整大小后 window:

<a href="https://www.dropbox.com/s/9sywqf1kpgl33yb/Screen%20Shot%202015-09-12%20at%2013.26.03.png?dl=0">New Image Appears</a>

然后用两根手指在图像上拖动:

<https://www.dropbox.com/s/d2he6bdzxbeefok/Screen%20Shot%202015-09-12%20at%2013.26.50.png?dl=0>

相关代码如下:

AppDelegate 如下:

#import "AppDelegate.h"
#import "MyImageView.h"

@interface AppDelegate ()
@property (strong, nonatomic) IBOutlet NSWindow *window;
@property (strong, nonatomic) IBOutlet NSPageController *pageController;
@property (strong, nonatomic) IBOutlet MyImageView *imageView;
@property (weak) IBOutlet NSTextField *infoLabel;
@property NSArray *imageArray;
@end

@implementation AppDelegate

#pragma mark Window delegate

- (void)windowDidResize:(NSNotification *)notification{
    //[_window layoutIfNeeded];
    //[_imageView doSelfLayout];
}

- (void)awakeFromNib {
    [_window setDelegate:self];
    [_imageView setTranslatesAutoresizingMaskIntoConstraints:NO];
    _imageArray = @[ [NSImage imageNamed:@"eggplant.png"],
                     [NSImage imageNamed:@"sandwich.png"],
                     [NSImage imageNamed:@"yoghurt.png"]];

    [_pageController setDelegate:self];
    [_pageController setArrangedObjects:_imageArray];
    [_pageController setTransitionStyle:NSPageControllerTransitionStyleStackBook];

    // Set info label's text
    NSString *info = [NSString stringWithFormat:@"Image %ld/%ld", ([_pageController selectedIndex]+1), [_imageArray count]];
    [_infoLabel setStringValue:info];
}

#pragma mark Page Controller delegate methods:

- (void)pageController:(NSPageController *)pageController didTransitionToObject:(id)object {
    /* When image is changed, update info label's text */
    NSString *info = [NSString stringWithFormat:@"Image %ld/%ld", ([_pageController selectedIndex]+1), [_imageArray count]];
    [_infoLabel setStringValue:info];
    //[_window layoutIfNeeded];
}

- (NSString *)pageController:(NSPageController *)pageController identifierForObject:(id)object {
    NSString *identifier = [[NSNumber numberWithInteger:[_imageArray indexOfObject:object]] stringValue];
    return identifier;
}

- (NSViewController *)pageController:(NSPageController *)pageController viewControllerForIdentifier:(NSString *)identifier {
    /* Create new view controller and image view */
    NSViewController *vController = [NSViewController new];
    NSImageView *iView = [[NSImageView alloc] initWithFrame:[_imageView frame]];
    [iView setImage:(NSImage *)[_imageArray objectAtIndex:[identifier integerValue]]];
    [iView setImageFrameStyle:NSImageFrameNone];

    /* Add image view to view controller and return view controller */
    [vController setView:iView];
    return vController;
}
@end

您在 IB 中为 scaling 属性 选择了什么选项?

Here's a sample project,图像会自动调整大小并选择 Proportionally Up or Down

我在下面制作了一个演示 Cocoa 项目 zip 文件,其中包含我正在寻找的内容,即 OS X 应用程序:

- uses NSPageController in History mode to display an array of images (Portrait and Landscape) in a resizeable window
- imageView maintains imagefile aspect ratio as it automatically resizes
- images list can be traversed using a PreviousButton and a NextButton, or using a touch pad, with a two-finger swipe.

https://www.dropbox.com/s/7qgmg5dr1bxosqq/PageController3%203.zip?dl=0

这是非常基本的东西,但我 post 希望它能在某个时候与其他人相同。马克