添加手势识别器uivew导航栏swift不起作用

时间:2018-06-24 15:17:26

标签: swift uiview uinavigationcontroller uinavigationbar uitapgesturerecognizer

我有一个带导航栏的导航控制器,我添加了一个带有imageView和UILabel的子视图的UIView用于titleView。 我需要能够单击该视图以在该视图上使用addGestureRecognizer进行其他操作,但控制台上未打印任何内容。 UIImageView必须位于UILabel旁边

这是我到目前为止尝试过的代码

holder

希望有人可以帮助我解决这个问题,非常感谢!!

UPDATE this is where I need to add a gesture recognizer

1 个答案:

答案 0 :(得分:2)

在viewDidLoad / viewWillAppear中添加此方法

 if let navigationBar = self.navigationController?.navigationBar {
        // create a button
         let button =  UIButton(type: .custom)
        button.frame = CGRect(x: navigationBar.frame.width/2-20, y: 0, width: 100, height: 40)
        button.setTitleColor(.red, for: .normal)
         button.setTitle("Button", for: .normal)
        button.addTarget(self, action: #selector(self.testing), for: .touchUpInside)
        // create a imageview
        let profileImageView = UIImageView()
         profileImageView.contentMode = .scaleAspectFill
        profileImageView.layer.cornerRadius = 20
        profileImageView.clipsToBounds = true
         profileImageView.frame = CGRect(x: navigationBar.frame.width/2-60, y: 0, width: 40, height: 40)
        profileImageView.image = UIImage(named: "heart")
        // Add two elements to navigationbar
        navigationBar.addSubview(profileImageView)
        navigationBar.addSubview(button)

    }

See this image

相关问题