macos 如何将子视图与父 NSView 的中心对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4681176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:36:08  来源:igfitidea点击:

How to align a subview to the center of a parent NSView

cocoamacos

提问by Akshay

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 添加到自定义 NSView 类。在创建图像视图时,我正在传递父容器的框架。

-(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 addSubViewmethod 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?

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

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.

任何帮助,将不胜感激。

回答by d11wtq

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(
  round((NSWidth([parentView bounds]) - NSWidth([subview frame])) / 2),
  round((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.

如果在调用之前需要居中的框架,initWithFrame:则只需使用上述逻辑来计算框架原点值。

回答by dengST30

Objective - Cin macOS Catalina , Version 10.15.3

Objective -macOS Catalina 中的C,版本 10.15.3

more readable @d11wtq's answer

更具可读性的@d11wtq 的回答

    CGFloat x = (NSWidth(parentView.bounds) - subviewWidth) * 0.5;
    CGFloat y = (NSHeight(parentView.bounds) - subviewHeight) * 0.5;
    CGRect f = CGRectMake(x, y, subviewWidth, subviewHeight);
    subview = [[NSView alloc] initWithFrame: f];

    subview.autoresizingMask = NSViewMinXMargin | NSViewMaxXMargin | NSViewMinYMargin | NSViewMaxYMargin;

回答by black_pearl

Swift 5@d11wtq's answer

斯威夫特 5@d11wtq 的回答

    let x = (parentView.bounds.width - subviewWidth) * 0.5
    let y = (parentView.bounds.height - subviewHeight) * 0.5
    let f = CGRect(x: x, y: y, width: subviewWidth, height: subviewHeight)
    let subview = NSView(frame: f)
    subview.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin ]

回答by user3301487

Try This. in .h

试试这个。在.h

@property (strong, nonatomic) IBOutlet UIView *CoinsPurchaseView;

@property (strong, nonatomic) IBOutlet UIView *CoinsPurchaseView;

in .m

在.m

self.CoinsPurchaseView.center = self.view.center;
    [self.view addSubview:self.CoinsPurchaseView];