在iOS中设置框架的原点 [英] Setting the origin of the frame in iOS
本文介绍了在iOS中设置框架的原点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试以编程方式设置框架的原点.
I am trying to set the origin of the frame programmatically.
方法1:
button.frame.origin.y = 100;
方法2:
CGRect frame = button.frame;
frame.origin.y = 100;
我尝试了方法1,但是它不起作用(显示错误消息,表明无法分配表达式).方法2正在工作.为什么会这样?
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的原因是,框架的各个属性是只读的.但是,整个框架本身都是可写/可分配的,因此您可以执行方法2.
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;
这篇关于在iOS中设置框架的原点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文