iPhone 旋转后如何移动屏幕上的项目位置
我有一个 UILabel,我需要在应用程序启动时向上移动大约 50 像素(因为它以纵向方式开始,我以横向方式设计它),然后每当它放回纵向时再次向上移动。现在我正在使用基于当前帧的CGRect
。问题是,当我尝试旋转手机时,框架使用纵向值,并且标签偏离了方向。我尝试过使用其他几种方法,所有方法都基于 CGRect,虽然它们将标签保留在右轴上,但它却被移动到各处。在我这样做的同时,我也在改变字体的大小,所以我认为这可能会影响一些东西。
我使用的代码是:
//In ViewDidLoad
CGRect defBox = self.seperator.frame;
//On Rotation to Portriate
CGRect newBox = self.seperator.frame;
newBox.origin.y += 50;
self.seperator.frame = newBox;
//On Rotation to Landscape
self.seperator.frame = defBox;
I have a UILabel
that I need to shift up by about 50 pixels on application start (because it starts out in portrait, and I designed it in landscape) and then once again whenever it is put back in portrait. Right now I'm using a CGRect
based on its current frame. The problem is when I try to rotate the phone the frame uses the portrait values and it places the label way off. I've tried using a couple other methods, all based around CGRect
, and while they kept the label on the right axis, it was shifted all over the place. At the same time I'm doing this, I'm also changing the size of the font, so I think that this might affect something.
The code I used is:
//In ViewDidLoad
CGRect defBox = self.seperator.frame;
//On Rotation to Portriate
CGRect newBox = self.seperator.frame;
newBox.origin.y += 50;
self.seperator.frame = newBox;
//On Rotation to Landscape
self.seperator.frame = defBox;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
self.separator.bounds
,而不是self.separator.frame
并在willAnimateRotationToInterfaceOrientation:
或didRotateFromInterfaceOrientation:
中执行。Use
self.separator.bounds
, notself.separator.frame
and do it inwillAnimateRotationToInterfaceOrientation:
ordidRotateFromInterfaceOrientation:
.