如何使用自动调整大小蒙版将 UIView 锚定在距中间点几个像素的位置?

发布于 2024-12-12 09:15:13 字数 410 浏览 0 评论 0原文

观察以下屏幕截图:

iPhone Autoresizing Mask Question

如何使用 Autoresizing Mask 将 UIView 锚定在距中间点几个像素的位置,以便它总是距中点左侧 20px?

我尝试将 autoresizingMask 属性设置为 UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin 它并没有真正做到这一点。

如果我将视图包装在另一个填充整个屏幕并且根本不调整大小的更大视图中,我确实设法做到了。但是有没有办法在没有额外视图的情况下做到这一点呢?

Observe the following screenshot:

iPhone Autoresizing Mask Question

How do I anchor UIView a few pixel off the middle point using Autoresizing Mask so that it is always 20px to the left of the middle point?

I have tried setting the autoresizingMask property to UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin and it's not really doing it.

I did manage to do it if I wrap the view in another bigger view that fills the entire screen and doesn't resize at all. But is there a way to do it without an additional view?

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

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

发布评论

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

评论(3

贱贱哒 2024-12-19 09:15:13

正如您已经发现的那样,自动调整蒙版大小不能很好地处理这种情况。如果您想将某些内容与其超级视图保持固定距离或在中间某处按比例调整大小/重新定位,它们会非常有用。仅使用这些选项就可以完成令人惊讶的工作量,但是偏离中心锚定并不是您可以轻松完成的事情

如果您想使用自动调整大小蒙版来执行此操作,则需要将框放入另一个空的 UIView,它处于更方便的位置来自动调整蒙版的大小。它看起来像这样。

这就是您现在所得到的:

                       center
                          |
|---------------------------------------------------|  <-- The main parent view
               |-----|  <-- your view

您想要的是:

                       center
                          |
|---------------------------------------------------|  <-- The main parent view
               |--------------------|  <-- The new view, centered in the parent
               |-----|  <-- your view

新视图应该完全透明,具有固定宽度,并且与父视图两侧的距离灵活。它应该足够宽以完全容纳偏离中心的盒子,并且不能更宽。如果它正好位于主父视图的中心,那么无论父视图的大小发生什么变化,它都会保持居中。

然后将您的框添加为新视图的子视图,具有固定的宽度和距父级左边缘的固定距离。现在,仅使用自动调整大小蒙版,您的视图将保持在您想要的位置。

更简单的选择可能是覆盖视图上的 -layoutSubviews 或控制器上的 -viewDidLayoutSubviews (适用于 iOS 5.0 及更高版本),然后手动定位视图。但是您询问如何使用自动调整大小蒙版来做到这一点,所以这就是您所得到的。如果不添加额外的视图,就无法使用自动调整大小蒙版来获得您想要的定位行为。

Autoresizing masks don't handle this case very well, as you've already discovered. They work great if you want to keep something a fixed distance from its superview or proportionally resizing/repositioning somewhere in the middle. You can do a surprising amount with just those options, but off-center anchoring is not something you can do easily

If you want do this with autoresizing masks, you'll need to put your box inside another empty UIView, one that is in a more convenient position for autoresizing masks. It will look like this.

Here's what you've got right now:

                       center
                          |
|---------------------------------------------------|  <-- The main parent view
               |-----|  <-- your view

What you want is this:

                       center
                          |
|---------------------------------------------------|  <-- The main parent view
               |--------------------|  <-- The new view, centered in the parent
               |-----|  <-- your view

The new view should be completely transparent, have a fixed width, and a flexible distance from both sides of the parent view. It should be wide enough to fully contain the off-center box, and no wider. If it is positioned exactly in the center of the main parent view, it will stay centered no matter what happens to the size of the parent view.

Then add your box as a subview of the new view, with a fixed width and fixed distance from the left edge of the parent. Now, using only autoresizing masks, your view will stay where you want it.

A simpler option might be to override -layoutSubviews on your view, or -viewDidLayoutSubviews on your controller (available iOS 5.0 and later) and just manually position the view. But you asked how to do it with autoresizing masks, so that's what you got. Without adding an extra view, there's no way to use autoresizing masks to get the positioning behavior you want.

终陌 2024-12-19 09:15:13

我想最好的解决方案是将 center 属性设置为:

myview.center = CGPointMake(self.view.frame.size.width/2-20, myview.center.y);

并在 willRotate 方法中设置它。

I guess the best solution is to set center property to:

myview.center = CGPointMake(self.view.frame.size.width/2-20, myview.center.y);

And set up it in willRotate method.

め可乐爱微笑 2024-12-19 09:15:13

不完全是答案,更多的是探索的途径。您可以尝试使用图层级属性anchorPoint。将其设置为 (1.0, 0.5) 意味着图层的位置将由其右边缘定义。在这种情况下,居中意味着右边缘居中。设置灵活的左边距和灵活的右边距,它可能会保持在中心左侧。

要使其位于中心左侧 20p 处,只需将锚点设置为视图中心右侧 20p 处即可。 (但是锚点单位是图层大小的一小部分,因此需要进行一些数学计算才能找到正确的值。)

我不确定它是否有效。我不确定将图层级定位与自动调整大小混合在一起会产生什么效果。

Not exactly an answer, more of an avenue for exploration. You can try playing with the layer-level property anchorPoint. Setting it to (1.0, 0.5) means the layer's position will be defined by its right edge. In that case centered would mean the right edge is centered. Set flexible left margin and flexible right margin and it might stay left of the center.

To get it exactly 20p left of the center, just set the anchorPoint to 20p right of the center of the view. (But anchorPoint units are a fraction of the size of the layer, so do some math to find the right value.)

I'm not sure if it will work. I'm not sure what the effects are of mixing layer-level positioning with autoresizing.

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