在今日扩展中使用自动布局的动态高度 [英] Dynamic height using Auto Layout in Today Extension
问题描述
我的原始"问题已解决,被"<NSAutoresizingMaskLayoutConstraint:0x6000000985b0 h=--& v=--& UIView:0x7fc83af0aeb0.height == 110 (active)>"
限制为110高度,但是那是在我读到有关NCWidgetDisplayMode
,.Compact
和
My "original" problem has been solved, it was restricted to 110 height, by "<NSAutoresizingMaskLayoutConstraint:0x6000000985b0 h=--& v=--& UIView:0x7fc83af0aeb0.height == 110 (active)>"
, but that was before I read about NCWidgetDisplayMode
, .Compact
and .Expanded
, which apparently was introduced in iOS 10.
有趣的事情::当他们在iOS 10中展示新功能时,似乎遇到了错误(
Funny thing: Looks like they had the bug when they presented the new feature in iOS 10 (https://developer.apple.com/videos/play/wwdc2016/101/?time=3221)
尽管实现了该功能,但我仍然无法获得正确的高度调整.请参阅下面的相关代码详细信息,并查看完整的源代码: https://github.com/everlof/TodayExtensionSample .
With that implemented though, I still don't get the correct height adjustment. See relevant code details below, and for the full source code check out: https://github.com/everlof/TodayExtensionSample.
override func viewDidLoad() {
super.viewDidLoad()
extensionContext?.widgetLargestAvailableDisplayMode = .expanded
view.backgroundColor = .red
lbl.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vitae tempor nulla, in volutpat lectus. Sed quis orci sit amet velit cursus congue non accumsan turpis. Phasellus quis augue lobortis, pharetra arcu vitae, condimentum nunc. Nam rutrum massa ac feugiat eleifend. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec non erat orci. Ut consequat faucibus sapien, et luctus magna posuere tempor."
lbl.numberOfLines = 0
lbl.backgroundColor = .blue
lbl.translatesAutoresizingMaskIntoConstraints = false
}
和widgetActiveDisplayModeDidChange
(返回零高度,以便使用自动布局"):
and widgetActiveDisplayModeDidChange
(which returns zero height so that it is using Auto Layout instead):
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
if activeDisplayMode == .expanded {
print("EXPANDED")
preferredContentSize = CGSize(width: 0.0, height: 0.0)
setupLabel()
} else if activeDisplayMode == .compact {
print("COMPACT")
preferredContentSize = maxSize
setupLabel()
}
}
和setupLabel
(将其删除并添加):
and setupLabel
(which removes it and adds it):
func setupLabel() {
lbl.removeFromSuperview()
view.addSubview(lbl)
lbl.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 18).isActive = true
lbl.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -18).isActive = true
lbl.topAnchor.constraint(equalTo: view.topAnchor, constant: 18).isActive = true
lbl.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -18).isActive = true
lbl.setContentCompressionResistancePriority(1000, for: .vertical)
}
如果扩展名是.expanded
中的STARTED
,则看起来正确:
If the extension is STARTED
in .expanded
it looks correct:
如果按下显示较少",则看起来正确:
if 'Show less' is pressed it looks correct:
但是,如果按下显示更多",它将不会再次扩展:
however, if 'Show more' is pressed, then it wont expand again:
推荐答案
这是因为当您按下显示更多" 时,会调用widgetActiveDisplayModeDidChange
方法,而activeDisplayMode
为expanded
.
This is because when you press "Show More", widgetActiveDisplayModeDidChange
method is called with activeDisplayMode
as expanded
.
根据您的代码,在扩展模式下,将preferredContentSize
设置为:
According to your code, in expanded mode you are setting the preferredContentSize
as:
preferredContentSize = CGSize(width: 0.0, height: 0.0)
因此,它占用了窗口小部件所允许的最小高度,即110.
So, it takes the minimum height allowed for widget, i.e. 110.
尝试一下:
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize)
{
if activeDisplayMode == .expanded
{
let size = self.sampleLabel.systemLayoutSizeFitting(self.sampleLabel.bounds.size)
preferredContentSize = CGSize(width: 0.0, height: size.height)
}
else
{
preferredContentSize = maxSize
}
}
这篇关于在今日扩展中使用自动布局的动态高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!