iOS14-ViewHierarchy的更改确实阻止UIView扩展正常工作 [英] iOS14 - ViewHierarchy changes do prevent UIView extension to work
问题描述
使用iOS14.0.1,Xcode12.0.1和Swift5.3,
Using iOS14.0.1, Xcode12.0.1 and Swift5.3,
在iOS14中,以下UIView扩展名无法正常工作:
With iOS14, the following UIView-extension no longer works properly:
@discardableResult
func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding: UIEdgeInsets = .zero, size: CGSize = .zero) -> AnchoredConstraints {
translatesAutoresizingMaskIntoConstraints = false
var anchoredConstraints = AnchoredConstraints()
if let top = top {
anchoredConstraints.top = topAnchor.constraint(equalTo: top, constant: padding.top)
}
if let leading = leading {
anchoredConstraints.leading = leadingAnchor.constraint(equalTo: leading, constant: padding.left)
}
if let bottom = bottom {
anchoredConstraints.bottom = bottomAnchor.constraint(equalTo: bottom, constant: -padding.bottom)
}
if let trailing = trailing {
anchoredConstraints.trailing = trailingAnchor.constraint(equalTo: trailing, constant: -padding.right)
}
if size.width != 0 {
anchoredConstraints.width = widthAnchor.constraint(equalToConstant: size.width)
}
if size.height != 0 {
anchoredConstraints.height = heightAnchor.constraint(equalToConstant: size.height)
}
[anchoredConstraints.top, anchoredConstraints.leading, anchoredConstraints.bottom, anchoredConstraints.trailing, anchoredConstraints.width, anchoredConstraints.height].forEach{ $0?.isActive = true }
return anchoredConstraints
}
自iOS14发行以来,我仅观察到该问题.以前,在iOS12和iOS13上,我从来没有遇到过问题,并且确实使用了很多扩展.
I only observe the problem since iOS14 release. Before, with iOS12 and iOS13 I never had a problem and I did use this extension a lot.
但是现在使用iOS14,它会导致UIControls不再起作用的问题.
But now with iOS14 it leads to problems with UIControls no longer working.
因此,如果我使用扩展名并执行以下操作-那么ViewHierarchy中的所有UIControl均无法正常工作(即不再执行target-actions,switch-didChanges等)
Therefore, if I use the extension and do the following - then any UIControls in my ViewHierarchy do not work (i.e. no target-actions, switch-didChanges, etc will no longer kick-in)
SettingsStackView.anchor(top: safeAreaLayoutGuide.topAnchor, leading: contentView.leadingAnchor, bottom: contentView.bottomAnchor, trailing: contentView.trailingAnchor)
但是,如果我没有UIView扩展名,那么一切都会很好:
However, if I do it without the UIView-extension, then everything works fine:
SettingsStackView.translatesAutoresizingMaskIntoConstraints = false
SettingsStackView.topAnchor.constraint(equalTo: contentView.safeAreaLayoutGuide.topAnchor).isActive = true
SettingsStackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
SettingsStackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
SettingsStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
请参见我的其他示例,其中我用更多的代码描述了问题.
See my other example where I describe the problem with more Code.
有人能告诉我为什么上面的UIView-extension是UIControls的根本原因,当它用于约束代码中的布局约束时,UIControls不再对触摸事件做出反应吗?
Can anybody tell me why the above UIView-extension is the root-cause of UIControls no longer reacting to touch events when used to constrain layout-constraints in code ?
推荐答案
除非您要显示的代码中有错字...
Unless you have a typo in the code you're showing...
使用.anchor
函数的代码行是:
SettingsStackView.anchor(top: safeAreaLayoutGuide.topAnchor, leading: contentView.leadingAnchor, bottom: contentView.bottomAnchor, trailing: contentView.trailingAnchor)
但是没有该功能的代码是:
but your code without that func is:
SettingsStackView.topAnchor.constraint(equalTo: contentView.safeAreaLayoutGuide.topAnchor).isActive = true
请注意,第一行正在使用:
Note that the first line is using:
safeAreaLayoutGuide.topAnchor
第二行正在使用:
contentView.safeAreaLayoutGuide.topAnchor
这篇关于iOS14-ViewHierarchy的更改确实阻止UIView扩展正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!