Mic*_*sen 5 cocoa objective-c nsview autolayout
我正在使用Autolayout让我的子视图正确调整大小时遇到麻烦.为了说明我的观点,我将一个简约的例子放在一起.
首先,我创建了一个新的NSViewController,并为其添加了一个子视图(在这种特殊情况下为NSTextView),并添加了Autolayout约束.

然后我为我的MainMenu.xib添加了一个自定义视图,并为此设置了Autolayout约束.

最后,我创建了一个视图控制器的实例,并将其视图放在我的自定义视图中.
#import "AppDelegate.h"
#import "MyViewController.h"
@interface AppDelegate()
@property (weak) IBOutlet NSView *customView;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
MyViewController *myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
[self.customView addSubview:myViewController.view];
myViewController.view.frame = self.customView.bounds;
}
@end
Run Code Online (Sandbox Code Playgroud)
由于在两个xib文件中都选中了"自动调整子视图",因此我希望在调整主窗口大小时调整NSTextView的大小.但是,它只是留在原地.

我在这里错过了什么?这让我困惑了好几天了.
谢谢Michael Knudsen
以防万一其他人遇到同样的问题.我最终以这种方式解决了这个问题(感谢@SevenBits将我指向了这个方向).
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
MyViewController *myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
[self.customView addSubview:myViewController.view];
myViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[subView]|"
options:0
metrics:nil
views:@{@"subView" : myViewController.view}];
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[subView]|"
options:0
metrics:nil
views:@{@"subView" : myViewController.view}];
[self.customView addConstraints:verticalConstraints];
[self.customView addConstraints:horizontalConstraints];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2978 次 |
| 最近记录: |