如何将子视图对齐到父NSView的中心 [英] How to align a subview to the center of a parent NSView

查看:567
本文介绍了如何将子视图对齐到父NSView的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个程序,其中我以编程方式添加一个NSImageView到自定义NSView类。在创建图像视图时,我传递父容器的框架。

   - (NSImageView *)loadNSImage: *)imageName frame:(NSRect)frame {
imageName = [[NSBundle mainBundle] pathForResource:imageName ofType:@png];
NSImage * image = [[NSImage alloc] initWithContentsOfFile:imageName];
NSImageView * imageView = [[NSImageView alloc] initWithFrame:frame];
[imageView setImage:image];
[imageView setImageScaling:NSImageScaleProportionallyUpOrDown];
[imageView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable | NSViewMaxXMargin | NSViewMaxYMargin | NSViewMinXMargin | NSViewMinYMargin];
[imageView setImageAlignment:NSImageAlignCenter];
[image release];
return imageView;
}

然后我使用 addSubView 方法添加到自定义视图。问题是图像粘在父视图的左下角。如何将此图像放置在父视图的中心?



我已经尝试添加一个偏移到框架的原点,但是这不是真的工作,当窗口



>解决方案

我不知道这是否是最好的方式,但我只是做一个简单的数学,当我想把一个子视图在其父中心。



如果您希望将边距保持居中,则您需要将所有边距设置为可自动调整大小。

 )]; 
[subview setAutoresizingMask:NSViewMinXMargin | NSViewMaxXMargin | NSViewMinYMargin | NSViewMaxYMargin];

这只是计算获得居中原点所需的边距。



如果您在调用 initWithFrame:之前需要中心框架,那么只需使用上述逻辑来计算框架原始值。


I'm developing a program in which I am programmatically adding an NSImageView to a custom NSView class. While creating the image-view, I am passing the frame of the parent container.

-(NSImageView *)loadNSImage:(NSString *)imageName frame:(NSRect)frame{
    imageName = [[NSBundle mainBundle] pathForResource:imageName ofType:@"png"];
    NSImage *image = [[NSImage alloc] initWithContentsOfFile:imageName]; 
    NSImageView *imageView = [[NSImageView alloc]initWithFrame:frame];
    [imageView setImage:image];
    [imageView setImageScaling:NSImageScaleProportionallyUpOrDown];
    [imageView setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable | NSViewMaxXMargin | NSViewMaxYMargin | NSViewMinXMargin | NSViewMinYMargin];
    [imageView setImageAlignment:NSImageAlignCenter];
    [image release];
    return imageView;
}

Then I am using the addSubView method to add this into the Custom View. The problem is that the image sticks to the bottom-left of the parent view. How can I place this image in the center of the parent view?

I have tried adding an offset to the frame origin, but that doesn't really work when the window is resized, or if an image with a different size is loaded.

Any help would be appreciated.

解决方案

I'm not sure if this is the best way to do it, but I just do simple math when I want to center a subview inside its parent.

You need to set all the margins to be auto-resizable if you want it to stay centered.

[subview setFrameOrigin:NSMakePoint(
  (NSWidth([parentView bounds]) - NSWidth([subview frame])) / 2,
  (NSHeight([parentView bounds]) - NSHeight([subview frame])) / 2
)];
[subview setAutoresizingMask:NSViewMinXMargin | NSViewMaxXMargin | NSViewMinYMargin | NSViewMaxYMargin];

This is just calculating the margins required to get a centered origin.

If you need the centered frame before you invoke initWithFrame: then just use the above logic to compute the frame origin values.

这篇关于如何将子视图对齐到父NSView的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆