gpt4 book ai didi

ios - 在今日扩展中使用自动布局的动态高度

转载 作者:行者123 更新时间:2023-12-01 19:57:50 25 4
gpt4 key购买 nike

编辑:我的“原始”问题已解决,被"<NSAutoresizingMaskLayoutConstraint:0x6000000985b0 h=--& v=--& UIView:0x7fc83af0aeb0.height == 110 (active)>"限制为110高度,但这是在我读到显然是在iOS 10中引入的NCWidgetDisplayMode.Compact.Expanded之前的。

有趣的是:在iOS 10(https://developer.apple.com/videos/play/wwdc2016/101/?time=3221)中展示新功能时看起来像是有bug。

尽管实现了该功能,但我仍然无法获得正确的高度调整。请参阅下面的相关代码详细信息,并查看完整的源代码: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(返回零高度,因此它使用“自动布局”):
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(将其删除并添加):
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)
}

如果扩展名是 STARTED中的 .expanded,则看起来正确:

enter image description here

如果按下“显示较少”,则看起来正确:

enter image description here

但是,如果按“显示更多”,则它将不会再次扩展:

enter image description here

最佳答案

这是因为当您按下“显示更多” 时,widgetActiveDisplayModeDidChange方法被称为activeDisplayMode作为expanded

根据您的代码,在扩展模式下,您将preferredContentSize设置为:

preferredContentSize = CGSize(width: 0.0, height: 0.0)

因此,它占用了窗口小部件允许的最小高度,即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
}
}

关于ios - 在今日扩展中使用自动布局的动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41524154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com