【问题标题】:UINavigationItem TitleView disappearsUINavigationItem TitleView 消失
【发布时间】:2016-02-25 00:32:43
【问题描述】:

我正在尝试为导航栏创建自定义 titleView。我可以在嵌入导航控制器的根视图控制器中设置 titleView。

当我将第二个视图控制器推入堆栈并尝试为此视图控制器设置 titleView 时,它不起作用。 titleView 快速出现和消失。当我回到前一个视图控制器时,这个 titleView 现在也会快速出现和消失。

有谁知道为什么会发生这种情况或如何正确设置titleView而不闪烁和消失?

class FirstViewController: UIViewController {

    var titleView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        addTitleView()
    }

    func addTitleView() {
        titleView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 44))

        let companyLabel = UILabel(frame: CGRect(x: 0, y: 3, width: 150, height: 11))
        companyLabel.text = "CPS Dashboard"
        companyLabel.textColor = UIColor.grayColor()
        companyLabel.textAlignment = .Center
        companyLabel.font = UIFont.systemFontOfSize(9)
        titleView.addSubview(companyLabel)

        let titleLabel = UILabel(frame: CGRect(x: 0, y: 16, width: 150, height: 18))
        titleLabel.text = "Dashboard"
        titleLabel.textColor = UIColor.blackColor()
        titleLabel.textAlignment = .Center
        titleLabel.font = UIFont.systemFontOfSize(15)
        titleView.addSubview(titleLabel)

        navigationItem.titleView = titleView
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "Show" {
            let controller = segue.destinationViewController as! SecondViewController
            controller.titleView = titleView
        }
    }
}

第二个视图控制器:

class SecondViewController: UIViewController {

    var titleView: UIView?

    override func viewDidLoad() {
        super.viewDidLoad()

        if let titleView = titleView {
            navigationItem.titleView = titleView
        }
    }
}

【问题讨论】:

  • 你能简单地显示你的代码吗? ?
  • 是的,我更新了问题以包含代码。
  • @msjtngus:请您再花点时间写出正确的英语。谢谢!
  • 尝试在viewWillAppear(_:)中调用addTitleView()
  • 在应用加载时在 viewWillAppear() 中调用 addTitleView() 对 FirstViewController 工作正常,然后推送 SecondViewController 会发生相同的出现/消失行为。当我弹回 FirstViewController 时,titleView 出现在导航栏的左侧并滑动到中心。

标签: ios swift uinavigationcontroller uinavigationbar titleview


【解决方案1】:

我遇到了同样的问题,但上述解决方案都没有为我解决这个问题。我的问题是我将translatesAutoresizingMaskIntoConstraints 设置为false。我想这会导致出现/消失,因为它需要设置为true,以便将视图内部限制在导航栏上。

【讨论】:

    【解决方案2】:

    我找到了解决方案。我将 addTitleView() 方法从 FirstViewController 复制到 SecondViewController,并在 viewDidLoad() 中调用它们。这完全符合我的要求。由于某种原因,将 titleView 作为属性向前传递并将其分配给 navigationItem.titleView 是行不通的。

    class FirstViewController: UIViewController {
    
        var titleView: UIView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            addTitleView()
        }
    
        func addTitleView() {
            titleView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 44))
    
            let companyLabel = UILabel(frame: CGRect(x: 0, y: 3, width: 150, height: 11))
            companyLabel.text = "CPS Dashboard"
            companyLabel.textColor = UIColor.grayColor()
            companyLabel.textAlignment = .Center
            companyLabel.font = UIFont.systemFontOfSize(9)
            titleView.addSubview(companyLabel)
    
            let titleLabel = UILabel(frame: CGRect(x: 0, y: 16, width: 150, height: 18))
            titleLabel.text = "Dashboard"
            titleLabel.textColor = UIColor.blackColor()
            titleLabel.textAlignment = .Center
            titleLabel.font = UIFont.systemFontOfSize(15)
            titleView.addSubview(titleLabel)
    
            navigationItem.titleView = titleView
        }
    }
    

    第二个视图控制器:

    class SecondViewController: UIViewController {
    
        var titleView: UIView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            addTitleView()
        }
    
        func addTitleView() {
            titleView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 44))
    
            let companyLabel = UILabel(frame: CGRect(x: 0, y: 3, width: 150, height: 11))
            companyLabel.text = "CPS Dashboard"
            companyLabel.textColor = UIColor.grayColor()
            companyLabel.textAlignment = .Center
            companyLabel.font = UIFont.systemFontOfSize(9)
            titleView.addSubview(companyLabel)
    
            let titleLabel = UILabel(frame: CGRect(x: 0, y: 16, width: 150, height: 18))
            titleLabel.text = "Dashboard"
            titleLabel.textColor = UIColor.blackColor()
            titleLabel.textAlignment = .Center
            titleLabel.font = UIFont.systemFontOfSize(15)
            titleView.addSubview(titleLabel)
    
            navigationItem.titleView = titleView
        }
    }
    

    【讨论】:

    • 一个视图只能添加到一个超级视图。当您将视图添加为不同父视图的子视图时,它将从旧视图中消失。因此,您的 2 个视图控制器的代码很可能在谁应该负责显示标题视图方面发生了争执。
    • 我明白了,谢谢你的解释,这可能就是发生了什么
    【解决方案3】:

    我的解决方案很简单,而且很有效:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        if let tv = navigationItem.titleView {
            print("transform", tv.transform)) // is always identity
            let bounds = tv.bounds
            print("bounds", bounds) // its origin may not be zero.
            tv.bounds = CGRect(origin: .zero, size: bounds.size)
            print("new bounds", tv.bounds)
        }
    }
    

    使用Xcode的视图调试器,你会发现titleView.bounds.origin不为零。
    如何让它再次发生,分两步: 1. UIViewController A和B; A有自定义navigationItem.titleView,B在其viewWillAppear()中隐藏navigationBar;当 B 弹出时,A.viewWillAppear() setNavigationBar(hidden: false, animated: true) 2.用户驱动的popViewController举手取消。
    然后你会发现,A的navigationBar是空白的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      相关资源
      最近更新 更多