添加子视图时访问父视图

发布于 2024-12-19 10:26:27 字数 544 浏览 2 评论 0原文

我有一个 ViewController,其中有 2 个不同大小的 NSView。我想将自定义 ViewController 的视图作为子视图添加到这两个 NSView 中,并动态调整大小以填充(两个父视图)。我可以在主布局的实现文件中很好地完成此操作,但它的代码很多。我想让我的自定义 ViewController 完成所有工作。为此,我需要知道要添加自定义视图的视图的高度和宽度。有一个parentViewController属性,但它对我没有任何作用。有没有办法引用正在添加视图的视图?

在我的自定义 ViewController viewDidLoad 方法中,我希望能够拥有

[self.view setFrame:CGRectMake(0, 0, 
    self.parentViewController.view.frame.size.width, 
    self.parentViewController.view.frame.size.height)];

,但这里的高度和宽度都是零。

我已经研究了几个小时的文档,但我仍然很困惑。任何帮助将不胜感激。

I have a ViewController that has 2 NSViews in it of different sizes. I'm wanting to add the view of a custom ViewController as a subView to both of these NSViews and have it dynamically size to fill (the 2 parent views). I can accomplish this just fine in the implementation file for the main layout, but its a lot of code. I would like to instead have my custom ViewController do all the work. To do that, I need to know the height and width of the view that I am adding my custom view to. There is a parentViewController property, but it isn't doing anything for me. Is there a way to reference the view that a view is being added to?

In my custom ViewController viewDidLoad method I would like to be able to have

[self.view setFrame:CGRectMake(0, 0, 
    self.parentViewController.view.frame.size.width, 
    self.parentViewController.view.frame.size.height)];

but the height and width are both nil here.

I've been digging through documentation for hours and I am still confused. Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初熏 2024-12-26 10:26:27

应该首先设置parentViewController 属性。如果没有设置则为零。您还没有指定它是否设置正确。不过,有一种更简单的方法可以做到这一点,假设您的视图的超级视图设置正确。试试这个:

[self.view setFrame:self.superview.frame];

或者如果您需要更改其中的某些内容,请从超级视图的框架中创建矩形。

The parentViewController property should be set in the first place. It will be nil if it is not set. You have not specified if it is set correctly. There is a simpler way to do this though, assuming that your view's superview is set correctly. Try this:

[self.view setFrame:self.superview.frame];

or make the rect from the superview's frame if you need to alter something in there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文