今天不一致的小部件行为打破了子视图的高度限制

Inconsistent Today Widget behavior breaks subview#39;s height constraints(今天不一致的小部件行为打破了子视图的高度限制)

本文介绍了今天不一致的小部件行为打破了子视图的高度限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与我刚刚在 Stackoverflow 上发布的另一个问题有关:

当我启动应用程序(在模拟器设备上测试)时,高度限制被简单地忽略,黄色视图一直占据整个可用空间下一个小部件的标题.

当我向上滑动通知中心并再次将其拉下时,视图突然跳跃(似乎突然看到"了它自己的高度限制)在小部件底部留下了 39px 的垂直空白空间:

我认为底部的 39px 边距 是今天小部件的 默认底部边距,由 defaultMarginInsets 参数传入widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) 方法,我可以通过重写此方法并提供我自己的边距插入来解决这种不一致的行为:

func widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) ->UIEdgeInsets {var newInsets = defaultMarginInsetsnewInsets.bottom = 20返回新插图}

但是,我真的更喜欢使用系统提供的边距而不是固定值.在我看来,这是关于今天小部件的另一个 iOS 错误.是吗?如果没有:我该如何解决这个问题?

解决方案

尽量避免使用别针.

对于定位,依赖于将您的视图与父视图的前缘、后缘、顶部或底部边缘对齐.

为了调整大小,请尝试将您的视图设置为与父视图具有相同的高度或宽度.并根据需要调整乘数.

这解决了我在 Today Widget 中遇到的自动布局不一致问题.

更新了截图:

见上文,我使用的是 align 菜单(不是 pin 菜单).我选择了我试图约束的视图,以及所有封装的超级视图,并告诉共享(或,对齐)尾随和底部边缘的先验.

我知道这不是 Apple 可能会展示的方式,但它是一种解决方法,可以避免在 Today Widgets 中使用 pin 时出现的错误.

更新 #2 - 这里是所有约束(包括高度和宽度):

该错误必须与完全固定的视图的推断大小有关,因为当我将视图的高度和宽度设置为相对于其父视图(而不是对其进行推断)时,该错误不会发生.

This question is related to another question I just posted on Stackoverflow:
Layout Constraint Conflicts in Default Today Widget

I added a Today Extension as a target to my app, removed the default "Hello World" label that's inside the widget's root view and added a plain UIView in its place. I gave the view a yellow color and pinned it to all edges of the root view - the same way the label was constrained. Then I added another constraint to the yellow view in order to give it a fixed height of 100px.

When I launch the app (tested on simulator and device) that height constraint is simply ignored and the yellow view takes up the whole available space all the way down to the header of the next widget.

When I swipe the notification center up and pull it down again the view suddenly jumps (it seems like it's suddenly "seeing" its own height constraint) leaving a vertical blank space of 39px at the bottom of the widget:

 

I figured that the 39px margin at the bottom is the default bottom margin of a today widget as passed in by the defaultMarginInsets parameter in the widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) method and that I can fix this inconsistent behavior by overriding this method and providing my own margin insets:

func widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) -> UIEdgeInsets {
    var newInsets = defaultMarginInsets
    newInsets.bottom = 20
    return newInsets
}

However, I would really prefer to use system provided margins instead of fixed values. It seems to me like this is another iOS bug regarding the today widget. Is it? And if not: How can I fix this?

解决方案

Try avoiding the use of pins.

For positioning, rely on aligning your view to the superview's leading, trailing, top, or bottom edges.

For sizing, try setting your view to have equal heights or widths with the superview. And adjusting the multiplier as needed.

This solved auto layout inconsistencies I was experiencing in a Today Widget.

Updated w/ screenshot:

See above, I am using the align menu (not the pin menu). I select both the view I am trying to constrain, along with the all encasing superview, and tell the prior to share (or, align with) trailing and bottom edges.

I know this is not how Apple may demonstrate it, however it is a workaround that avoids the bugs that occur when using pins with Today Widgets.

Update #2 - And here is all the constraints (including height and width):

The bug must be related to the inferred sizing of a view that is entirely pinned, because when I set the height and width of my view to be relative to its superview (rather than having it be inferred), the bug does not occur.

这篇关于今天不一致的小部件行为打破了子视图的高度限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:今天不一致的小部件行为打破了子视图的高度限制

基础教程推荐