UIView setCenter问题

发布于 2024-10-24 03:38:32 字数 1055 浏览 0 评论 0原文

我在做一些非常简单的事情时遇到了很多麻烦。我有一个 viewController 和一个 subViewController,两者都具有从 nib 加载的视图。为了澄清起见,父视图的大小为 1024 x 748,子视图的笔尖大小为 640 x 480。

在 viewController 的 viewDidLoad 上,我添加了 subViewController 的视图,如下所示:

[self.view addSubview:self.subViewController.view];
NSLog(@"subview.frame:%@",NSStringFromCGRect(self.subViewController.view.frame));

日志输出:

subview.frame:{{0, 0}, {640, 480}}

然后,为了测试结果,我使用 setCenter 尝试将子视图重新定位在父视图内的同一位置,如下所示:

CGPoint newCenter = CGPointMake(0,0);  //should stay at the same position..
[self.subViewController.view setCenter:newCenter];
NSLog(@"after changing center.subview.frame:%@",NSStringFromCGRect(self.subViewController.view.frame));

日志输出:

after changing center. subview.frame:{{-320, -240}, {640, 480}}

现在我有一种感觉,笔尖可能有什么东西被搞砸了,我猜测这与弹簧和弹簧有关。支柱/尺寸和位置选项。我只能猜测是这个,因为这是我对IB了解最少的功能。

关于可能导致此问题的原因还有其他想法吗?对于如何在 IB 中设置视图控制器的视图,以便它们自动与其他视图控制器的视图对齐,你们有什么一般建议吗?设置完成后, setCenter 是动态移动它们的正确方法吗?

I'm having a lot of trouble doing something very simple. I have a viewController and a subViewController, both with views loaded from nibs. For clarification the parent view is sized 1024 by 748 and the subView is 640 by 480 in the nibs.

On the viewController's viewDidLoad I add the subViewController's view like so:

[self.view addSubview:self.subViewController.view];
NSLog(@"subview.frame:%@",NSStringFromCGRect(self.subViewController.view.frame));

The log outputs:

subview.frame:{{0, 0}, {640, 480}}

Then, just to test things out, I use setCenter to attempt to reposition the subview within the parent view at the same position like so:

CGPoint newCenter = CGPointMake(0,0);  //should stay at the same position..
[self.subViewController.view setCenter:newCenter];
NSLog(@"after changing center.subview.frame:%@",NSStringFromCGRect(self.subViewController.view.frame));

The log outputs:

after changing center. subview.frame:{{-320, -240}, {640, 480}}

Now I have a feeling that something might be getting screwed up in the nibs and am guessing it's something to do with the springs & struts / size & position options. I can only guess it's this because this is the feature I have least understanding of when it comes to IB.

Any other ideas as to what could be causing this issue? Do you guys have any tips in general on how to set up viewcontrollers' views in IB so that they are automatically positioned in alignment with other viewcontrollers' views? And after setting things up, is setCenter the correct way of moving them around dynamically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风夜微凉 2024-10-31 03:38:32

鉴于您正在做的事情,输出是正确的。点 (0,0) 是左上角。如果将子视图的中心设置为此,那么子视图的左上角确实将是(-640/2,-480/2)

要对齐左上角,请使用

[self.subViewController.view setCenter:CGPointMake(320.0,240.0)];

The outputs are correct given what you are doing. The point (0,0) is the top-left corner. If you set the center of subview to this, then the top left corner of the subview will indeed be (-640/2, -480/2).

To get the top-left corners to align, use

[self.subViewController.view setCenter:CGPointMake(320.0,240.0)];
半衬遮猫 2024-10-31 03:38:32

问题尚不清楚。如果将中心设置为 0,0,则帧原点应该为 -320、-240,正如其显示的那样。也许你误解了CGRect? CGRect由原点和尺寸组成。框架是一个 CGRect。框架、大小和中心都是相关的,改变框架会改变大小和中心,改变中心会改变框架。

The problem is unclear. If you set the center to 0,0, then you should expect the frame origin to be -320, -240 as it appears to be. Perhaps you misunderstand CGRect? CGRect is made up of origin and size. Frame is a CGRect. Frame, size and center are all related, and changing frame changes size and center, and changing center changes frame.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文