繁体   English   中英

UITableView 在子视图中不响应触摸事件并且不滚动

[英]UITableView in a subview doesn't respond to touch events and doesn't scroll

语言:Swift 5

iOS :13.2

macOS :卡特琳娜 10.15.4

我的应用程序中有以下结构:

在此处输入图像描述

根据我 select 来自分段控件或屏幕底部按钮的内容,不同的视图作为子视图以下列方式添加到 ContainerView:

在分段控制的情况下:

//viewControllerForSelectedSegmentIndex returns the appropriate UIViewController
if let vc = viewControllerForSelectedSegmentIndex(index: sender.selectedSegmentIndex, 
    control: sender) 
{
        self.addChild(vc)
        self.transition(from: self.currentViewController!, to: vc, duration: 0.5, 
        options:.transitionFlipFromRight, animations: 
        {
            self.currentViewController!.view.removeFromSuperview()
            vc.view.frame = CGRect(x: 0 , y: 130, width: self.view.frame.width, height: 
            self.view.frame.height-250)
            self.containerView.addSubview(vc.view)
            
            }, completion: { finished in
                vc.didMove(toParent: self)
                self.currentViewController!.removeFromParent()
                self.currentViewController = vc
            }
        )
        sender.changeUnderlinePosition()

    }

如果选择其中一个按钮:

    let vc = BrowseController()
    self.addChild(vc)
    self.currentViewController!.view.removeFromSuperview()
    vc.view.frame = CGRect(x: 0 , y: 130, width: self.view.frame.width, height: 
            self.view.frame.height-250)
    self.containerView.addSubview(vc.view)
    vc.didMove(toParent: self)
    self.currentViewController!.removeFromParent()
    self.currentViewController = vc

UIViewController:

每个作为孩子添加的 UIViewController 中都有一个 UIScrollView。 所有 UI 元素都添加到 UIScrollView 内部的视图中。

问题:

几个 viewControllers 包含 UITableView。UITableView 已正确呈现,但是,我无法滚动它。 其中一个 tableview 的单元格内有 UIButton,我也怀疑它是否识别任何用户交互,如触摸。

到目前为止我已经尝试过/我的分析到目前为止:

  1. 我为问题中提到的 UITableView 设置了 isUserInteractionEnabled true。

  2. 为了检查触摸事件是否被识别,我在它的选择器 function 中添加了带有打印语句的 UITapGestureRecognizer。此外,在 touchesBegan function 中添加了打印语句。它们都没有被打印出来。

  3. 我尝试使用将 viewController 的视图放在前面

     self.view.bringSubviewToFront(self.currentViewController.view)

    它也没有帮助!

有人可以在这里指出问题吗?

我有一个子和段的例子,检查下面的例子。

import UIKit

class ViewController: UIViewController {

    let segment = UISegmentedControl(items: ["uno" , "dos","tres"])
    let containerView : UIView = {
        let view = UIView()
        view.backgroundColor = .red
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        addingChild(childViewController: tableViewController)
        
        segment.addTarget(self, action: #selector(selectedTapped), for: .valueChanged)
        self.view.addSubview(segment)
        self.view.addSubview(containerView)
        segment.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            segment.topAnchor.constraint(equalTo: self.view.topAnchor , constant: 50),
            segment.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
            segment.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
            containerView.topAnchor.constraint(equalTo: self.segment.bottomAnchor, constant: 10),
            containerView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
            containerView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
            containerView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
        ])
        
        
        
    }
    let tableViewController = TableViewChild(style: .grouped)
    let secondChild = SecondChild()
   //segment changed
    @objc func selectedTapped(_ segment : UISegmentedControl){
        
        if segment.selectedSegmentIndex == 0 {
            addingChild(childViewController: tableViewController)
            removeChild(childViewController: secondChild)
        }
        
        if segment.selectedSegmentIndex == 1 {
            addingChild(childViewController: secondChild)
            removeChild(childViewController: tableViewController)
            
        }else if segment.selectedSegmentIndex == 2 {
            removeChild(childViewController: tableViewController)
            removeChild(childViewController: secondChild)
        }
        
        
    }
    // remove child
    func removeChild(childViewController : UIViewController){
        willMove(toParent: nil)
        childViewController.view.removeFromSuperview()
        removeFromParent()
    }
    //Add child
    func addingChild(childViewController : UIViewController){
        addChild(childViewController)
        self.containerView.addSubview(childViewController.view)
        childViewController.didMove(toParent: self)
        childViewController.view.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            childViewController.view.topAnchor.constraint(equalTo: containerView.topAnchor),
            childViewController.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
            childViewController.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
            childViewController.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
        ])
    }

}

class SecondChild : UIViewController {
    let label : UILabel = {
        let label = UILabel()
        label.text = "Second Label"
        return label
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .gray
        self.view.addSubview(label)
        label.translatesAutoresizingMaskIntoConstraints = false
        label.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
        label.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
    }
}

class TableViewChild : UITableViewController {
    let elements = ["1","2","3","4","5"]
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return elements.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = elements[indexPath.row]
        return cell
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("hola")
    }
    
}

我建议您使用约束而不是框架来设置您的子维度,在我的示例中,我添加了子项并在按下段时删除了另一个子项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM