Full transparency of navigation bar with Swift & font resizing of button in navigation bar

Source: Internet
Author: User

Some of the little questions about navigation bar that have been encountered in the project over the past few days and how to solve them are documented:

1. Fully Transparent navigation bar

Sometimes we want to navigation Viewcontroller top navigation bar to become completely transparent, we first thought is from story board, but tried for half a day, also can not achieve completely transparent effect


Choosing the translucent Navigation bar here can only turn the bar into the kind of glass that can dilute the background color, so how exactly can the entire Navigation bar become transparent?

The answer is implemented by code:

Very simple, in viewdidload or didappear, add the following 3 lines of code:

        Self.navigationcontroller?. Navigationbar.setbackgroundimage (UIImage (), forBarMetrics:UIBarMetrics.Default)        Self.navigationcontroller?. Navigationbar.shadowimage = UIImage ()        Self.navigationcontroller?. Navigationbar.translucent = True

try it yourself.


2. Change the style of Backbutton or Leftbarbutton in bar

If we use the Navigation Viewcontroller series, when we switch to the next page, the bar will automatically appear on the left side of the button, the text of the button is the default text content of the previous page Baritem, these are the default, will automatically help us build.

Sometimes the problem is that if the "a" bar item's text content is very long, from a page push to B page, the text of the return button will be very long, so that the B page of the item content to squeeze to the right, this is very ugly.

There are two ways we can handle this,

1> We can create a new button and give it text, style, etc.

Let Backbutton = Uibarbuttonitem (title: "< Back", Style:UIBarButtonItemStyle.Plain, target:self, Action: "GoBack") NA Vigationitem.leftbarbuttonitem = Backbuttonnavigationitem.leftbarbuttonitem?. Settitletextattributes ([Nsfontattributename:uifont (Name: "Chalkduster", size:20)!], Forstate: Uicontrolstate.normal)

the downside to this is that you have no way to get back from the left-hand side of the screen, and you must manually call navigation's Pop method in GoBack's function to achieve the return effect.

2> or we can change the style of the return button, such as manually adjusting its text font size

        Let Customfont = Uifont (name: "Heiti SC", size:12.0)        uibarbuttonitem.appearance (). Settitletextattributes ([ Nsfontattributename:customfont!], ForState:UIControlState.Normal)
Note that because we are actually changing the Uibarbuttonitem, so this code we want to put in appdelegate to achieve. This way, the font for All button classes in bar is set to size 12th.

Full transparency of navigation bar with Swift & font resizing of button in navigation bar

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.