macos Cocoa NSView 改变自动调整属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/843683/
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 06:08:43  来源:igfitidea点击:

Cocoa NSView changing autosizing properties

objective-ccocoamacos

提问by Ben Reeves

Using interface builder you can select the corners an object should stick to when resizing. How can you do this programatically?

使用界面构建器,您可以选择对象在调整大小时应坚持的角落。你怎么能以编程方式做到这一点?

Interface Builder

界面生成器

采纳答案by Martin Gordon

See the setAutoresizingMask:method of NSView and the associated resizing masks.

请参阅NSView的setAutoresizingMask:方法和相关的调整大小掩码

回答by e.James

I find that the autoresizingBitmasks are horribly named, so I use a category on NSView to make things a little more explicit:

我发现这些autoresizingBit掩码的名字很糟糕,所以我在 NSView 上使用了一个类别来使事情更加明确:

// MyNSViewCategory.h:
@interface NSView (myCustomMethods)

- (void)fixLeftEdge:(BOOL)fixed;
- (void)fixRightEdge:(BOOL)fixed;
- (void)fixTopEdge:(BOOL)fixed;
- (void)fixBottomEdge:(BOOL)fixed;
- (void)fixWidth:(BOOL)fixed;
- (void)fixHeight:(BOOL)fixed;

@end



// MyNSViewCategory.m:
@implementation NSView (myCustomMethods)

- (void)setAutoresizingBit:(unsigned int)bitMask toValue:(BOOL)set
{
    if (set)
    { [self setAutoresizingMask:([self autoresizingMask] | bitMask)]; }
    else
    { [self setAutoresizingMask:([self autoresizingMask] & ~bitMask)]; }
}

- (void)fixLeftEdge:(BOOL)fixed
{ [self setAutoresizingBit:NSViewMinXMargin toValue:!fixed]; }

- (void)fixRightEdge:(BOOL)fixed
{ [self setAutoresizingBit:NSViewMaxXMargin toValue:!fixed]; }

- (void)fixTopEdge:(BOOL)fixed
{ [self setAutoresizingBit:NSViewMinYMargin toValue:!fixed]; }

- (void)fixBottomEdge:(BOOL)fixed
{ [self setAutoresizingBit:NSViewMaxYMargin toValue:!fixed]; }

- (void)fixWidth:(BOOL)fixed
{ [self setAutoresizingBit:NSViewWidthSizable toValue:!fixed]; }

- (void)fixHeight:(BOOL)fixed
{ [self setAutoresizingBit:NSViewHeightSizable toValue:!fixed]; }

@end

Which can then be used as follows:

然后可以按如下方式使用:

[someView fixLeftEdge:YES];
[someView fixTopEdge:YES];
[someView fixWidth:NO];

回答by tjw

Each view has the mask of flags, controlled by setting a the autoresizingMask property with the OR of the behaviors you want from the resizing masks. In addition, the superview needs to be configured to resize its subviews.

每个视图都有标志掩码,通过设置 autoresizingMask 属性与您想要从调整大小掩码中的行为的 OR 来控制。此外,还需要配置 superview 以调整其 subviews 的大小

Finally, in addition to the basic mask-defined resizing options, you can fully control the layout of subviews by implementing -resizeSubviewsWithOldSize:

最后,除了基本的 mask-defined 调整大小选项之外,您还可以通过实现-resizeSubviewsWithOldSize来完全控制子视图的布局

回答by Mazyod

@e.James answer gave me an idea to simply create a new enum with more familiar naming:

@e.James 的回答给了我一个想法,只需创建一个具有更熟悉命名的新枚举:

typedef NS_OPTIONS(NSUInteger, NSViewAutoresizing) {
    NSViewAutoresizingNone                 = NSViewNotSizable,
    NSViewAutoresizingFlexibleLeftMargin   = NSViewMinXMargin,
    NSViewAutoresizingFlexibleWidth        = NSViewWidthSizable,
    NSViewAutoresizingFlexibleRightMargin  = NSViewMaxXMargin,
    NSViewAutoresizingFlexibleTopMargin    = NSViewMaxYMargin,
    NSViewAutoresizingFlexibleHeight       = NSViewHeightSizable,
    NSViewAutoresizingFlexibleBottomMargin = NSViewMinYMargin
};

Also, from my research, I discovered that @James.s has a serious bug in the NSView additions.The coordinate system in Cocoa has a flipped y-axis in terms of iOS coordinates system. Hence, in order to fix the bottom and top margin, you should write:

此外,从我的研究中,我发现 @James.s在 NSView 添加中存在严重错误。Cocoa 中的坐标系在 iOS 坐标系方面有一个翻转的 y 轴。因此,为了固定底部和顶部边距,您应该编写:

- (void)fixTopEdge:(BOOL)fixed
{ [self setAutoresizingBit:NSViewMaxYMargin toValue:!fixed]; }

- (void)fixBottomEdge:(BOOL)fixed
{ [self setAutoresizingBit:NSViewMinYMargin toValue:!fixed]; }

From the cocoa docs:

来自可可文档:

NSViewMinYMargin

The bottom margin between the receiver and its superview is flexible.
Available in OS X v10.0 and later.

NSViewMinYMargin

The bottom margin between the receiver and its superview is flexible.
Available in OS X v10.0 and later.