Setting up the back button in navigation controller
In your case add a custom button on the Navigation.
class YourViewController: UIViewController {
//Navigation Items.
//left bar button item.
private var leftBarButtonItem : UIBarButtonItem!
//left button.
private var navigationLeftButton : UIButton!
//Your other variable/object declaration.
func viewDidLoad() {
super.viewDidLoad()
self.leftBarButtonItem = UIBarButtonItem()
}
func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.setNavigationBackButton()
}
private func setNavigationBackButton() {
if(self.navigationLeftButton == nil) {
self.navigationLeftButton = UIButton(type: UIButtonType.System)
}
//Styling your navigationLeftButton goes here...
self.navigationLeftButton.addTarget(self, action: Selector("backButtonTapped"), forControlEvents: UIControlEvents.TouchUpInside)
self.leftBarButtonItem.customView = self.navigationLeftButton
self.navigationItem.leftBarButtonItem = self.leftBarButtonItem
}
func backButtonTapped(AnyObject:sender) {
// Here add your custom functionalities.
// Note, this will not pop to previous viewcontroller,
}
}
Setting Back Button in Navigation Controller
You can set navigationItem.leftBarButtonItem
to a button of your own design. That overrides backBarButtonItem
, and iOS will not replace your text with "Back". But if you want an arrow you would have to design it yourself as part of your button.
In my code I set the button whenever the title is changed:
override var title:String? {
get {
return super.title
}
set {
super.title = newValue
if (navigationController?.childViewControllers.count > 1) {
let backButton = // ... custom button
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
backButton.addTarget(self, action: "backAction", forControlEvents: .TouchUpInside)
} else {
navigationItem.hidesBackButton = true
}
Execute action when back bar button of UINavigationController is pressed
One option would be implementing your own custom back button. You would need to add the following code to your viewDidLoad method:
- (void) viewDidLoad {
[super viewDidLoad];
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(back:)];
self.navigationItem.leftBarButtonItem = newBackButton;
}
- (void) back:(UIBarButtonItem *)sender {
// Perform your custom actions
// ...
// Go back to the previous ViewController
[self.navigationController popViewControllerAnimated:YES];
}
UPDATE:
Here is the version for Swift:
override func viewDidLoad {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true
let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Bordered, target: self, action: "back:")
self.navigationItem.leftBarButtonItem = newBackButton
}
func back(sender: UIBarButtonItem) {
// Perform your custom actions
// ...
// Go back to the previous ViewController
self.navigationController?.popViewControllerAnimated(true)
}
UPDATE 2:
Here is the version for Swift 3:
override func viewDidLoad {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true
let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(YourViewController.back(sender:)))
self.navigationItem.leftBarButtonItem = newBackButton
}
func back(sender: UIBarButtonItem) {
// Perform your custom actions
// ...
// Go back to the previous ViewController
_ = navigationController?.popViewController(animated: true)
}
Related Topics
Detect If the Device Is Iphone X
How to Create a Multiline Textfield in Swiftui
How to Use Timer (Formerly Nstimer) in Swift
Background Location Services Not Working in iOS 7
How to Use Background Thread in Swift
How to Record Audio on Iphone With Avaudiorecorder
How to Install iOS 6 Sdk on Xcode 5
Save String to the Nsuserdefaults
How to Parse a Json File in Swift
Eliminate Extra Separators Below Uitableview
Move Textfield When Keyboard Appears Swift
How to Perform Unwind Segue Programmatically
How to Group by the Elements of an Array in Swift
Creating a Left-Arrow Button (Like Uinavigationbar'S "Back" Style) on a Uitoolbar
Undefined Symbols For Architecture I386: _Objc_Class_$_Skpsmtpmessage", Referenced From: Error