Setting the origin of the frame in iOS

倖福魔咒の 提交于 2019-12-12 10:29:16

问题


I am trying to set the origin of the frame programmatically.

Method1:

button.frame.origin.y = 100;

Method 2:

CGRect frame = button.frame;
frame.origin.y = 100;

I tried method 1 but it is not working(showing an error saying Expression is not assignable). Method 2 is working. Why is this so?

Need guidance on what I am doing right.


回答1:


The reason you can't do method #1 is because a frame's individual properties are read-only. However, the entire frame itself is writable/assignable, thus you can do method #2.

You can even do tricks like this:

CGRect newFrame = button.frame;
newFrame.origin.y += 100; // add 100 to y's current value
button.frame = newFrame;



回答2:


You know button.frame.origin.y return a value.

if you are using this one so you will get this error...

Expression is not assignable.

button.frame.origin.y = 100;

So this is correct way

CGRect frame = button.frame;
frame.origin.y = 100;

otherwise you can do like this...

button.frame = CGRectMake(button.frame.origin.x, 100, button.frame.size.width, button.frame.size.height)



回答3:


It's because if you were able to directly change a frame's origin or size, you would bypass the setter method of UIView's frame property. This would be bad for multiple reasons.

UIView would have no chance to be notified about changes to it's frame. But it has to know about these changes to be able to update it's subviews or redraw itself.




回答4:


button.frame.origin.y = 100;

equals to the following call:

[button getFrame].origin.y = 100;

in which [button getFrame] gives you the copied CGRect.

You are trying to set a variable of a structure of a structure. There is no pointer from the UIView to the structure (as it is a basic C struct so can't be pointed). hence, copied instead.

So basically, you are copying the structure and setting it. But, that just doesn't work like that.



来源:https://stackoverflow.com/questions/16332517/setting-the-origin-of-the-frame-in-ios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!