ios - subview 将触摸传递给其下方的 subview

标签 ios swift uiview subview

我添加了一个 subview ,如下所示:

func showPlayerView2() {
    self.view.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height - 56)
    //let theView = myView
    let theView = PlayerView()
    theView.willMove(toParentViewController: self.navigationController)
    self.navigationController?.addChildViewController(theView)
    theView.view.isUserInteractionEnabled = true
    theView.playPauseButton.isUserInteractionEnabled = true
    self.navigationController?.view.bringSubview(toFront: theView.view)
    self.navigationController?.view.addSubview(theView.view)
    theView.view.frame = CGRect(x: 0, y: self.view.frame.maxY, width: 375, height: 56)

    theView.didMove(toParentViewController: self.navigationController)
    self.view.layoutSubviews()
}

PlayerView 有一个按钮,按下时应该在屏幕上打印一些内容。

即使将 isUserInteractionEnabled 设置为 true,它仍然不会注册触摸,并且它们会传递到其下方的 View 。我几乎查看了所有与此相关的问题,但没有一个答案有效。我可以做什么来解决这个问题?

最佳答案

如果您正在寻找通用的自定义导航菜单,请尝试此代码

  func setupPageNavigationBar(){
        navigationController?.navigationBar.barTintColor = UIColor(red:1.00, green:0.22, blue:0.22, alpha:1.0)
        navigationController?.navigationBar.translucent = false

        let backButton = UIButton(type:.System)
        backButton.setImage(UIImage(named: "img_back")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Normal)
        backButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)

        let imgButton = UIButton(type:.System)
        imgButton.setImage(UIImage(named: "img_logo_bar")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Normal)
        imgButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)


        navigationItem.leftBarButtonItems  = [UIBarButtonItem(customView: backButton),UIBarButtonItem(customView:imgButton)]
        backButton.addTarget(self, action: #selector(self.goBack), forControlEvents: .TouchUpInside)

        let titleLabel = UILabel()
        titleLabel.text = "SOME TITLE"
        let titleLabel2 = UILabel()
        titleLabel2.text = "Some text"

        titleLabel.font = UIFont.boldSystemFontOfSize(15)
        titleLabel.textColor = UIColor.whiteColor()
        titleLabel.textAlignment = .Left

        titleLabel2.font = UIFont.systemFontOfSize(11)
        titleLabel2.textColor = UIColor.whiteColor()
        titleLabel2.textAlignment = .Left

        let titleView = UIView()
        titleView.frame = CGRect(x: 0, y: 0, width: view.frame.width-90, height: 50)
        //titleView.backgroundColor = UIColor.whiteColor()

        titleView.addSubview(titleLabel)
        titleLabel.translatesAutoresizingMaskIntoConstraints = false

        titleView.addSubview(titleLabel2)
        titleLabel2.translatesAutoresizingMaskIntoConstraints = false


        //titleLabel.frame = CGRect(x: 0, y: 0, width: 134, height: 34)


        let viewDict = ["titleLabel":titleLabel,"titleLabel2":titleLabel2]

        titleView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[titleLabel]-0-|", options: NSLayoutFormatOptions(), metrics: nil, views: viewDict))
        titleView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[titleLabel2]-0-|", options: NSLayoutFormatOptions(), metrics: nil, views: viewDict))

        titleView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-5-[titleLabel(20)]-0-[titleLabel2(15)]", options: NSLayoutFormatOptions(), metrics: nil, views: viewDict))

        //5+20+5+15+5
        navigationItem.titleView = titleView

        let homeButton = UIButton(type:.System)
        homeButton.setImage(UIImage(named: "img_home")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Normal)
        homeButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        navigationItem.rightBarButtonItem =  UIBarButtonItem(customView: homeButton)
        homeButton.addTarget(self, action:  #selector(self.gotoHome), forControlEvents: .TouchUpInside)
        //CGRectMake()



    }

在 viewController 中

override func viewDidLayoutSubviews()
    {

        self.setupPageNavigationBar()
    }

关于ios - subview 将触摸传递给其下方的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47106508/

相关文章:

swift - '[UILabel ]' does not have a member named ' 文本'

iOS在UIView上闪烁动画的次数有限

objective-c - 通过滑动 UIView 滑出导航以覆盖另一个 UIView

ios - 识别 subview 的类

ios - UICollectionView:更新节页脚标题

ios - 无法访问 [AnyHashable :Any] - Swift 4 类型的值

ios - BLE特性通知属性的本质是什么?

ios - 停止 AudioUnit 录制后出现错误

ios - 高度错误的模态 UINavigationController 栏

ios - 解析键包含 A 或 B 的查询 - Swift