可可视图中按钮/文本字段的位置
我很抱歉问这样的问题,但我似乎无法从野生网络中找到答案。
我想找出自定义视图中 NSTextField
的位置,以便我可以通过编程方式在其下添加其他文本字段。 我不只是在 IB 中添加其他文本字段的原因是因为我需要能够动态创建它们。 NSTextField
链接到 IBOutlet
。
所以主要问题是:如何找到 NSTextField
的位置(或 NSButton
,atm 并不重要)< strong>在自定义视图中(我需要获取项目的坐标)?
I'm sorry to ask such a question, but I can't seem to find the answer for it from the wild web.
I want to find out the location of a NSTextField
in my custom view, so I can add other textfields under it programmatically.
The reason I don't just add other textfields in IB, is because I need to be able to dynamically create them.
The NSTextField
is linked to an IBOutlet
.
So the main question is: How do I find out the location of a NSTextField
(or NSButton
, it doesn't really matter atm) in a custom view (I need to get the coordinates of the item)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 NSTextField 创建一个实例变量,然后对其调用这些方法以获取所需的四个值。
Make an instance Variable with the NSTextField and then call these methods on it to get the four values you want.
继承自
NSView
的每种视图或控件(例如NSButton
、NSTextField
等)都有一个-frame
包含定位信息的方法。如果您的自定义视图有一个连接到
NSTextField
的IBOutlet
,您可以执行以下操作:NSRect
包含有关该视图所在位置的所有信息。文本字段已定位。有关详细信息,请参阅以下 2 个指南:
视图编程指南:了解视图的框架和边界
Cocoa 绘图指南:坐标系和变换
Every kind of view or control (like
NSButton
,NSTextField
, etc.) that inherits fromNSView
has a-frame
method that contains the positioning information.If your custom view has an
IBOutlet
connected up to theNSTextField
, you could do the following:That
NSRect
contains all of the information for where the text field is positioned.See the following 2 guides for more information:
View Programming Guide: Understanding a View's Frame and Bounds
Cocoa Drawing Guide: Coordinate Systems and Transforms
任何视图都具有
bounds
和frame
属性。bounds
是将视图包围在该视图自己的坐标空间中的矩形;frame
是父视图坐标空间中的封闭矩形。在这种情况下,您需要文本视图的框架,即它在包含它的视图中的位置和大小,以便您可以在同一超级视图中找到其他文本视图。Any view has both a
bounds
and aframe
property.bounds
is the rectangle that encloses the view in that view's own coordinate space;frame
is the enclosing rectangle in the superview's coordinate space. In this case, you want the text view's frame, i.e. its location and size in the view that contains it, so that you can locate other text views in the same superview.