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.
来源:https://stackoverflow.com/questions/37010292/inconsistent-today-widget-behavior-breaks-subviews-height-constraints