0
`lblAddress.frame.width`
`lblAddress.frame.size.width`

lblAddress.frame.width我已经搜索了很多次,但我仍然没有指出...... AND 之间有什么区别lblAddress.frame.size.width。它给了我相同的输出..我不知道什么时候使用哪个?

请提出一些例子,这样我就可以弄清楚整个事情。

4

5 回答 5

1

它们(几乎)是相同的。一个是CGRect的宽度,另一个是CGSize 的宽度

唯一的区别是它frame.width总是返回正值,frame.size.width在某些情况下可能返回负值。

frame.width如果我只关心标准化几何,我会直接使用速记。

于 2017-06-06T03:47:01.727 回答
1

frame 和 size 都具有 width 属性。您可能会感到困惑,因为尺寸也是框架的属性。就像我问你我穿什么颜色的鞋子,但下一刻问我的脚穿什么颜色的鞋子,我问的是同样的事情,我可能会方便地问一种或另一种方式根据我的情况。

于 2017-06-06T03:57:35.347 回答
1

要了解更多信息,您可以查看 CGRect 结构是如何构造的。

struct CGRect {
  var origin: CGPoint
  var size: CGSize
}

为了添加一些额外的功能,苹果还提供了一个 CGRect 的扩展,如下所示

extension CGRect {

    @available(iOS 2.0, *)
    public var height: CGFloat { get }

    @available(iOS 2.0, *)
    public var width: CGFloat { get }
}

现在回答你的问题。lblAddress.frame 将返回一个 CGRect

lblAddress.frame.width //This will use the extended functionality 
lblAddress.frame.size.width //this will use the actual struct

internally the width extension on CGRect will always use the same 
CGSize property from struct to give the width back and hence the same 
size property is accessed in both case, but the difference is the first 
one is standardized and hence only gives back positive values whereas 
the second will give the negative values as well.
于 2017-06-06T06:25:47.110 回答
0

你想找到原点在这里使用这个代码ViewSend是视图的出口

self.ViewSend.frame.origin.y

你想找到高度所以使用这个代码

self.ViewSend.frame.size.height

你想找到宽度所以使用这个代码

self.ViewSend.frame.size.width
于 2017-06-06T05:33:02.333 回答
0

它们几乎相同;然而,fram.width是一个只能获取的值,但frame.size.width可以用来设置或获取值

于 2017-06-06T06:04:38.437 回答