2017-10-21 107 views
0

我有一个UINavigationItem,我将它的titleView设置为一个UIView,它具有嵌入的UILabel和UIImageView。我试图添加一个UITapGestureRecognizer到视图,但它似乎不工作。任何解决方案此外,添加一个gestureRecognizer到整个navigationBar是不是一个选项,因为我有一个rightBarButtonItem,并希望利用后退按钮。Swift UITapGesture在标题视图不工作

这里是我的代码:

func configureTitleView() { 
    guard let profile = profile else { 
    // Pop navController 
    return 
    } 

    let titleView = UIView() 
    titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 40) 

    let containerView = UIView() 
    containerView.translatesAutoresizingMaskIntoConstraints = false 
    titleView.addSubview(containerView) 

    let profileImageView = UIImageView() 
    profileImageView.translatesAutoresizingMaskIntoConstraints = false 
    profileImageView.contentMode = .scaleAspectFill 
    profileImageView.clipsToBounds = true 
    let imageURL = URL(string: profile!.firstProfilePicture!) 
    profileImageView.sd_setImage(with: imageURL) 

    containerView.addSubview(profileImageView) 

    profileImageView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true 
    profileImageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true 
    profileImageView.widthAnchor.constraint(equalToConstant: 36).isActive = true 
    profileImageView.heightAnchor.constraint(equalToConstant: 36).isActive = true 

    profileImageView.layer.cornerRadius = 36/2 

    let nameLabel = UILabel() 

    containerView.addSubview(nameLabel) 
    nameLabel.text = profile!.displayName! 
    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: 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 

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.openProfile)) 
    tapGesture.numberOfTapsRequired = 1 
    titleView.addGestureRecognizer(tapGesture) 
    titleView.isUserInteractionEnabled = true 
} 
+0

你可以分享你的代码如何在导航栏中添加所有这些项目吗?我希望你在背景视图中添加轻击手势,并且图像视图和标签位于视图上方,因此手势没有响应。 – Bharath

+0

一个建议,您可以尝试添加一个清晰颜色的按钮,而不是点击手势,就像添加另外两个按钮并为该按钮添加目标一样。 – Bharath

+0

@Bharath我已经添加了我的代码...有没有一种方法可以在不使用按钮的情况下进行? –

回答

2

与iOS 11开始,视图中使用UIBarButtonItem(customView:)现在使用自动布局奠定了添加到工具栏为UIBarButtonItem。这包括通过UIViewControllernavigationItem.titleView财产添加到UINavigationBar的标题视图。您应该在titleView上添加尺寸限制。例如:

titleView.widthAnchor.constraint(equalToConstant: 100).isActive = true 
titleView.heightAnchor.constraint(equalToConstant: 40.0).isActive = true 

否则,自动布局将要使用的是CGSize.zero你的标题观的本质内容大小。即使该视图的子视图不支持,手势也会被屏蔽到所附视图的边界。因为titleView没有限制的范围是CGRect.zero它永远不会开火。添加约束并按预期工作。

欲了解更多信息,请参阅WWDC 2017会议Updating your app for iOS 11

+0

工作奇迹谢谢! –

相关问题