添加手势识别器 uivew 导航栏 swift 不起作用
Posted
技术标签:
【中文标题】添加手势识别器 uivew 导航栏 swift 不起作用【英文标题】:add gesture recognizer uivew navigation bar swift not working 【发布时间】:2018-06-24 15:17:25 【问题描述】:我有一个带有导航栏的导航控制器,我添加了一个 UIView,其中包含 imageView 的子视图和用于 titleView 的 UILabel。 我需要能够单击该视图以在该视图上使用 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")
希望有人能帮我解决这个问题,非常感谢!!
UPDATE this is where I need to add a gesture recognizer
【问题讨论】:
【参考方案1】:在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)
【讨论】:
感谢您的回答,它有效但现在标签旁边没有imageView,我只有nameLabel,我该如何添加它? 只需在viewwillAppear中添加点击手势就可以了 这很好用,非常感谢,但是现在即使我触摸左侧或右侧的任何图标栏,点击手势也可以工作,因为该手势在所有导航栏上都可用,你知道如何解决它? 我更新了我的代码检查一次,如果你也想要图像操作,然后用 uiimageview 替换为 uibutton 我刚刚尝试了我的框架,如果您想根据需要更改框架以上是关于添加手势识别器 uivew 导航栏 swift 不起作用的主要内容,如果未能解决你的问题,请参考以下文章
添加手势识别器以允许在 Swift 中的 MapView 上水平滑动