【问题标题】:导航控制器 navigationItems 没有按我预期的那样显示
【发布时间】:2022-01-23 06:27:36
【问题描述】:

我关注 youtube 上的视频。当我尝试设置导航控制器项目时遇到了问题。

    func setupNavigationBarItems()  {
        let twitterImage = UIImageView(image: UIImage(named: "twitter_icon"))
        twitterImage.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
        twitterImage.contentMode = .scaleAspectFit
        
        navigationController?.navigationItem.titleView = twitterImage
        
        let profileButton = UIButton(type: .system)
        profileButton.setImage(UIImage(named: "profile_image"), for: .normal)
        profileButton.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
        navigationController?.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: profileButton)
    }

Result

这就是结果。什么都没有显示

编辑—— 我做了同样的改变

  navigationController?.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: profileButton)

--

        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: profileButton)

它只显示 profileButton 和按钮延伸到边缘

new result

【问题讨论】:

    标签: ios swift uinavigationcontroller uikit uinavigationbar


    【解决方案1】:

    点击调试视图

    将帮助您调试视图发生的情况 (先构建项目,你会看到这个按钮)

    【讨论】:

    • 这可能有很多原因,例如您的 navigationController 为 nil 或关于 navBar 项目约束
    • 我照你说的做了,那里没有风景。但是我删除了navigationController?。现在在我的代码中,它只显示 profileButton 和按钮延伸到边缘
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多