[英]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,我也懷疑它是否識別任何用戶交互,如觸摸。
到目前為止我已經嘗試過/我的分析到目前為止:
我為問題中提到的 UITableView 設置了 isUserInteractionEnabled true。
為了檢查觸摸事件是否被識別,我在它的選擇器 function 中添加了帶有打印語句的 UITapGestureRecognizer。此外,在 touchesBegan function 中添加了打印語句。它們都沒有被打印出來。
我嘗試使用將 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.