Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between frame.size.width and frame.width

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
}

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.

like image 942
Harikrishnan Avatar asked Dec 02 '14 06:12

Harikrishnan


People also ask

What is the difference between frame and bounds in UIView?

The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0). The frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.

What's a difference between frame and bounds Swift?

TLDR: Bounds refers to the views own coordinate system while Frame refers to the views parent coordinate system.

What is frame and bounds in IOS?

Short description. frame = a view's location and size using the parent view's coordinate system ( important for placing the view in the parent) bounds = a view's location and size using its own coordinate system (important for placing the view's content or subviews within itself)


2 Answers

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

like image 132
Vinay Jain Avatar answered Oct 17 '22 03:10

Vinay Jain


frame is of CGRect structure, apart from its width and height have only getters, they can only be positive. From the documentation:

Regardless of whether the height is stored in the CGRect data structure as a positive or negative number, this function returns the height as if the rectangle were standardized. That is, the result is never a negative number.

However, size is of CGSize structure, from the documentation:

A CGSize structure is sometimes used to represent a distance vector, rather than a physical size. As a vector, its values can be negative. To normalize a CGRect structure so that its size is represented by positive values, call the standardized function.

So the difference is obvious.

like image 29
Silk Song Avatar answered Oct 17 '22 03:10

Silk Song