gpt4 book ai didi

ios - UINavigationItem TitleView 消失

转载 作者:可可西里 更新时间:2023-11-01 01:37:52 27 4
gpt4 key购买 nike

我正在尝试为导航栏创建自定义 titleView。我能够在嵌入导航 Controller 的 Root View Controller 中设置 titleView。

当我将第二个 View Controller 插入堆栈并尝试为此 View Controller 设置 titleView 时,它不起作用。 titleView 快速出现和消失。当我回到上一个 View Controller 时,这个 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
}
}
}

第二个 View Controller :

class SecondViewController: UIViewController {

var titleView: UIView?

override func viewDidLoad() {
super.viewDidLoad()

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

最佳答案

我找到了一个解决方案。我将 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
}
}

第二个 View Controller :

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
}
}

关于ios - UINavigationItem TitleView 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33861952/

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