how to create back button in navigation bar
As you said in your comment you use a modal controller
Add the following in viewWillappear
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:self action:@selector(Back)];
self.navigationItem.leftBarButtonItem = backButton;
And in
- (IBAction)Back
{
[self dismissViewControllerAnimated:YES completion:nil]; // ios 6
}
Custom back button for NavigationView's navigation bar in SwiftUI
TL;DR
Use this to transition to your view:
NavigationLink(destination: SampleDetails()) {}
Add this to the view itself:
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
Then, in a button action or something, dismiss the view:
presentationMode.wrappedValue.dismiss()
Full code
From a parent, navigate using NavigationLink
NavigationLink(destination: SampleDetails()) {}
In DetailsView hide navigationBarBackButton
and set custom back button to leading navigationBarItem
,
struct SampleDetails: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var btnBack : some View { Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
HStack {
Image("ic_back") // set image here
.aspectRatio(contentMode: .fit)
.foregroundColor(.white)
Text("Go back")
}
}
}
var body: some View {
List {
Text("sample code")
}
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: btnBack)
}
}
How to create Navigation bar with back button on it?
Xcode handles this for you.
Highlight your first controller. Go to the menu bar and click Embed In > Navigation Controller. Now using UIButton
or something, control-click and drag from your first view controller's button to your second view controller and make it a Push segue. The back button won't appear, but if you run it, it will be there.
So you should have 3 views in storyboard. The Navigation Controller, your first view controller, and your second view controller.
Here is one way to do it using code.
Swift custom back button in navigation bar
Try code below :-)
func SetBackBarButtonCustom()
{
//Back buttion
let btnLeftMenu: UIButton = UIButton()
btnLeftMenu.setImage(UIImage(named: "back_arrow"), for: UIControlState())
btnLeftMenu.addTarget(self, action: #selector(UIViewController.onClcikBack), for: UIControlEvents.touchUpInside)
btnLeftMenu.frame = CGRect(x: 0, y: 0, width: 33/2, height: 27/2)
let barButton = UIBarButtonItem(customView: btnLeftMenu)
self.navigationItem.leftBarButtonItem = barButton
}
func onClcikBack()
{
_ = self.navigationController?.popViewController(animated: true)
}
How to properly implement the top back button in navigation drawer?
if (item.getItemId() == android.R.id.home) {
int backStackCount = fragmentManager.getBackStackEntryCount();//check currently how many frags loaded
if (backStackCount > 0) {
fragmentManager.popBackStack(); //go back to previously loaded fragment
}
}
Currently, clicking back only exits the app.
Using popBackStack()
will pop up the backstack making your app exists, but you just need to return to the default fragment.
To fix this, you need to change the behavior of the drawer burger button, so that it can be used sometimes to open the drawer navView layout, and other times to back to the default fragment; the later is when you want to add the top back button instead.
how I can implement the up/top back button for the nav drawer
This requires to access the setToolbarNavigationClickListener
method which enables you add a listener to burger clicks.
In this case you need to return back to the home fragment as you need, in the onCreate()
method add:
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Enable the functionality of opening the side drawer, when the burger icon is clicked
toggle.setDrawerIndicatorEnabled(true); // Show the burger icon & enable the drawer funcionality
navController.navigate(R.id.home); // Back to default fragment (replace home with your default fragment id in the navGraph)
}
});
The remaining part is to show the back button whenever you want to go to certain fragment
And use toggle.setDrawerIndicatorEnabled()
to enable/disable the functionality of opening/closing the drawer when the home/burger icon is clicked
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
// Repeat this condition for all the Fragments that you want to show the back button
if (destination.getId() == R.id.settings_id) { // replace `settings_id` with your fragment id in the navGraph that you want to show the back button
// Disable the functionality of opening the side drawer, when the burger icon is clicked & show the UP button instead
toggle.setDrawerIndicatorEnabled(false);
}
}
});
Swift Custom NavBar Back Button Image and Text
You can do something like that:
let yourBackImage = UIImage(named: "back_button_image")
self.navigationController?.navigationBar.backIndicatorImage = yourBackImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = yourBackImage
self.navigationController?.navigationBar.backItem?.title = "Custom"
Your image will only have one color though
Related Topics
How to Control My Headset for My Music Player
Xcode Doesn't Write Anything to Output Console
Setting Layoutmargins of Uiview Doesn't Work
Authentication with Wkwebview in Swift
Ios/C: Convert "Integer" into Four Character String
Uitableview Pagination - Bottom Refresh to Load New Data in Swift
How to Segue with Data from One Tab to Another Tab Properly
Cannot Invoke Initializer for Type 'Double' with an Argument List of Type '(String)'
Uisearchcontroller Disable Cancel Uibarbuttonitem
Fontawesome Pro and Xamarin.iOS Only One Font Can Be Active
Can the -Objc Flag Be Applied Selectively to Static Libraries
Avaudiosession .Defaulttospeaker Changes Mic Input
How to Get Touchid Information and Compare to a Fingerprint Database
How to Show Different Alerts Based on a Condition After Clicking a Button in Swiftui