gpt4 book ai didi

ios - 在 titleView 中查看 Swift UITapGesture 不工作

转载 作者:行者123 更新时间:2023-11-28 09:51:21 25 4
gpt4 key购买 nike

我有一个 UINavigationItem,我将它的 titleView 设置为一个嵌入了 UILabel 和 UIImageView 的 UIView。我正在尝试将 UITapGestureRecognizer 添加到 View 中,但它似乎不起作用。任何解决方案?此外,将 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
}

最佳答案

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

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

否则,自动布局将使用标题 View 的固有内容大小,即 CGSize.zero。手势被屏蔽到它们所附加的 View 的边界,即使该 View 的 subview 不是。因为没有约束的 titleView 的边界是 CGRect.zero 它永远不会触发。添加约束并按预期工作。

有关更多信息,请参阅 WWDC 2017 session Updating your app for iOS 11 .

关于ios - 在 titleView 中查看 Swift UITapGesture 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46867592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com