【问题标题】:iOS14: no UIControl working anymore in a UIKit Modal ViewControlleriOS14:UIKit 模态视图控制器中不再有 UIControl 工作
【发布时间】:2021-01-19 20:53:15
【问题描述】:

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

我对这个问题感到绝望:

使用 iOS12 和 iOS13,我的应用运行良好。

但是现在有了 iOS14,没有 UIButton,没有 UISwitch,没有 segue - 我的 Modal ViewController 没有任何工作。

我不知道 Apple 在 UIKit 中做了什么改变导致这个问题发生???? !!!!

请帮忙!

我的 Modal ViewController 呈现如下:

@objc func settingsBtnPressed(_ sender: UIButton) {
        
    let settingsVC = SettingsViewController()
    settingsVC.backDelegate = self

    let navController = UINavigationController(rootViewController: settingsVC)
    navController.navigationBar.barStyle = .black
    navController.navigationBar.tintColor = .white
    navController.navigationBar.prefersLargeTitles = true
    navController.navigationBar.backgroundColor = UIColor.clear
    navController.presentationController?.delegate = self

    self.present(navController, animated: true)
}

class SettingsViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
    
    let cellId = "cellID"
    
    init() {
        let collectionViewFlowLayout = UICollectionViewFlowLayout()
        collectionViewFlowLayout.estimatedItemSize = CGSize(width: UIScreen.main.bounds.width, height: 1)
        super.init(collectionViewLayout: collectionViewFlowLayout)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()        
        // ...                        
        collectionView.register(SettingsCollectionViewCell.self, forCellWithReuseIdentifier: cellId)
    }
        
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! SettingsCollectionViewCell
        cell.setup(titlesFirstSection: titlesFirstSection, iconsFirstSection: iconsFirstSection, onStatesFirstSection: onStatesFirstSection, )
        return cell
    }
}
class SettingsCollectionViewCell: UICollectionViewCell {
    
    var SettingsStackView = UIStackView()
    var attries: UICollectionViewLayoutAttributes!
    var myContentHeight: CGFloat = 0.0
    
    var firstSettingsSectionView: MenuSwitchListSectionView?
    
    let elementHeight: CGFloat = 44.0
    let separatorLineThickness: CGFloat = 1.0
    let sectionViewBackgroundColor = ImageConverter.UIColorFromRGB(0x2C2C2E, alpha: 1.0)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
        
    func setup(titlesFirstSection: [String],
                iconsFirstSection: [UIImage?]?,
             onStatesFirstSection: [Bool]) {
                
        myContentHeight = UIScreen.main.bounds.height - 120
                                
        initFirstSettingsSectionView(titles: titlesFirstSection, icons: iconsFirstSection, onStates: onStatesFirstSection)
        
        SettingsStackView = VerticalStackView(arrangedSubviews: [
            UIView(),
            firstSettingsSectionView! as UIView,
            UIView()
        ], spacing: 10.0, alignment: .center )
        
        contentView.addSubview(SettingsStackView)
        contentView.isUserInteractionEnabled = true
        
        setConstraints(nrOfItemsFirstSection: titlesFirstSection.count)
    }
    
    fileprivate func initFirstSettingsSectionView(titles: [String], icons: [UIImage?]?, onStates: [Bool]) {

        firstSettingsSectionView = MenuSwitchListSectionView(frame: .zero,
                                  elementHeight: elementHeight,
                         separatorLineThickness: separatorLineThickness,
                                backgroundColor: sectionViewBackgroundColor,
                                         titles: titles,
                                          icons: icons,
                                       onStates: onStates
        )
        firstSettingsSectionView?.switchMutatedDelegate = self
        firstSettingsSectionView?.assignDelegate()
    }
    
    fileprivate func setConstraints(nrOfItemsFirstSection: Int) {

        contentView.translatesAutoresizingMaskIntoConstraints = false
        SettingsStackView.translatesAutoresizingMaskIntoConstraints = false
        firstSettingsSectionView?.translatesAutoresizingMaskIntoConstraints = false
        
        SettingsStackView.anchor(top: safeAreaLayoutGuide.topAnchor, leading: contentView.leadingAnchor, bottom: nil, trailing: contentView.trailingAnchor)
                        
        let totalHeightFirstSection: CGFloat = elementHeight * CGFloat(nrOfItemsFirstSection) + separatorLineThickness * CGFloat(nrOfItemsFirstSection - 1)
        firstSettingsSectionView?.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8.0).isActive = true
        firstSettingsSectionView?.heightAnchor.constraint(equalToConstant: totalHeightFirstSection).isActive = true
        firstSettingsSectionView?.widthAnchor.constraint(equalToConstant: contentView.bounds.width - 16.0).isActive = true
    }
class MenuSwitchElementStackView: UIStackView {
    
    weak var switchMutatedDelegate: SwitchMutatedDelegate?

    let imgViewWidth: CGFloat = 23.0
        
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    convenience init(frame: CGRect, text: String, icon: UIImage?, onState: Bool, switchID: Int) {
        self.init(frame: frame)
        composeStackView(text: text, icon: icon, onState: onState, switchID: switchID)
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func composeStackView(text: String, icon: UIImage?, onState: Bool, switchID: Int) {
        
        // StackView properties
        axis = .horizontal
        alignment = .center
        spacing = 23.0
        
        var iconImgView: UIImageView?
        
        // create Icon
        if let icon = icon {
            iconImgView = UIImageView()
            iconImgView?.image = icon
            iconImgView?.contentMode = .scaleAspectFit
        }
        
        // create Title
        let titleLabel = UILabel()
        titleLabel.font = AppConstants.Font.ListSectionElementTitleFont
        titleLabel.text = text
        titleLabel.textColor = .white
        titleLabel.tintColor = .white
        titleLabel.heightAnchor.constraint(equalToConstant: 19.0).isActive = true
        
        // create choice TextLbl
        let mySwitch = UISwitch()
        mySwitch.tag = switchID
        mySwitch.setOn(onState, animated: false)
        mySwitch.addTarget(self, action: #selector(MenuSwitchElementStackView.switchChanged(_:)), for: UIControl.Event.valueChanged)
        
        // text stackView creation
        if let iconImgView = iconImgView {
            addArrangedSubview(iconImgView)
        }
        addArrangedSubview(titleLabel)
        addArrangedSubview(mySwitch)
        // set Constraints
        setConstraints(iconImgView, titleLabel, mySwitch)
    }
    
    @objc func switchChanged(_ mySwitch: UISwitch) {
        switchMutatedDelegate?.switchChanged(isOn: mySwitch.isOn, switchID: mySwitch.tag)
    }
    
    fileprivate func setConstraints(_ iconImgView: UIImageView?, _ titleLabel: UILabel, _ mySwitch: UISwitch) {
        
        translatesAutoresizingMaskIntoConstraints = false
        iconImgView?.translatesAutoresizingMaskIntoConstraints = false
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        mySwitch.translatesAutoresizingMaskIntoConstraints = false
        
        iconImgView?.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0.0).isActive = true
        iconImgView?.widthAnchor.constraint(equalToConstant: 24.0).isActive = true
        iconImgView?.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true

        titleLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        
        mySwitch.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        mySwitch.leadingAnchor.constraint(equalTo: trailingAnchor, constant: -64.0).isActive = true
    }
}
class MenuSwitchListSectionView: UIView {
    
    var sectionElementsStackView: MenuSwitchListSectionWithElementsStackView?
    weak var switchMutatedDelegate: SwitchMutatedDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    convenience init(frame: CGRect,
             elementHeight: CGFloat,
    separatorLineThickness: CGFloat,
           backgroundColor: UIColor,
                    titles: [String],
                     icons: [UIImage?]?,
                  onStates: [Bool]) {
        self.init(frame: frame)
        composeView(elementHeight: elementHeight, separatorLineThickness: separatorLineThickness, backgroundColor: backgroundColor, titles: titles, icons: icons, onStates: onStates)
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func assignDelegate() {
        self.sectionElementsStackView?.switchMutatedDelegate = switchMutatedDelegate
        sectionElementsStackView?.assignDelegate()
    }
    
    fileprivate func composeView(elementHeight: CGFloat, separatorLineThickness: CGFloat, backgroundColor: UIColor, titles: [String], icons: [UIImage?]?, onStates: [Bool]) {

        // create background View
        let totalHeight: CGFloat = elementHeight * CGFloat(titles.count) + separatorLineThickness * CGFloat(titles.count - 1)
        let backGroundView = ListSectionBackgroundView(frame: .zero, height: totalHeight, backgroundColor: backgroundColor)
               
        // create stackView
        sectionElementsStackView = MenuSwitchListSectionWithElementsStackView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: totalHeight), elementHeight: elementHeight, separatorLineThickness: separatorLineThickness, titles: titles, icons: icons, onStates: onStates)
        
        // add background and stackView to self
        addSubview(backGroundView)
        addSubview(sectionElementsStackView!)
        
        // set Constraints
        translatesAutoresizingMaskIntoConstraints = false
        backGroundView.translatesAutoresizingMaskIntoConstraints = false
        sectionElementsStackView?.translatesAutoresizingMaskIntoConstraints = false
        
        backGroundView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        backGroundView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
        backGroundView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
        backGroundView.heightAnchor.constraint(equalToConstant: totalHeight).isActive = true
        sectionElementsStackView?.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        sectionElementsStackView?.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16.0).isActive = true
        sectionElementsStackView?.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10.0).isActive = true
    }
}
class MenuSwitchListSectionWithElementsStackView: UIStackView {
    
    weak var switchMutatedDelegate: SwitchMutatedDelegate?
    var elementStackView: MenuSwitchElementStackView?
    var elements: [MenuSwitchElementStackView]?
    var titles: [String]? // needed for gesture callback
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    convenience init(frame: CGRect,
             elementHeight: CGFloat,
    separatorLineThickness: CGFloat,
                    titles: [String],
                     icons: [UIImage?]?,
                  onStates: [Bool]) {
        self.init(frame: frame)
        self.titles = titles // needed for gesture callback
        composeStackView(elementHeight: elementHeight, separatorLineThickness: separatorLineThickness, titles: titles, icons: icons, onStates: onStates)
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func assignDelegate() {
        if let elements = elements {
            for element in elements {
                element.switchMutatedDelegate = switchMutatedDelegate
            }
        }
    }
    
    fileprivate func composeStackView(elementHeight: CGFloat, separatorLineThickness: CGFloat, titles: [String], icons: [UIImage?]?, onStates: [Bool]) {

        // stackView properties
        axis = .vertical
        alignment = .leading
        spacing = 0.0
        
        // create stackView elements
        elements = (0..<titles.count).map { (idx) -> MenuSwitchElementStackView in
            if icons?.count ?? 0 > 0 {
                elementStackView = MenuSwitchElementStackView(frame: .zero, text: titles[idx], icon: icons?[idx], onState: onStates[idx], switchID: idx)
            } else {
                elementStackView = MenuSwitchElementStackView(frame: .zero, text: titles[idx], icon: nil, onState: onStates[idx], switchID: idx)
            }
            elementStackView?.isUserInteractionEnabled = true
            elementStackView?.tag = idx
            return elementStackView!
        }
        
        var singleLines = [UIView]()
        let lineThickness: CGFloat = separatorLineThickness
        
        // compose the stackView
        if let elements = elements {
            for (idx, view) in elements.enumerated() {
                
                // add element to stackView
                addArrangedSubview(view)
                
                if idx < titles.count - 1 {
                    // add single Line to stackView
                    let singleLineView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: lineThickness))
                    singleLineView.backgroundColor = ImageConverter.UIColorFromRGB(0x606060, alpha: 1.0)
                    singleLines.append(singleLineView)
                    
                    addArrangedSubview(singleLineView)
                }
            }
        }
        
        // set constraints
        translatesAutoresizingMaskIntoConstraints = false

        if let elements = elements {
            for (idx, element) in elements.enumerated() {
                element.translatesAutoresizingMaskIntoConstraints = false
                element.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
                element.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
                element.heightAnchor.constraint(equalToConstant: elementHeight).isActive = true
                if idx < titles.count - 1 {
                    singleLines[idx].translatesAutoresizingMaskIntoConstraints = false
                    singleLines[idx].topAnchor.constraint(equalTo: element.bottomAnchor).isActive = true
                    singleLines[idx].heightAnchor.constraint(equalToConstant: lineThickness).isActive = true
                    if icons?.count ?? 0 > 0 {
                        if let _ = icons?[idx] {
                            singleLines[idx].leadingAnchor.constraint(equalTo: leadingAnchor, constant: 39.0).isActive = true
                        }
                    } else {
                        singleLines[idx].leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
                    }
                    singleLines[idx].trailingAnchor.constraint(equalTo: trailingAnchor, constant: 9.0).isActive = true
                }
            }
        }
    }
}
class ListSectionBackgroundView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    convenience init(frame: CGRect, height: CGFloat = 60.0, backgroundColor: UIColor) {
        self.init(frame: frame)
        composeView(height: height, backgroundColor: backgroundColor)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func composeView(height: CGFloat, backgroundColor: UIColor) {
        
        // View properties
        self.backgroundColor = backgroundColor
        self.layer.cornerRadius = 10.0
        self.layer.masksToBounds = true
        self.clipsToBounds = true
        
        // set Constraints
        setConstraints(height)
    }
    
    fileprivate func setConstraints(_ height: CGFloat) {
        self.translatesAutoresizingMaskIntoConstraints = false
        self.heightAnchor.constraint(equalToConstant: height).isActive = true
    }
}

【问题讨论】:

  • 我猜你正在添加按钮并直接切换到单元格。不幸的是,您没有显示 SettingsCollectionViewCell,所以这只是一个猜测。
  • 我在原始问题中添加了SettingsCollectionViewCell - 见上文......(......即它非常冗长 - 但我希望你能看到导致 iOS14 问题的原因......)。感谢您的帮助-我对此感到非常绝望:/
  • 很抱歉,我仍然没有看到不起作用的按钮和开关。我现在唯一能想到的是,他们可能不在他们的监管范围内。您可以通过研究 View Debugger 来检查。
  • View-Debugger 看起来不错。我看不出有什么问题。我在原始问题中添加了更多上述视图组合。起初,由于代码比较多,我并没有打算全部呈现。但也许要找到错误,现在更有意义。同样,在 iOS12 和 iOS13 中,一切正常。只有 iOS14 会导致问题。
  • 是的,他们确实改变了一些关于单元格和堆栈视图的东西,所以这就是我所关注的。很抱歉不能从这里提供帮助。

标签: uibutton uikit collectionview uiswitch ios14


【解决方案1】:

我终于找到了解决办法。

由于某种原因,我的代码中的 UIView 扩展“锚”不再适用于 iOS14。

Here I describe the problem more precisely...

因此,如果我使用扩展程序并执行以下操作 - 那么我的 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

也许有人可以告诉我为什么 iOS14 会出现这种新行为???

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多