Items often use Uinavigationcontroller to navigate the individual pages, the return button on the left-hand side of the navigation bar, the default title text is the parent page title.
However, if the title (title) text of the subpage is too long, the text of the return button disappears:
Similarly, modify the text of the return button even if you set Navigationitem.backbarbuttonitem as a custom Uibarbuttonitem in the parent page:
If the title text of the subpage is too long, the text of the return button disappears:
Workaround: Replace the navigation bar title view with a custom label and limit the length size
The Navigationitem.titleview property makes it easy to replace it with a custom UIView view, where we use a fixed-size uilabel, and the effect is as follows:
Import Uikit
Class Detailviewcontroller:uiviewcontroller {
Override Func Viewdidload () {
Let Titleview = UIView (frame:cgrectmake (0, 0, 200, 40))
Let Labelfortitle = Uilabel (frame:cgrectmake (0, 0, 200, 30))
Labelfortitle.font = Uifont.systemfontofsize (17.0, Weight:uifontweightmedium)
Labelfortitle.center = Titleview.center
Labelfortitle.text = "Article List (Welcome to hangge.com)"
Titleview.addsubview (Labelfortitle)
Self.navigationItem.titleView = Titleview
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}
Feature improvements: If the text is too long, label automatically reduces the text size
The above method truncates the extra text and adds it later .... If you want the title text to be fully displayed, you can use the Adjustsfontsizetofitwidth property to have the text label automatically scale the text to fit the label size. The effect chart is as follows:
Import Uikit
Class Detailviewcontroller:uiviewcontroller {
Override Func Viewdidload () {
Let Titleview = UIView (frame:cgrectmake (0, 0, 200, 40))
Let Labelfortitle = Uilabel (frame:cgrectmake (0, 0, 200, 30))
Labelfortitle.font = Uifont.systemfontofsize (17.0, Weight:uifontweightmedium)
Labelfortitle.center = Titleview.center
Labelfortitle.adjustsfontsizetofitwidth = True
Labelfortitle.text = "Article List (Welcome to hangge.com)"
Titleview.addsubview (Labelfortitle)
Self.navigationItem.titleView = Titleview
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}