添加子视图后 titleview 消失了吗?

titleview dissapears when a subview is added?

这对我来说是新的,如果我问的问题不对,请原谅我。

我正在学习一个教程,其中我们在导航栏的标题视图中创建了几个子视图,以便显示图片和用户名。当我创建带有红色背景的初始 titleview 时,它按预期显示。但是,当我添加一个容器子视图来放置文本和图像时,红色的标题视图消失了。我完成了教程并且文本显示在正确的位置,但它不允许我添加点击手势,因为 titleview 不再可以点击了?

我将为此功能添加我的代码 - 希望我遗漏了一个愚蠢的错误。

func setupNavBarWithUser(user: User) {

    let titleView = UIView()
    titleView.frame = CGRect(x: 0, y: 0, width: 130, height: 35)
    titleView.backgroundColor = UIColor.red

    let containerView = UIView()
    containerView.translatesAutoresizingMaskIntoConstraints = false
    containerView.backgroundColor = UIColor.blue
    titleView.addSubview(containerView)

    let profileImageView = UIImageView()
    profileImageView.translatesAutoresizingMaskIntoConstraints = false
    profileImageView.contentMode = .scaleAspectFill
    profileImageView.layer.cornerRadius = 20
    profileImageView.clipsToBounds = true

    if let profileImageUrl = user.profileImageUrl {
        profileImageView.loadImageUsingCacheWithUrlString(urlString: profileImageUrl)
    }

    containerView.addSubview(profileImageView)

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

    let nameLabel = UILabel()


    containerView.addSubview(nameLabel)

    nameLabel.text = user.name
    nameLabel.translatesAutoresizingMaskIntoConstraints = false

    nameLabel.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8).isActive = true
    nameLabel.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
    nameLabel.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
    nameLabel.heightAnchor.constraint(equalToConstant: 40).isActive = true

    containerView.centerXAnchor.constraint(equalTo: titleView.centerXAnchor).isActive = true
    containerView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true

    self.navigationItem.titleView = titleView

    titleView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showChatController)))
    titleView.isUserInteractionEnabled = true

}

将 setupNavBarWithUser 方法替换为:

func setupNavBarWithUser(user: User) {

        let titleView = UIView()
        titleView.frame = CGRect(x: 0, y: 0, width: 130, height: 45)
        titleView.backgroundColor = UIColor.red

        let containerView = UIView()
        containerView.translatesAutoresizingMaskIntoConstraints = false
        containerView.backgroundColor = UIColor.blue
        self.navigationItem.titleView = titleView

        titleView.addSubview(containerView)

        containerView.topAnchor.constraint(equalTo: titleView.topAnchor, constant: 0).isActive = true
        containerView.bottomAnchor.constraint(equalTo: titleView.bottomAnchor, constant: 0).isActive = true
        containerView.leadingAnchor.constraint(equalTo: titleView.leadingAnchor, constant: 0).isActive = true
        containerView.trailingAnchor.constraint(equalTo: titleView.trailingAnchor, constant: 0).isActive = true

        let profileImageView = UIImageView()
        profileImageView.translatesAutoresizingMaskIntoConstraints = false
        profileImageView.contentMode = .scaleAspectFill
        profileImageView.layer.cornerRadius = 20
        profileImageView.clipsToBounds = true

        if let profileImageUrl = user.profileImageUrl {
            profileImageView.loadImageUsingCacheWithUrlString(urlString: profileImageUrl)
        }

        containerView.addSubview(profileImageView)

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

        let nameLabel = UILabel()

        containerView.addSubview(nameLabel)

        nameLabel.text = user.name
        nameLabel.translatesAutoresizingMaskIntoConstraints = false

        nameLabel.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8).isActive = true
        nameLabel.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
        nameLabel.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
        nameLabel.heightAnchor.constraint(equalToConstant: 40).isActive = true


        self.navigationItem.titleView = titleView

        titleView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showChatController)))
        titleView.isUserInteractionEnabled = true

    }

您可以对比一下代码。如您所见,您必须以正确的顺序添加子视图以设置约束。