容器视图中的按钮在第二个视图中不起作用

Button in container view dosen't work in second view

因此,我有 2 个视图,中间有一个按钮。该按钮被约束到中心视图。当我滑动到另一个视图时,按钮移动到另一个视图的第二个位置(可见)。我的问题是我无法从其他视图访问按钮的功能,因此该按钮不起作用。它是用 contentView 创建的:

这是我的代码:

    import UIKit

class MainVC: UIViewController {

    let setupMainVC = SetupMainVC()

    var rightViewXConstant : NSLayoutConstraint!
    var leftViewXConstant : NSLayoutConstraint!
    var arrowLeftXConstant : NSLayoutConstraint!
    var centerViewXConstant : NSLayoutConstraint!

    var rightViewOpen = false
    var leftViewOpen = false

    var windowSize = CGFloat()

    override func viewDidLoad() {
        super.viewDidLoad()

        let window = UIWindow.init(frame: UIScreen.main.bounds)
        window.makeKeyAndVisible()

        view.backgroundColor = UIColor.clear

        setupMainVC.mainVC = self
        setupMainVC.setupViews()

        windowSize = window.frame.size.width

        setupLeftViewController()
        setupCenterViewController()


        navigationItem.title = "Center View"

        self.setupMainVC.arrowButtonLeft.transform = CGAffineTransform(rotationAngle: 0)
        arrowLeftXConstant.constant = 20

        print(windowSize)

        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Left", style: .plain, target: self, action: #selector(leftButAction))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Right", style: .plain, target: self, action: #selector(rightButAction))
        setupMainVC.arrowButtonLeft.addTarget(self, action: #selector(leftButAction), for: .touchUpInside)

        NotificationCenter.default.addObserver(self, selector: #selector(leftButAction), name: NSNotification.Name("leftButtonAction"), object: nil)
    }

    func leftButAction() {
        print("Left Action")

        if leftViewOpen {
            leftViewOpen = false
            centerViewXConstant.constant = 0
            leftViewXConstant.constant = 0
            self.setupMainVC.arrowButtonLeft.transform = CGAffineTransform(rotationAngle: 0)
            arrowLeftXConstant.constant = 20
            navigationItem.title = "Center View"
            print(false)
        } else {
            leftViewOpen = true
            centerViewXConstant.constant = windowSize
            leftViewXConstant.constant = windowSize
            self.setupMainVC.arrowButtonLeft.transform = CGAffineTransform(rotationAngle: -179)
            arrowLeftXConstant.constant = -20
            navigationItem.title = "Left View"
            print(true)
        }

        UIView.animate(withDuration: 0.3) {
            self.view.layoutIfNeeded()
        }

    }

    func rightButAction() {
        print("Right Action")

        if rightViewOpen {
            rightViewOpen = false
            print(false)
        } else {
            rightViewOpen = true
            print(true)
        }

        UIView.animate(withDuration: 0.3) {
            self.view.layoutIfNeeded()
        }
    }

    func center() {
        let center = CenterVC()
        addChildViewController(center)
        center.view.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(center.view)

        centerViewXConstant = center.view.centerXAnchor.constraint(equalTo: center.view.centerXAnchor, constant: 0)
        centerViewXConstant.isActive = true
        center.view.centerYAnchor.constraint(equalTo: center.view.centerYAnchor).isActive = true
        center.view.heightAnchor.constraint(equalTo: center.view.heightAnchor).isActive = true
        center.view.widthAnchor.constraint(equalTo: center.view.widthAnchor).isActive = true
        center.view.backgroundColor = .blue

        center.didMove(toParentViewController: self)
    }

    //MARK: Center View
    func setupCenterViewController() {
        let controller = CenterVC()

        addChildViewController(controller)
        controller.view.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(controller.view)
        controller.view.addSubview(setupMainVC.roundViewOnLeftView)
        controller.view.addSubview(setupMainVC.arrowButtonLeft)

        centerViewXConstant = controller.view.leftAnchor.constraint(equalTo: setupMainVC.leftView.rightAnchor, constant: 0)
        centerViewXConstant.isActive = true

        controller.view.centerYAnchor.constraint(equalTo: setupMainVC.leftView.centerYAnchor).isActive = true
        controller.view.heightAnchor.constraint(equalTo: setupMainVC.leftView.heightAnchor).isActive = true
        controller.view.widthAnchor.constraint(equalTo: setupMainVC.leftView.widthAnchor).isActive = true

        setupMainVC.roundViewOnLeftView.centerXAnchor.constraint(equalTo: controller.view.leftAnchor).isActive = true
        setupMainVC.roundViewOnLeftView.centerYAnchor.constraint(equalTo: controller.view.centerYAnchor).isActive = true
        setupMainVC.roundViewOnLeftView.heightAnchor.constraint(equalToConstant: 100).isActive = true
        setupMainVC.roundViewOnLeftView.widthAnchor.constraint(equalToConstant: 100).isActive = true

        arrowLeftXConstant = setupMainVC.arrowButtonLeft.centerXAnchor.constraint(equalTo: setupMainVC.roundViewOnLeftView.centerXAnchor, constant: 20)
        arrowLeftXConstant.isActive = true

        setupMainVC.arrowButtonLeft.centerYAnchor.constraint(equalTo: setupMainVC.roundViewOnLeftView.centerYAnchor).isActive = true
        setupMainVC.arrowButtonLeft.heightAnchor.constraint(equalToConstant: 100).isActive = true
        setupMainVC.arrowButtonLeft.widthAnchor.constraint(equalToConstant: 100).isActive = true

        controller.didMove(toParentViewController: self)
    }

    //MARK: Left View
    func setupLeftViewController() {
        let controller = LeftVC()
        //let navBar: UINavigationController = UINavigationController(rootViewController: controller)
        addChildViewController(controller)
        controller.view.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(controller.view)

        leftViewXConstant = controller.view.rightAnchor.constraint(equalTo: setupMainVC.centerView.leftAnchor, constant: 0)
        leftViewXConstant.isActive = true

        controller.view.centerYAnchor.constraint(equalTo: setupMainVC.centerView.centerYAnchor).isActive = true
        controller.view.heightAnchor.constraint(equalTo: setupMainVC.centerView.heightAnchor).isActive = true
        controller.view.widthAnchor.constraint(equalTo: setupMainVC.centerView.widthAnchor).isActive = true

        controller.didMove(toParentViewController: self)
    }
}

设法解决问题。小事总是大麻烦。 由于这部分功能,它没有工作:

controller.view.addSubview(setupMainVC.roundViewOnLeftView)
controller.view.addSubview(setupMainVC.arrowButtonLeft)

要使按钮正常工作,它需要是 MainView(容器)的子视图,这样它将位于同一视图层次结构中,如下所示:

view.addSubview(setupMainVC.roundViewOnLeftView)
view.addSubview(setupMainVC.arrowButtonLeft)