标题视图中的Swift UITapGesture无法正常工作

时间:2017-10-21 20:10:12

标签: ios swift uinavigationcontroller uinavigationitem

我有一个UINavigationItem,我把它的titleView设置为一个嵌入了UILabel和UIImageView的UIView。我正在尝试向视图添加UITapGestureRecognizer,但它似乎不起作用。有解决方案吗此外,将gestureRecognizer添加到整个navigationBar不是一个选项,因为我有一个rightBarButtonItem并且想要使用后退按钮。

这是我的代码:

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
}

2 个答案:

答案 0 :(得分:5)

从iOS 11开始,使用UIBarButtonItem添加到UIBarButtonItem(customView:)工具栏的视图现在使用自动布局进行布局。这包括通过UINavigationBar的{​​{1}}属性添加到navigationItem.titleView的标题视图。您应该在UIViewController上添加大小限制。例如:

titleView

否则,自动布局将使用标题视图的内在内容大小titleView.widthAnchor.constraint(equalToConstant: 100).isActive = true titleView.heightAnchor.constraint(equalToConstant: 40.0).isActive = true 。即使该视图的子视图不是,手势也会被屏蔽到它们所附着的视图的边界。因为没有约束的CGSize.zero的界限是titleView,它永远不会发射。添加约束,它按预期工作。

有关详细信息,请参阅WWDC 2017会话Updating your app for iOS 11

答案 1 :(得分:0)

您无需向自定义视图添加显式的高度和宽度常量约束。

只需将子视图添加到自定义视图,添加宽度和高度锚点即可。

    let customView = UIView()

    let button = UIButton(type: .custom)
    button.translatesAutoresizingMaskIntoConstraints = false
    customView.addSubview(button)

    [
        button.widthAnchor.constraint(equalTo: customView.widthAnchor),
        button.heightAnchor.constraint(equalTo: customView.heightAnchor),
        ].forEach({$0.isActive = true})

    navigationItem.titleView = customView