在Swift游乐场中调整实时视图的大小

时间:2023-01-23 22:15:05

I'm having trouble figuring out how to lay out views in Swift Playgrounds for iPad, though this may also be relevant to Mac users.

我无法弄清楚如何在Swift Playgrounds for iPad中布置视图,尽管这也可能与Mac用户有关。

The following code should create a view with a red square (also a view) that is near the edges of its' super view, but not touching them.

以下代码应创建一个视图,其中包含一个红色正方形(也是一个视图),该视图靠近其“超级视图”的边缘,但不会触及它们。

let v = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
let sqv = UIView(frame: CGRect(x: 400, y: 400, width: 50, height: 50))
sqv.backgroundColor = .red
v.addSubview(sqv)
PlaygroundPage.current.liveView = v

The result is not what you'd expect:

结果不是你所期望的:

在Swift游乐场中调整实时视图的大小

I suspect I know what is going on here; live views are at a fixed size that is larger than the display area. Some characteristics of the view are ignored when it is acting as the live view. However, I can't find where this is mentioned in the documentation, which vexes me. More importantly, how do I deal with this? I would like to be able to layout simple UIs that change to fit the current size of the live view. I don't know how to address this issue without trial & error and hardcoding, which are two things I would really like to avoid.

我怀疑我知道这里发生了什么;实时视图的大小固定,大于显示区域。当视图充当实时视图时,将忽略视图的某些特征。但是,我无法找到文档中提到的位置,这让我很烦恼。更重要的是,我该如何处理?我希望能够布局简单的UI,这些UI会根据实时视图的当前大小进行更改。我不知道如何在没有反复试验和硬编码的情况下解决这个问题,这是我真正想避免的两件事。

2 个解决方案

#1


6  

I suspect I know what is going on here; live views are at a fixed size that is larger than the display area.

我怀疑我知道这里发生了什么;实时视图的大小固定,大于显示区域。

Actually it's more like the other way around. An iPad screen is 1024 points wide (in landscape orientation). The right-hand pane (where it shows your live view) is 512 points wide. The playground forces your root view (v) to fill that pane, inset by 40 points on the left, right, and top (and more on the bottom). So your root view's width is forced to 432 ( = 512 - 2 * 40), less than the 500 you specified.

实际上它更像是另一种方式。 iPad屏幕宽1024点(横向)。右侧窗格(显示您的实时视图)宽512点。操场强制您的根视图(v)填充该窗格,在左侧,右侧和顶部(以及底部更多)上插入40个点。因此,您的根视图的宽度被强制为432(= 512 - 2 * 40),小于您指定的500。

Views created in code (like yours) have translatesAutoresizingMaskIntoConstraints = true, and a resizing mask of 0, which means don't adjust the view's frame at all when its parent is resized. So the playground resizes your root view to width 432, but your root view doesn't move or resize its subview (sqv).

在代码中创建的视图(与您的一样)具有translatesAutoresizingMaskIntoConstraints = true和调整大小掩码0,这意味着在调整其父级的大小时,根本不调整视图的帧。因此,游乐场将根视图调整为宽度432,但您的根视图不会移动或调整其子视图(sqv)。

The easiest fix is to set the autoresizing mask of the subview to express your intent that it remain near the right and bottom edges of the root view. That means it should have flexible top and left margins:

最简单的解决方法是设置子视图的自动调整大小以表示您保留在根视图的右下边缘附近的意图。这意味着它应具有灵活的左上边距和左边距:

let v = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
let sqv = UIView(frame: CGRect(x: 400, y: 400, width: 50, height: 50))
sqv.autoresizingMask = [.flexibleLeftMargin, .flexibleTopMargin]
sqv.backgroundColor = .red
v.addSubview(sqv)
PlaygroundPage.current.liveView = v

Result:

在Swift游乐场中调整实时视图的大小

#2


1  

let sqv = UIView(frame: CGRect(x: UIScreen.mainScreen().bounds.width-50-1, y:400, width: 50, height: 50))

The above code places your subview 1 point away from the right of the main view. Try changing the value 1 after 50 in x to desired value.

上面的代码将您的子视图放置在远离主视图右侧1个点的位置。尝试将50 in x之后的值1更改为所需的值。

#1


6  

I suspect I know what is going on here; live views are at a fixed size that is larger than the display area.

我怀疑我知道这里发生了什么;实时视图的大小固定,大于显示区域。

Actually it's more like the other way around. An iPad screen is 1024 points wide (in landscape orientation). The right-hand pane (where it shows your live view) is 512 points wide. The playground forces your root view (v) to fill that pane, inset by 40 points on the left, right, and top (and more on the bottom). So your root view's width is forced to 432 ( = 512 - 2 * 40), less than the 500 you specified.

实际上它更像是另一种方式。 iPad屏幕宽1024点(横向)。右侧窗格(显示您的实时视图)宽512点。操场强制您的根视图(v)填充该窗格,在左侧,右侧和顶部(以及底部更多)上插入40个点。因此,您的根视图的宽度被强制为432(= 512 - 2 * 40),小于您指定的500。

Views created in code (like yours) have translatesAutoresizingMaskIntoConstraints = true, and a resizing mask of 0, which means don't adjust the view's frame at all when its parent is resized. So the playground resizes your root view to width 432, but your root view doesn't move or resize its subview (sqv).

在代码中创建的视图(与您的一样)具有translatesAutoresizingMaskIntoConstraints = true和调整大小掩码0,这意味着在调整其父级的大小时,根本不调整视图的帧。因此,游乐场将根视图调整为宽度432,但您的根视图不会移动或调整其子视图(sqv)。

The easiest fix is to set the autoresizing mask of the subview to express your intent that it remain near the right and bottom edges of the root view. That means it should have flexible top and left margins:

最简单的解决方法是设置子视图的自动调整大小以表示您保留在根视图的右下边缘附近的意图。这意味着它应具有灵活的左上边距和左边距:

let v = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
let sqv = UIView(frame: CGRect(x: 400, y: 400, width: 50, height: 50))
sqv.autoresizingMask = [.flexibleLeftMargin, .flexibleTopMargin]
sqv.backgroundColor = .red
v.addSubview(sqv)
PlaygroundPage.current.liveView = v

Result:

在Swift游乐场中调整实时视图的大小

#2


1  

let sqv = UIView(frame: CGRect(x: UIScreen.mainScreen().bounds.width-50-1, y:400, width: 50, height: 50))

The above code places your subview 1 point away from the right of the main view. Try changing the value 1 after 50 in x to desired value.

上面的代码将您的子视图放置在远离主视图右侧1个点的位置。尝试将50 in x之后的值1更改为所需的值。