Cocoa 中的 BoxLayout
最初,我想问如何以编程方式使用 cocoa 创建用户界面,即不使用界面生成器。但似乎其他人已经问过这个问题 答案对我没有帮助。
所以我会问一个不同的问题,我希望这能间接帮助我回答上一个问题。这里是:
(QUESTION_START)
如何创建一个与 BoxLayout 类? (只需点击链接,该页面上的图像就说明了您需要了解的有关 BoxLayout 的所有信息。)
(QUESTION_END)
如果您能提供正确方向的帮助,我们将不胜感激!
有一些与问题相关的子任务,例如
“我如何询问用户界面元素(例如按钮)它想要多大”(在将其绘制到屏幕上之前)。要在屏幕上绘制它,您必须已经知道它的大小,不是吗?显然,界面构建器应用程序已经找到了一种方法来做到这一点。
我知道许多 Cocoa 开发人员认为尝试我想做的事情是一个愚蠢的想法。让我告诉你:我知道这个观点背后的理由。现在,在没有界面生成器的情况下布局控件很糟糕,因为在可可中没有任何东西可以接近布局管理器。但如果你认为我的问题很愚蠢,请不要回答。整个互联网充满了为什么你永远不想用 cocoa 代码创建 UI 的解释。
谢谢!
Originally, I wanted to ask how to create user interfaces with cocoa programmatically, i.e. without using interface builder. But it seems that someone else has already asked this question and the answers didn't help me.
So I'll ask a different question, that I hope will indirectly help me answer the previous question. Here it is:
(QUESTION_START)
How do I create an Objective C class that is equivalent in functionality with the BoxLayout class in Java? (Just click the link, the image on that page says everything you need to know about BoxLayout.)
(QUESTION_END)
Any help in the right direction will be appreciated!
There are a few sub tasks that are connected with the question, e.g.
"How do I ask a user interface element (e.g. a button) how large it wants to be" (before it has been drawn to the screen). To draw it on the screen you have to already know its size, don't you? Obviously, the interface builder application has figured out a way to do this.
I know that many Cocoa developers think it's a stupid idea to even try what I want to do. Let me tell you: I know the reasoning behind that opinion. Right now, laying out controls without interface builder sucks, because there is nothing that comes even close to a layout manager in cocoa. But if you think my question is stupid, please DONT answer. The whole internet is full of explanations why you would never want to create UIs with code in cocoa.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回答你的第一个问题有点困难并且相当复杂,所以我将首先深入探讨你的子问题:
当您创建一个 UI 元素时,您可以通过其构造函数 initWithFrame: 告诉它它应该有多大(以及它应该在哪里);或者您可以稍后通过其
setFrame:
方法设置其框架。然后它会将自己吸引到那个空间中。您可以通过其frame
方法获取元素的框架。考虑到这一点,假设
BoxLayout
类是某种控制器,您可以在其中添加 UI 元素,然后BoxLayout
控制器会将它们排列在一个某种NSView
上的网格(或其他)。我知道您并不是在寻找质疑动机的答案,但考虑到 BoxLayout 类与在 IB 中布局界面的复杂性,询问为什么要这样做似乎很重要。
Answering your first question is kind of difficult and fairly involved, so I'm going to dive into your subquestion first:
When you create a UI element, you tell it how big it should be (as well as where it should be) via its
initWithFrame:
constructor; or you can set its frame later via itssetFrame:
method. It will then draw itself into that space. You can get an element's frame via itsframe
method.With that in mind, a
BoxLayout
class would, hypothetically, be a controller of some sort in which you could add UI elements, and then theBoxLayout
controller would arrange them in a grid (or whatever) on anNSView
of some sort.I know you weren't looking for answers that questions motives, but given the complexity of a
BoxLayout
class vs. laying out the interface in IB, it seems relevant to ask why you want to do this.