使用自动布局正确对齐通知中心小部件中的元素

Properly align elements in Notification Center widget using Auto Layout

我很难在我的通知中心扩展中正确对齐元素。我想对齐元素以尊重应用的默认边距 - 它应该与应用程序名称中的第一个字符左对齐,并与应用的一些填充右对齐。如果您注意到日历扩展中的线条,我正在尝试完全匹配这些边距(尽管在某些情况下它们似乎也不太正确)。

我使用自动布局设置了我的元素,所有这些都是通过编程完成的。我已将前导设置为 self.view 的前导和尾随 self.view 的尾随。当我 运行 在各种设备上扩展时,加载填充不一致,而尾部始终清晰到最远的边缘。

如果我将它从 Leading 和 Trailing 更改为 LeadingMargin 和 TrailingMargin,它看起来完全一样。如果我更改它以将元素的 Leading 与视图的 LeadingMargin 对齐,并将尾部与视图的 TrailingMargin 对齐,那么尾部似乎是所需的边距,但在 iPhone 6 上,即使它在iPad。在 iPhone 6 Plus 中,前导与应用程序图标的开头对齐,尾部没有填充,但只是有时!其他时候它确实会增加边距。

如何配置它以正确对齐元素?

我还没有实现widgetMarginInsetsForProposedMarginInsets

//viewDidLoad:
let label = UILabel()
label.backgroundColor = UIColor.blueColor()
label.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(label)
self.view.addConstraint(NSLayoutConstraint(item: label, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: label, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: label, attribute: .Leading, relatedBy: .Equal, toItem: self.view, attribute: .Leading, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: label, attribute: .Trailing, relatedBy: .Equal, toItem: self.view, attribute: .Trailing, multiplier: 1, constant: 0))
let heightConstraint = NSLayoutConstraint(item: label, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1, constant: 100)
heightConstraint.priority = 999
self.view.addConstraint(heightConstraint)

iPhone 6人像:

iPhone 6+人像:

iPhone 6加横向:

iPad (Portrait/Landscape):

看起来这是一个影响 iOS 8.2 及之前版本的问题,因为它的行为符合预期,并且如果我在 iOS 8.3 中使用 Leading -> Leading and Trailing -> TrailingMargin 则它是一致的。