添加手势识别器 uivew 导航栏 swift 不起作用 [英] add gesture recognizer uivew navigation bar swift not working

查看:23
本文介绍了添加手势识别器 uivew 导航栏 swift 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我目前尝试过的代码

private func setupNavBarWithUser() {让 titleView = UIView()让宽度 = titleView.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)).widthtitleView.frame = CGRect(origin:CGPoint.zero, size:CGSize(width: width, height: 500))让 profileImageView = UIImageView()profileImageView.translatesAutoresizingMaskIntoConstraints = falseprofileImageView.contentMode = .scaleAspectFillprofileImageView.layer.cornerRadius = 20profileImageView.clipsToBounds = trueImageService.getImage(withURL: NSURL(string: (user?.pictureURL)!)! as URL) { (image) inprofileImageView.image = 图像}titleView.addSubview(profileImageView)profileImageView.leftAnchor.constraint(equalTo: titleView.leftAnchor).isActive = trueprofileImageView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = trueprofileImageView.widthAnchor.constraint(equalToConstant: 40).isActive = trueprofileImageView.heightAnchor.constraint(equalToConstant: 40).isActive = true让 nameLabel = UILabel()titleView.addSubview(nameLabel)nameLabel.text = user?.first_namenameLabel.textColor = .whitenameLabel.translatesAutoresizingMaskIntoConstraints = falsenameLabel.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, 常量: 8).isActive = truenameLabel.centerYAnchor.constraint(equalTo: profileImageView.centerYAnchor).isActive = truenameLabel.rightAnchor.constraint(equalTo: titleView.rightAnchor).isActive = truenameLabel.heightAnchor.constraint(equalTo: profileImageView.heightAnchor).isActive = truetitleView.centerXAnchor.constraint(equalTo: titleView.centerXAnchor).isActive = truetitleView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = trueself.navigationItem.titleView = titleView让识别器 = UITapGestureRecognizer(target: self, action: #selector(self.testing))titleView.isUserInteractionEnabled = truetitleView.addGestureRecognizer(识别器)}@objc 功能测试(){打印(你好")}

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

I have a navigation controller with a navigation bar, I have added a UIView with subview of imageView and UILabel for titleView. I need to be able to click on that view to do something else with addGestureRecognizer on that view but nothing is printed on the console. The UIImageView has to be next to the UILabel

Here is the code I tried so far

private func setupNavBarWithUser() {
    let titleView = UIView()
    let width = titleView.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)).width
    titleView.frame = CGRect(origin:CGPoint.zero, size:CGSize(width: width, height: 500))

    let profileImageView = UIImageView()
    profileImageView.translatesAutoresizingMaskIntoConstraints = false
    profileImageView.contentMode = .scaleAspectFill
    profileImageView.layer.cornerRadius = 20
    profileImageView.clipsToBounds = true
    ImageService.getImage(withURL: NSURL(string: (user?.pictureURL)!)! as URL) { (image) in
        profileImageView.image = image
    }
    titleView.addSubview(profileImageView)
    profileImageView.leftAnchor.constraint(equalTo: titleView.leftAnchor).isActive = true
    profileImageView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true
    profileImageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
    profileImageView.heightAnchor.constraint(equalToConstant: 40).isActive = true

    let nameLabel = UILabel()
    titleView.addSubview(nameLabel)
    nameLabel.text = user?.first_name
    nameLabel.textColor = .white
    nameLabel.translatesAutoresizingMaskIntoConstraints = false
    nameLabel.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8).isActive = true
    nameLabel.centerYAnchor.constraint(equalTo: profileImageView.centerYAnchor).isActive = true
    nameLabel.rightAnchor.constraint(equalTo: titleView.rightAnchor).isActive = true
    nameLabel.heightAnchor.constraint(equalTo: profileImageView.heightAnchor).isActive = true

    titleView.centerXAnchor.constraint(equalTo: titleView.centerXAnchor).isActive = true
    titleView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true

    self.navigationItem.titleView = titleView
    let recognizer = UITapGestureRecognizer(target: self, action: #selector(self.testing))
    titleView.isUserInteractionEnabled = true
    titleView.addGestureRecognizer(recognizer)
}

@objc func testing() {
    print("hello")
}

Hope someone can help me with this problem, much thank you !!

UPDATE this is where I need to add a gesture recognizer

解决方案

Add this method in 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)

    }

这篇关于添加手势识别器 uivew 导航栏 swift 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆