为什么视图的帧宽度通过自动布局总计600乘以600 [英] Why is frame width of the view alway 600 by 600 with autolayout

查看:103
本文介绍了为什么视图的帧宽度通过自动布局总计600乘以600的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为swift中的练习/乐趣制作一个基本的扫雷应用程序。我想这样做,所以板的大小(10瓦宽)适应任何iOS屏幕。

I am making a basic Minesweeper app for practice / fun in swift. I want to make it so the size of the board (10 tiles wide) adapts to any iOS screen.

为此,我通过获取tileContainer view.frame.width和/ 10来设置每个图块的大小。

To do this I'm setting the size of each tile by getting my tileContainer view.frame.width and / 10.

我的问题是tileContainer视图设置为600,无论如何。在我的故事板上,我将tileContainer设置为等于View容器的宽度,按CTR点击&拖动到视图并选择相等的宽度。无论我正在测试的设备如何,这都会将tileContainer的宽度设置为600。 (这是我的问题,宽度应该根据屏幕宽度变化而不是常数600)

My problem is that the tileContainer view is set to 600, no matter what. On my Storyboard I set the tileContainer equal to the width of the View Container, by CTR click & dragging to the view and selecting equal width. This keeps the width of tileContainer set at 600, regardless of the device I'm testing on. (which is my problem, width should change depending on screen width not a constant 600)

有谁知道如何获得适当的屏幕宽度,无论设备如何它正在被使用?

Does anyone know how I can get the proper width of the screen regardless of the device it's being used on?

推荐答案

当您使用自动布局时,子视图将在 viewDidLayoutSubviews 功能。因此,如果您在此之前调用 tileContainer.frame.size ,例如在 viewDidLoad 中,它将始终为600 600(这是故事板中的默认大小)。

When you are using auto-layout, the subviews are laid out after the viewDidLayoutSubviews function. Therefore, if you call tileContainer.frame.size before that, such as in viewDidLoad, it will always be 600 by 600 (which is the default size in storyboard).


viewDidLayoutSubviews :被调用以通知视图控制器其视图刚刚布置了其子视图。 参考

viewDidLayoutSubviews: Called to notify the view controller that its view has just laid out its subviews. reference



override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    print(tileContainer.frame.size); // This is actual size you are looking for
}

这篇关于为什么视图的帧宽度通过自动布局总计600乘以600的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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