iOS Development swift modifies navigation bar "back" button text, icon

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.

But if the title of the superior page is very long, the return button Word will be very ugly:

Returning text when the text is extremely long becomes "back":


One, to modify the "Back button" text, like the next two ways:

1, set in the parent page
Set Navigationitem.backbarbuttonitem as a custom Uibarbuttonitem
In this way, all of the child interfaces return to the text we define, while the text remains in front of the return arrow.



Let item = Uibarbuttonitem (title: "Back", Style:. Plain, Target:self, Action:nil)
Self.navigationItem.backBarButtonItem = Item;

Or you can simply set the text to an empty string so that there is only one arrow.


Let item = Uibarbuttonitem (title: "", Style:. Plain, Target:self, Action:nil)
Self.navigationItem.backBarButtonItem = Item;

2, set in sub page
Navigationitem.leftbarbuttonitem to Custom Uibarbuttonitem
This way you can give each child page return button to set different text, but there is no small arrow in front of the text.


Import Uikit

Class Detailviewcontroller:uiviewcontroller {

Override Func Viewdidload () {
Super.viewdidload ()

Let leftbarbtn = Uibarbuttonitem (title: "Back", Style:. Plain, Target:self,
Action: "Backtoprevious")
Self.navigationItem.leftBarButtonItem = leftbarbtn
}

Return button click Response
Func backtoprevious () {
Self.navigationcontroller? Popviewcontrolleranimated (True)
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

Second, modify the "Back button" icon

As you can see from the last example above, only text does not have a picture when you modify the return button on a subpage. If you want to use a custom picture, or if the picture text needs to do the following:

1, if only need picture, do not need text

For example, we want to use the picture on the left (Back@2x.png) as the return icon

Import Uikit

Class Detailviewcontroller:uiviewcontroller {

Override Func Viewdidload () {
Super.viewdidload ()

Let leftbarbtn = Uibarbuttonitem (title: "", Style:. Plain, Target:self,
Action: "Backtoprevious")
Leftbarbtn.image = UIImage (named: "Back")

To remove the left space, or the button is not on top of the front
Let spacer = Uibarbuttonitem (barbuttonsystemitem:. Fixedspace, Target:nil, Action:nil)
Spacer.width =-10;

Self.navigationItem.leftBarButtonItems = [Spacer, LEFTBARBTN]
}

Return button click Response
Func backtoprevious () {
Self.navigationcontroller? Popviewcontrolleranimated (True)
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

2, you need both pictures and text


This is the time to create a uibutton to achieve

Import Uikit

Class Detailviewcontroller:uiviewcontroller {

Override Func Viewdidload () {

Let button = UIButton (type:. System)
Button.frame = CGRectMake (0, 0, 65, 30)
Button.setimage (UIImage (named: "Back"), Forstate:. Normal)
Button.settitle ("Back", Forstate:.) Normal)
Button.addtarget (Self, Action: "Backtoprevious", forControlEvents:. Touchupinside)

Let leftbarbtn = Uibarbuttonitem (Customview:button)

To remove the left space, or the button is not on top of the front
Let spacer = Uibarbuttonitem (barbuttonsystemitem:. Fixedspace, Target:nil, Action:nil)
Spacer.width =-10;

Self.navigationItem.leftBarButtonItems = [SPACER,LEFTBARBTN]
}

Return button click Response
Func backtoprevious () {
Self.navigationcontroller? Popviewcontrolleranimated (True)
}

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.