Я создал настраиваемый заголовок навигации, создав UIView под названием TitleView, затем в нем я сохраняю UIView под названием containerView и внутри него он содержит UIImageView под названием profileImageView и UILabel. Я хочу включить обработчик касания в UIImageView. Поэтому я попытался добавить его так:
//add touch handler to Profile Image to segue to expanded view
let singleTap = UITapGestureRecognizer(target: self, action: #selector(expandProfile))
profileImageView.isUserInteractionEnabled = true
singleTap.numberOfTouchesRequired = 1
profileImageView.addGestureRecognizer(singleTap)
, а обработчик касания на данный момент просто печатает - но он никогда не вызывается
//action for user clicking on the profile image view
@objc func expandProfile() {
print("THIS WORKS!!!")
}
Я не уверен, почему это не работает . Чтобы попытаться исправить это, я включил UserInteraction на панели навигации, TitleView и Container View, но это не помогло? Я не уверен, что вызывает проблему.
отредактируйте, чтобы увидеть код, который устанавливает мою панель навигации:
//Setting Up nav bar to have image and name
func setupNavBar(){
//We first create a titleview which we will use as the custom titleview in the navigation bar
let titleView = UIView()
titleView.isUserInteractionEnabled = true
titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 60)
//ContainerView will be within the titleview so that it allows it to grow dynamically with size of the name
let containerView = UIView()
containerView.isUserInteractionEnabled = true
containerView.translatesAutoresizingMaskIntoConstraints = false
titleView.addSubview(containerView)
//The view for storing the Profile picture of the match
let profileImageView = UIImageView()
profileImageView.loadImageUsingCacheWithUrlString(urlString: matchImageURLs[0])
profileImageView.translatesAutoresizingMaskIntoConstraints = false
profileImageView.contentMode = .scaleAspectFill
profileImageView.layer.cornerRadius = 20
profileImageView.clipsToBounds = true
containerView.addSubview(profileImageView)
//Constraints for Profile Image
profileImageView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
profileImageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
profileImageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 40).isActive = true
//add touch handler to Profile Image to segue to expanded view
self.navigationController?.navigationBar.isUserInteractionEnabled = true
let singleTap = UITapGestureRecognizer(target: self, action: #selector(expandProfile))
profileImageView.isUserInteractionEnabled = true
singleTap.numberOfTouchesRequired = 1
profileImageView.addGestureRecognizer(singleTap)
self.view.bringSubviewToFront(profileImageView)
//View for the name of the match
let nameLabel = UILabel()
containerView.addSubview(nameLabel)
nameLabel.text = matchName
nameLabel.translatesAutoresizingMaskIntoConstraints = false
//Name Label constraints
nameLabel.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8).isActive = true
nameLabel.centerYAnchor.constraint(equalTo: profileImageView.centerYAnchor).isActive = true
nameLabel.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
nameLabel.heightAnchor.constraint(equalTo: profileImageView.heightAnchor).isActive = true
containerView.centerXAnchor.constraint(equalTo: titleView.centerXAnchor).isActive = true
containerView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true
self.navigationItem.titleView = titleView
//Adding the side menu button
let elipsesImage = UIImage(systemName: "ellipsis")
let menuItem = UIBarButtonItem(image: elipsesImage, style: .plain, target: self, action: #selector(menuTapped))
menuItem.tintColor = UIColor.init(red: 238/255, green: 119/255, blue: 98/255, alpha: 1)
navigationItem.rightBarButtonItem = menuItem
rightBarDropDown.anchorView = menuItem
rightBarDropDown.dataSource = ["Un-Match","Block", "View Profile"]
rightBarDropDown.cellConfiguration = { (index, item) in return "\(item)" }
}