добавить распознаватель жестов uivew панель навигации swift не работает - PullRequest
0 голосов
/ 24 июня 2018

У меня есть контроллер навигации с панелью навигации, я добавил UIView с подпредставлением imageView и UILabel для titleView.Мне нужно иметь возможность щелкнуть по этому представлению, чтобы сделать что-то еще с addGestureRecognizer для этого представления, но на консоли ничего не печатается.UIImageView должен быть рядом с UILabel

Вот код, который я пробовал до сих пор

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")
}

Надеюсь, кто-то может помочь мне с этой проблемой, большое спасибо !!

ОБНОВЛЕНИЕ, где мне нужно добавить распознаватель жестов

1 Ответ

0 голосов
/ 29 июня 2018

Добавьте этот метод в 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

...