Remove Text from Back Button Keeping the Icon

How to remove all navigationbar back button title

If you want back arrow so following code put into AppDelegate file into didFinishLaunchingWithOptions method.

For Objective-C

 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];

For Swift

let BarButtonItemAppearance = UIBarButtonItem.appearance()
BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)

Another option give below.

In Objective C

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];

In Swift

self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)

UPDATE :

    let BarButtonItemAppearance = UIBarButtonItem.appearance()

let attributes: [NSAttributedStringKey: Any] = [
BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 0.1),
NSAttributedStringKey.foregroundColor: UIColor.clear]

BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)
BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)

UPDATE SWIFT 4.1 :

    let attributes = [NSAttributedStringKey.font:  UIFont(name: "Helvetica-Bold", size: 0.1)!, NSAttributedStringKey.foregroundColor: UIColor.clear]

BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)
BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)

Using Offset

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(-1000, 0), for:UIBarMetrics.default)

Remove text from Back button keeping the icon

I know this already has an answer, but you can also do it in code (in case you're working with nibs)

navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

Add the above in the first view controller.

Note that you need to do this for each view controller that is pushing. So if you have 3 view controllers, and you want to remove the back text from all of them, you'll have to add the line in view controller 1 and 2.

Swift 4 remove text from back button

There are a couple ways to achieve what you want to do.

When you push a view controller (VC2) onto the navigation stack for the first controller (VC1) the back button uses the title of the first screen by default as the back button title when the second screen is presented.

Sample Image

So in VC1 you could set the view controller's title to an empty string before you push onto the stack

override func viewDidLoad() {
super.viewDidLoad()
self.title = ""
}

and this will leave you with a back button that just has the '<' character.

Sample Image

The second method is to set a custom back bar button item in the parent view controller (VC1)

override func viewDidLoad() {
super.viewDidLoad()
navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)
}

Sample Image

which sets the back button title to whatever you set in your UIBarButtonItem title field.

So your implementation might look something like this: (pick one option or the other where commented, if you do both the custom bar button item overrides the title.)

@IBAction func didSelectSegment(_ sender: UISegmentedControl) {

let vcName = vcNames[sender.selectedSegmentIndex]
if vcName == "NQTPastRecordViewController" && loggined() == false {
let alert = alertAskLogin({
let signInVC = getController("EVSignInViewController")

// set title to empty string here
title = ""

// or set a custom back bar button item and set target/action as needed
navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)

navigationController?.pushViewController(signInVC, animated: true)
})
self.present(alert, animated: true, completion: nil)
segmentControll.selectedSegmentIndex = 0
return
}
container.swipeToController(at: sender.selectedSegmentIndex)

}

iOS Swift - How to remove top bar back button's Back text?

This will work for you

self.navigationController?.navigationBar.topItem?.title = " "

Remove back button text from navigationbar in SwiftUI

So I actually ended up with the following solution that actually works. I am overwriting the navigation bar items like so

.navigationBarItems(leading:
Image("backButton")
.foregroundColor(.blue)
.onTapGesture {
self.presentationMode.wrappedValue.dismiss()
}
)

The only issue with this was that the back gesture wasn't working so that was solved by actually extending the UINavigationController

extension UINavigationController: UIGestureRecognizerDelegate {
override open func viewDidLoad() {
super.viewDidLoad()
interactivePopGestureRecognizer?.delegate = self
}

public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return viewControllers.count > 1
}
}

Now it's looking exactly the way I want it, the solution is kinda hacky... but it works for now, hopefully SwiftUI will mature a little bit so this can be done easier.

UINavigationBar Hide back Button Text

In the interface builder, you can select the navigation item of the previous controller and change the Back Button string to what you'd like the back button to appear as. If you want it blank, for example, just put a space.

You can also change it with this line of code:

[self.navigationItem.backBarButtonItem setTitle:@"Title here"];

Or in Swift:

self.navigationItem.backBarButtonItem?.title = ""

Remove the text from back button in SwiftUI

You need to set the title of the view that the back button will pop to:

struct ContentView: View {    
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: DetailView()) {
Text("push view")
}
}.navigationBarTitle("", displayMode: .inline)
}
}
}

struct DetailView: View {
var body: some View {
Text("Detail View")
}
}

Alternatively, to conditionally set or unset the title of the source view, depending on the presentation status you can use the code below.

Beware that the isActive parameter has a bug, but that will most likely be solved soon. Here's a reference to the bug mentioned SwiftUI: NavigationDestinationLink deprecated

struct ContentView: View {
@State private var active: Bool = false

var body: some View {
NavigationView {
VStack {
NavigationLink(destination: DetailView(), isActive: $active) {
Text("push view")
}
}.navigationBarTitle(!active ? "View Title" : "", displayMode: .inline)
}
}
}

struct DetailView: View {
var body: some View {
Text("Detail View")
}
}

How to change back button text in Navigation item to picture in xcode

Add below code in viewDidLoad of viewController where you want to change back button title:

 let backButton = UIBarButtonItem()
backButton.title = ""
self.navigationController?.navigationBar.topItem?.backBarButtonItem = backButton

Sample Image

Remove back button text from inherited navigation bar Swift 3

You better custom back button for this task.

but You also can do it in other ways. Ex: You have ViewController1, and ViewController2 (You push ViewController2 from ViewController1)

ViewController1

public class ViewController1: UIViewController {

override public func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.title = "viewcontroller1 title"
}

}

ViewController2

class ViewController2: UIViewController {

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// get previous view controller and set title to "" or any things You want
if let viewControllers = self.navigationController?.viewControllers {
let previousVC: UIViewController? = viewControllers.count >= 2 ? viewControllers[viewControllers.count - 2] : nil; // get previous view
previousVC?.title = "" // or previousVC?.title = "Back"
}
}

}


Related Topics



Leave a reply



Submit