2

我必须UITableView在不同的单元格中显示一系列数据。我添加了一个UIContextMenuInteractiontableView以便我可以与每个单元格交互并获得预览。当我在任何单元格上执行交互时,它首先会给出一堆约束警告。在警告中,它提到了groupView我从不引用或访问的 a,除非它是默认的上下文菜单内容。

此外,当退出交互时,tableView 在恢复正常之前通过将 tableView 向下移动来表现怪异 - 请参见此处:https ://streamable.com/3g5byj


代码:

extension ProjectsViewController: UIContextMenuInteractionDelegate {
    private func setupContextMenuInteraction() {
        let interaction = UIContextMenuInteraction(delegate: self)
        tableView.addInteraction(interaction)
    }

    func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        guard let indexPath = tableView.indexPathForRow(at: location), let _ = tableView.cellForRow(at: indexPath) else { return nil }
        let project = projects[indexPath.section]

        return UIContextMenuConfiguration(identifier: "\(indexPath.section)" as NSCopying, previewProvider: {
            let projectViewController = ProjectViewController(project: project)
            return projectViewController
        }) { actions -> UIMenu? in
            let favourite = UIAction(title: "Favourite", image: UIImage(systemName: "star"), identifier: nil) { _ in
            }

            let share = UIAction(title: "Share", image: UIImage(systemName: "square.and.arrow.up"), identifier: nil) { _ in
                guard let url = URL(string: project.url) else { return }

                let activityController = UIActivityViewController(activityItems: [url], applicationActivities: nil)

                DispatchQueue.main.async {
                    self.present(activityController, animated: true, completion: nil)
                }
            }

            return UIMenu(title: "", children: [favourite, share])
        }
    }
}

约束警告:

2020-05-16 19:56:40.398521+0200 MyApp[2690:219904] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x600002b75180 h=--& v=--& UIInterfaceActionGroupView:0x7fc782c500f0.height == 0   (active)>",
    "<NSLayoutConstraint:0x600002b79950 groupView.actionsSequence....height >= 66   (active, names: groupView.actionsSequence...:0x7fc78311c800 )>",
    "<NSLayoutConstraint:0x600002b794f0 UIInterfaceActionGroupView:0x7fc782c500f0.top == _UIContentConstraintsLayoutGuide:0x7fc782c4eb30''.top   (active)>",
    "<NSLayoutConstraint:0x600002b79540 V:[_UIContentConstraintsLayoutGuide:0x7fc782c4eb30'']-(0)-|   (active, names: '|':UIInterfaceActionGroupView:0x7fc782c500f0 )>",
    "<NSLayoutConstraint:0x600002b79f40 groupView.actionsSequence....top == _UIContentConstraintsLayoutGuide:0x7fc782c4eb30''.top   (active, names: groupView.actionsSequence...:0x7fc78311c800 )>",
    "<NSLayoutConstraint:0x600002b79f90 groupView.actionsSequence....bottom == _UIContentConstraintsLayoutGuide:0x7fc782c4eb30''.bottom   (active, names: groupView.actionsSequence...:0x7fc78311c800 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600002b79950 groupView.actionsSequence....height >= 66   (active, names: groupView.actionsSequence...:0x7fc78311c800 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
4

0 回答 0