It may sound very silly to people who know this but I am quite new to iOS. I wanted to know, why cant we modify origin or size or may be x,y or height width of these properties of a UIView frame ?
frame is a CGRect struct and origin and size are again CGPoint and CGSize structures. when we can access them, why cant we modify them ?
There is nothing like this in a CGRect, we can modify it's attributes so why cant we do it for frame or bounds.
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.
Frame A view's frame ( CGRect ) is the position of its rectangle in the superview 's coordinate system. By default it starts at the top left. Bounds A view's bounds ( CGRect ) expresses a view rectangle in its own coordinate system.
I assume you mean why can't you do somethIng like this:
myView.frame.origin.x = 23.0;
It's a good question, nothing silly about it. The problem is that the frame is held as the property of the view, not the individual components of the frame (the size and origin, or even deeper, the width, length, x and y).
The frame is fundamental to the view, so there are lots of actions that a view needs to do if its frame is modified. If you were to reach in and modify the origin.x directly then you would be bypassing the setFrame
method where all of this magic probably happens.
When you access view.frame you are being given a CGRect that has the same value as view.frame, but is not actually the frame, so any modifications to it don't affect the view.
You can do it in steps:
CGRect frame = view.frame;
frame.origin.x = 23.0;
view.frame = frame;
The answer lies in the (slightly obfuscated) language syntax:
myView
is an Objective-C object.myView.frame
is a property accessor method being send.frame.origin.x = 1
modifies the returned temporary struct that is local to your code.Your intent was to tell an object something by sending it a message. But instead you asked the object for data and then modified this data. The object would never now.
The compiler should warn you about this error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With