【问题标题】:get the value of x and y of UILabel?获取 UILabel 的 x 和 y 的值?
【发布时间】:2011-05-21 23:01:30
【问题描述】:

我想以编程方式获取 UILabel 的位置。我该怎么做,即获取 UILabel 的 x 和 y?

我试过textAlert.center.ytextAlert.center.xtextalert是UILabel的名字)。

【问题讨论】:

  • 名字是textalert还是textAlert

标签: objective-c cocoa-touch ios4


【解决方案1】:

这是您要查找的 frame 属性。试试看。

textAlert.frame.origin.x
textAlert.frame.origin.y
textAlert.frame.size.width
textAlert.frame.size.height

【讨论】:

  • @Tushar 正如 Alexsander Akers 指出的那样,ivars 不区分大小写。由于您似乎没有提到错误,因此您可能已经定义了两者。
  • 这应该可以,你确定你没有空引用吗?什么代码初始化了你的 UILabel,你目前从 deepak 的代码中得到了什么?
【解决方案2】:

这实际上取决于您想要查看的哪个部分的坐标。

frame.origin - 返回左上角
center - 返回视图的中心
CGRectGetMinX(frame) - 获取左边缘的 x 坐标。
CGRectGetMinY(frame) - 获取 y 坐标上边缘的坐标。
CGRectGetMidX(frame) - 获取中心的 x 坐标。
CGRectGetMidY(frame) - 获取中心的 y 坐标。
CGRectGetMaxX(frame) - 获取右边缘的 x 坐标。
CGRectGetMaxY(frame) - 获取底部边缘的 y 坐标。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 2014-01-17
    • 2013-04-22
    相关资源
    最近更新 更多