简体   繁体   English

iOS-定位UIViews

[英]iOS - Positioning UIViews

I have a UIView and in interface builder I have set it's location to 160, 57. 我有一个UIView,并且在界面构建器中将其位置设置为160、57。

I at some point during the apps life cycle, the UIView may move, and so I have a button to put it back to where it was before. 我在应用程序生命周期中的某个时候,UIView可能会移动,因此我有一个按钮可将其恢复到之前的状态。 So I use the following code: 因此,我使用以下代码:

[self.myView setCenter:CGPointMake(160, 57)];

However, when the UIView moves back, it's not in exactly the same place as it was when the app first loaded. 但是,当UIView向后移动时,它与应用程序首次加载时的位置并不完全相同。 It's a tiny tiny bit to one side. 一侧只有一点点。

Any idea why this is? 知道为什么会这样吗? Thanks in advance. 提前致谢。

尝试使用view.bounds.origin而不是view.center设置视图的位置。

I have solved my problem by using: 我已经使用以下方法解决了我的问题:

[self.myView setCenter:CGPointMake(159.5, 57)];

I didn't realise CGPointMake would accept floating point values so I thought I had to use 159 (too little) or 160 (too much). 我没有意识到CGPointMake会接受浮点值,所以我认为我必须使用159(太少)或160(太多)。 159.5 was just right though. 159.5是正确的。

It still doesn't explain why 160 works in Interface builder but I can just set this value when the view loads anyway. 它仍然没有解释为什么160在“界面”构建器中起作用,但是无论如何在视图加载时我都可以设置该值。

Thanks for everyone's answers. 感谢大家的回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM