frame.size.width和frame.width之间的区别 [英] Difference between frame.size.width and frame.width
问题描述
我正在swift中编写一个程序,刚才我注意到我可以直接访问 CGRect
框架的width和height属性而不使用 CGSize
宽度和高度。那就是我现在能够编写这样的代码。
I was writing a program in swift and just now I noticed that I can directly access a CGRect
frame's width and height properties directly without using the CGSize
width and height. That is I am now able to write a code like this.
@IBOutlet var myView: UIView!
override func viewDidLoad()
{
super.viewDidLoad()
var height = myView.frame.height
var height1 = myView.frame.size.height
}
在Objective C中,当我尝试编写相同的代码时,行 height = view.frame.height
抛出错误。任何人都可以告诉我这两行代码中的差异(如果有的话)。
In Objective C, when I tried to write the same code, the line height = view.frame.height
is throwing an error. Can anyone please tell me the difference(if any) in these two lines of code.
推荐答案
我只是看了 CGRect
结构参考。在Swift中,定义了一个扩展名,其成员 height
和 width
。请看下面的代码
I just looked into the CGRect
structure reference. In Swift there is an extension defined which have members height
and width
. Please have a look at the code below
struct CGRect {
var origin: CGPoint
var size: CGSize
}
extension CGRect {
...
var width: CGFloat { get }
var height: CGFloat { get }
...
}
这样你就可以直接获取身高
和 width
来自 CGRect
的值。如您所见,这些只是 getters
,因此如果您尝试使用 view.frame.height = someValue <设置这些值,则会出错/ code>
So that you can directly fetch height
and width
values from a CGRect
. As you can see these are only getters
, so you will get an error if you try to set these values using view.frame.height = someValue
这篇关于frame.size.width和frame.width之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!