【问题标题】:When setting the frame of a UILabel, is it possible to set it to the vertical and horizontal center of the superview in code?在设置UILabel的frame时,是否可以在代码中设置到superview的纵横中心?
【发布时间】:2013-05-19 20:29:27
【问题描述】:

CGRectMake(),但它指定中心,水平和垂直?

【问题讨论】:

标签: ios objective-c uiview uilabel


【解决方案1】:
  1. 获取包含视图的尺寸。
  2. 获取要插入的视图的尺寸。
  3. x_position = width_of_containing_view / 2.0 - width_of_inserted_view / 2.0
  4. y_position = height_of_containing_view / 2.0 - height_of_inserted_view / 2.0

【讨论】:

    【解决方案2】:

    你也可以label.center = [superview convertPoint:superview.center fromView:superview.superview];=P

    【讨论】:

      【解决方案3】:

      将标签的center 属性设置为其父视图bounds 的中心点。由于 superview 的 centerframe 属性位于 它们的 superview 的坐标空间中,因此这些属性没有帮助(除非您像 fumoboy 那样进行转换)。

      label.center = CGRectGetMidPoint(label.superview.bounds);

      为了方便,我做了这个函数:

      CGPoint CGRectGetMidPoint(CGRect rect)
      {
          return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
      }
      

      【讨论】:

        猜你喜欢
        • 2014-06-21
        • 1970-01-01
        • 1970-01-01
        • 2015-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-18
        • 2012-05-01
        相关资源
        最近更新 更多