4

我正在尝试为导航栏创建自定义 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
        }
    }
}
4

3 回答 3

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
    }
}
于 2015-11-23T15:43:45.937 回答
0

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

于 2019-10-30T17:53:07.227 回答
0

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

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藏navigationBar在其中viewWillAppear();当 B 弹出时,A.viewWillAppear() setNavigationBar(hidden: false, animated: true) 2. 用户驱动popViewController通过抬手取消。
然后你会发现,AnavigationBar是空白的。

于 2017-07-15T14:15:55.150 回答