如何定位UIView? [英] How to position a UIView?

查看:120
本文介绍了如何定位UIView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了几个小时试图定位一个UIView,最终发现我需要修改视图框架。所以我像这样在UIViewController子类中添加了一个'setPosition'方法

I spent a couple of hours trying to position a UIView and eventually figured out that I needed to modify the views frame. So I added a 'setPosition' method to the UIViewController subclass like this

- (void) setPosition:(CGPoint)position
{

    CGRect newFrame = self.view.frame;
    newFrame.origin.x = position.x;
    newFrame.origin.y = position.y;
    self.view.frame = newFrame;

}

然而,这看起来很简单,为什么UIViews没有这个方法已经,这让我认为这可能不是正确的方式做到这一点。所以这是我的问题...

However, that seems so simple that I don't understand why UIViews don't have this method already, which makes me think this might not be the right way to do it. So that's my question...

这个方法好吗,还是我做某些我不应该做的事...因为某种原因?

Is this method OK or am I doing something I shouldn't be doing... for some reason?

推荐答案

像这样做的复制,修改和设置框架是如何做到这一点。这也可以通过创建一个rect并直接设置它来完成:

Copying, Modifying and setting the frame again like you have done here is how this is generally done. This can also be done by creating a rect and setting it directly:

UIView.frame = CGRectMake(50,50,50,50); // x,y,w,h

在动画块中执行此操作将使这些更改生效。

Doing this in an animation block will animate these changes.

您可以用以下方式设置视图中心点:

Alternitively you can set a views Center point with :

UIView.center = CGPointMake(50,50);

这篇关于如何定位UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆