Swift's navigation bar title text is too long, causing the return button text to disappear problem solved

Source: Internet
Author: User
Tags uikit

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.