简体   繁体   English

出现视图时,iOS导航栏标题的动态设置会在文本中产生省略号

[英]iOS Navigation Bar Title set dinamically is making a ellipsis in text when view appears

I was trying to set the back button title in a navigation bar, like this 我试图在导航栏中设置后退按钮标题,就像这样

override func viewWillAppear(animated: Bool) {
  super.viewWillAppear(animated)
  self.title = self.backUpTitle
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    self.title = "Back"
}

Where self.backUpTitle has the original title for the current ViewController. 其中self.backUpTitle具有当前ViewController的原始标题。

It works very well, but I'm a having a quick effect each time I click "Back": the title of the navigation bar appears with the first three letters followed by ellipsis (Eg: "Title" would show as "Tit..."), and after the view fully appears, it shows the entire title without any problem. 效果很好,但是每次单击“上一步”时我都会很快见效:导航栏的标题出现,前三个字母后接省略号(例如:“标题”将显示为“山雀”。 ”),然后在完全显示该视图后,它会显示整个标题,没有任何问题。

这是出现视图时发生的情况

这是视图已经出现时的样子

The thing is... it does not happen in a normal case, so I guess it has to do with my solution about setting Back Button Title. 问题是...在正常情况下不会发生,因此我想这与我设置后退按钮标题的解决方案有关。

The question is: is there a way to avoid this effect? 问题是: 有没有办法避免这种影响? Am I calling self.title in a wrong function? 我在错误的函数中调用self.title吗?

I'm using Xcode 8 and iOS 10.0 我正在使用Xcode 8和iOS 10.0

I've tried running your code on my own machine and I'm not showing the same problem; 我尝试在自己的计算机上运行您的代码,但没有显示相同的问题; I'm thinking you might be using custom views for the title of the navigation bar and your self.backUpTitle is inside a custom view that causes the ellipsis. 我认为您可能正在使用自定义视图作为导航栏的标题,而您的self.backUpTitle位于导致省略号的自定义视图内部。

Some suggestions: 一些建议:

  1. If you just want to show “Create User” that way without the ellipsis, you might want to remove all custom views for your navigation bar and just set the ViewController title like what you are doing in your code. 如果只想以这种方式显示“创建用户”而不用省略号,则可能要删除导航栏的所有自定义视图,并像在代码中一样设置ViewController标题。

  2. Using “self.title” will change the title of your ViewController, make sure your ViewController is embedded to a UIViewController. 使用“ self.title”将更改您的ViewController的标题,请确保将ViewController嵌入到UIViewController中。 However, if you created your navigation bar, setting the title should be: 但是,如果您创建了导航栏,则将标题设置为:

    navigationBar.topItem.title = “Create User”

  3. Just to reiterate, this is what my code looks like (which looks like yours) under a ViewController that is embedded in a UINavigationController: 重申一下,这是我的代码在UINavigationController中嵌入的ViewController下的样子(看起来像您的样子):

     var backUpTitle: String! override func viewDidLoad() { backUpTitle = "Create User" } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.title = self.backUpTitle } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) self.title = "Back" } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS:如何将导航栏标题文本从粗体设置为细体 - iOS: How to set the navigation bar title text from bold to thin 单击iOS中的选项卡项时,无法在导航栏中设置标题 - Can not set title in Navigation bar when click on tabbar item in iOS 当“prefersLargeTitles”设置为true时,更改导航栏标题的文本颜色 - Changing the text color of a navigation bar title when “prefersLargeTitles” is set to true iOS导航栏标题查看按钮 - iOS Navigation Bar Title View Buttons 导航栏和表格视图之间的黑条出现在iOS 6上 - Black bar between navigation bar and table view appears on iOS 6 自定义标题视图作为iOS 11新导航栏中的大标题 - Custom title view as large title in iOS 11 new navigation bar iOS:表格视图的标题栏被导航栏意外覆盖 - iOS: table view's title bar covered by navigation bar unexpectedly 如何在导航栏标题文本上设置搜索栏的文本? - How to set Search Bar's text on Navigation Bar Title Text? 带有长文本的标题视图会推动Swift 4 iOS 11导航栏后退按钮 - Swift 4 iOS 11 navigation bar back button is pushed by a title view with long text 使用新iOS7 SDK在视图上方显示带有提示的导航栏 - Navigation bar with prompt appears over the view with new iOS7 SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM