【问题标题】:iOS14 - ViewHierarchy changes do prevent UIView extension to workiOS 14 - 查看层次结构更改以防止 UIView 扩展工作
【发布时间】:2021-01-22 08:15:10
【问题描述】:

使用 iOS14.0.1、Xcode12.0.1 和 Swift5.3,

在 iOS14 中,以下 UIView 扩展不再正常工作:


@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 我从来没有遇到过问题,而且我确实经常使用这个扩展。

但现在使用 iOS14 会导致 UIControls 不再工作的问题。

因此,如果我使用扩展并执行以下操作 - 那么我的 ViewHierarchy 中的任何 UIControls 都不起作用(即不再启动目标操作、switch-didChanges 等)

SettingsStackView.anchor(top: safeAreaLayoutGuide.topAnchor, leading: contentView.leadingAnchor, bottom: contentView.bottomAnchor, trailing: contentView.trailingAnchor)

但是,如果我在没有 UIView 扩展的情况下执行此操作,则一切正常:

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

请参阅my other example,我用更多代码描述了问题。

谁能告诉我为什么上面的 UIView 扩展是 UIControls 在用于约束代码中的布局约束时不再对触摸事件做出反应的根本原因?

【问题讨论】:

    标签: swift uiview hierarchy nslayoutconstraint stackview


    【解决方案1】:

    除非您在显示的代码中有错字...

    使用.anchor 函数的代码行是:

    SettingsStackView.anchor(top: safeAreaLayoutGuide.topAnchor, leading: contentView.leadingAnchor, bottom: contentView.bottomAnchor, trailing: contentView.trailingAnchor)
    

    但是没有该功能的代码是:

    SettingsStackView.topAnchor.constraint(equalTo: contentView.safeAreaLayoutGuide.topAnchor).isActive = true
    

    注意第一行使用的是:

    safeAreaLayoutGuide.topAnchor
    

    第二行正在使用:

    contentView.safeAreaLayoutGuide.topAnchor
    

    【讨论】:

    • 哦,伙计-你很好!就是这样!。 UIView-extension 仍在工作 - 但我必须注意将contentView 用于所有锚点。我根本没看到。但是,我仍然不清楚为什么没有“contentView”的锚可以在 iOS12 和 iOS13 下工作,而不是在 iOS14 下工作??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多