Difference between frame.size.width and frame.width

后端 未结 3 1784
梦毁少年i
梦毁少年i 2020-12-17 16:15

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

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-17 17:08

    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 }
        ...
    }
    

    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

提交回复
热议问题