如何在swift3中以编程方式显示导航栏中的tittle(How to show the tittle in navigation bar programmatically in swift3)

编程入门 行业动态 更新时间:2024-10-24 18:20:34
如何在swift3中以编程方式显示导航栏中的tittle(How to show the tittle in navigation bar programmatically in swift3)

我只是尝试扩展导航栏的高度...我尝试下面的代码,它的工作正常....但它无法显示标题? 我试过self.title =“”和self.navigationitem.title =“”也......

self.navBar.tintColor = UIColor.blue // self.navigationItem.title = "Instrumental" self.title = "Instrumental" navBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white] let backButton = UIButton(type: .custom) backButton.frame = CGRect(x: 380, y: 15, width: 30, height: 30) backButton.setImage(UIImage(named:"vertical-dots (1)"), for: .normal) // backButton.addTarget(self, action: #selector(ViewController.backButton(_:)), for: .TouchUpInside) navBar.addSubview(backButton) func setNavBarToTheView() { self.navBar.frame = CGRect(x:0, y:0, width:420, height:60) // Here you can set you Width and Height for your navBar self.navBar.backgroundColor = (UIColor.blue) self.view.addSubview(navBar) }

I just try to extend my height of navigation bar...I tried that code below, Its working fine....but it can't able to show the title? I tried self.title ="" and self.navigationitem.title ="" also......

self.navBar.tintColor = UIColor.blue // self.navigationItem.title = "Instrumental" self.title = "Instrumental" navBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white] let backButton = UIButton(type: .custom) backButton.frame = CGRect(x: 380, y: 15, width: 30, height: 30) backButton.setImage(UIImage(named:"vertical-dots (1)"), for: .normal) // backButton.addTarget(self, action: #selector(ViewController.backButton(_:)), for: .TouchUpInside) navBar.addSubview(backButton) func setNavBarToTheView() { self.navBar.frame = CGRect(x:0, y:0, width:420, height:60) // Here you can set you Width and Height for your navBar self.navBar.backgroundColor = (UIColor.blue) self.view.addSubview(navBar) }

最满意答案

您似乎手动将UINavBar添加到视图中。 当您执行此操作时,将视图控制器放入导航控制器时会丢失所有自动功能。

要设置导航栏的标题,您还需要创建UINavigationItem并将其设置为导航栏的items属性。

如果将视图控制器放在导航控制器中,事情会变得非常简单。 然后,您将获得所有默认行为,包括标准导航栏,而无需执行添加自己的所有工作。

It appears that you are manually adding a UINavBar to the view. When you do that you lose all of the automatic features when you put your view controller into a navigation controller.

To set the title of your nav bar you need to also create a UINavigationItem and set it to the nav bar's items property.

Things really would be a lot simpler if you put your view controller in a navigation controller. Then you get all of the default behavior including a standard nav bar without the need to do all the work of adding your own.

更多推荐

本文发布于:2023-08-06 21:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454960.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中以   栏中   方式   如何在   bar

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!