How to hide 'Back' button on navigation bar on iPhone? - ios

I added a navigation control to switch between views in my app. But some of the views shouldn't have 'Back' (the previous title) button. Any ideas about how to hide the back button?

Objective-C:
self.navigationItem.hidesBackButton = YES;
Swift:
navigationItem.hidesBackButton = true

The best way is to combine these, so it will hide the back button even if you set it up manually :
self.navigationItem.leftBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;

hide back button with bellow code...
[self.navigationItem setHidesBackButton:YES animated:YES];
or
[self.navigationItem setHidesBackButton:YES];
Also if you have custom UINavigationBar then try bellow code
self.navigationItem.leftBarButtonItem = nil;

In Swift:
Add this to the controller
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.setHidesBackButton(true, animated: false)
}

Use the code:
self.navigationItem.backBarButtonItem=nil;

In the function viewDidLoad of the UIViewController use the code:
self.navigationItem.hidesBackButton = YES;

Don't forget that you need to call it on the object that has the nav controller. For instance, if you have nav controller pushing on a tab bar controller with a RootViewController, calling self.navigationItem.hidesBackButton = YES on the RootViewController will do nothing. You would actually have to call self.tabBarController.navigationItem.hidesBackButton = YES

Add this code in your view controller
UIView *myView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 300, 30)];
UIBarButtonItem *btnL = [[UIBarButtonItem alloc]initWithCustomView:myView];
self.navigationItem.leftBarButtonItem = btnL;

Don't forget that we have the slide to back gesture now. You probably want to remove this as well. Don't forget to enable it back again if necessary.
if ([self.navigationItem respondsToSelector:#selector(hidesBackButton)]) {
self.navigationItem.hidesBackButton = YES;
}
if ([self.navigationController respondsToSelector:#selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

For me none of the above seemed to work, It had no visual effect. I am using storyboards with a view that is "embedded" in a navigation controller.
I then at code level add my menuItems and for some reason the "backButton" is visible when visually debugging the view hierarchy, and my menuItem Icon is displayed beneath the invisible "back button".
I tried the settings, as suggested at the various hook methods and that had no effect. Then I tried a more brutal approach and iterate over the subview which also had no effect.
I inspected my icon sizes and appeared to be ok.
After referring to he apple Human Interface Guideline I confirmed my Icons are correct. (1 pixel smaller in my case 24px 48px 72px).
The strangest part then is the actual fix...
When adding the BarButton Item give it a title with at least one character, In my case a space character.
Hopes this helps someone.
//left menu - the title must have a space
UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithTitle:#" " <--THE FIX
style:UIBarButtonItemStylePlain
target:self
action:#selector(showMenu)];
leftButtonItem.image = [UIImage imageNamed:#"ic_menu"];
[self.navigationItem setLeftBarButtonItem:leftButtonItem];

It wasn't working for me in all cases when I set
self.navigationItem.hidesBackButton = YES;
in viewWillAppear or ViewDidLoad, but worked perfectly when I set it in init of the viewController.

try this one -
self.navigationController?.navigationItem.hidesBackButton = true

In c# or Xamarin.ios,
this.NavigationItem.HidesBackButton = true;

navigationItem.leftBarButtonItem = nil
navigationItem.hidesBackButton = true
if you use this code block inside didLoad or loadView worked but not worked perfectly.İf you look carefully you can see back button is hiding when your view load.Look's weird.
What is the perfect solution?
Add BarButtonItem component from componentView (Command + Shift + L) to your target viewControllers navigation bar.
Select BarButtonItem set Title = " " from right panel

self.navigationItem.setHidesBackButton(true, animated: true)

Related

how to add a UISearchBar in the middle of a NavigationBar [duplicate]

How can I show a UISearchBar in the NavigationBar?
I can't figure out how to do this.
Your help is very much appreciated.
To put searchBar into the center of navigationBar:
self.navigationItem.titleView = self.searchBarTop;
To put searchBar to the left/right side of navigationBar:
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;
As of iOS 7, the UISearchDisplayController supports this by default. Set the UISearchDisplayController's displaysSearchBarInNavigationBar = YES to get this working easily.
Per the documentation:
Starting in iOS 7.0, you can use a search display controller with a navigation bar (an instance of the UINavigationBar class) by configuring the search display controller’s displaysSearchBarInNavigationBar and navigationItem properties.
As one commenter noted, using searchDisplayController.displaysSearchBarInNavigationBar = true ends up hiding any existing left/right bar button items.
I've found two different ways of adding a searchBar to a navigationBar using iOS7's new property on searchDisplayController.
1) Nib Based Approach
If you're using a .xib, you can set a User Defined Runtime Attribute for this value and for whatever reason, the leftBarButtonItem stays in tact. I have not tested it with a rightBarButtonItem.
2) Code (Timing Matters)
If you want to implement in code, timing seems to matter. It seems that you must add the searchBar to the navigationBar first, then set your barButtonItem.
- (void)viewDidLoad
{
...
self.searchDisplayController.displaysSearchBarInNavigationBar = true;
self.navigationItem.leftBarButtonItem = [UIBarButtonItem new];
...
}
Check out Apple's UICatalog sample code. It shows how to use the new UISearchController in three different ways: modally, in nav bar, and below the navigation bar.
Objective C code snippet for UISearchBar in NavigationBar
- (void)viewDidLoad {
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
if (#available(iOS 11.0, *)) {
self.navigationItem.searchController = searchController;
} else {
self.navigationItem.titleView = searchController.searchBar;
}
}
Read more here

how to disable a navigation bar button item in iOS

I have created a navigation controller. In the second view (which is pushed), I have some webservice call and placing a overlay view and setting
self.view.userInteractionEnabled = NO ;
Once web service call is complete, then I am reverting to
self.view.userInteractionEnabled = YES ;
When I do this, every other buttons except the buttons on the navigation bar are disabled. How to disable those two navigation bar button items ? (a button similar to back button, which pops to first view controller and another button which gives info about help).
I have tried using self.navigationItem.backBarButtonItem.enabled = NO. But still I am able to tap on the button and can navigate to first screen. How can I disable these two buttons ?
Try this
Recommended
self.navigationItem.leftBarButtonItem.enabled = NO;
self.navigationItem.rightBarButtonItem.enabled = NO;
Or Simply Disable by
on Edge case
self.view.window.userInteractionEnabled = NO;
Update:
Recently Apple doesn't allow the back button to enable / disable. Instead of that we can hide it.
self.navigationItem.hidesBackButton = YES;
You can do the following if you are running on Swift
self.navigationItem.rightBarButtonItem?.enabled = true
This snippet will disable the button.
Just disable your UINavigationController view and navigation bar interaction:
self.navigationController.navigationBar.userInteractionEnabled = NO;
self.navigationController.view.userInteractionEnabled = NO;
And enable it when you need it back:
self.navigationController.navigationBar.userInteractionEnabled = YES;
self.navigationController.view.userInteractionEnabled = YES;
Latest Swift: To hide the back button, you MUST use:
self.navigationItem.setHidesBackButton(true, animated: false)
Note: This can trigger a bug in the navigation bar that can cause an artifact to appear in place of a hidden back button when transitioning to a view that doesn't have a back button (or has a leftButton in its place). The artifact that appears is either ellipses "..." or the title of the previous viewController on the stack. I believe this bug to be related to the bug documented in apple's own sample code project "CustomizingUINavigationBar", CustomBackButtonViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;
}
This code should work on Swift 4.2
self.navigationItem.rightBarButtonItem?.isEnabled = false
The above code will disable the button. To enable it switch the boolean to true
Updated for Swift 3:
If you want to disable a navigation bar button item OR you want to disable hole UINavigationBar i.e. all item present on navigation bar, use below lines of code;
// if you want disable
self.navigationController?.navigationBar.isUserInteractionEnabled = false
// if you want enable again
self.navigationController?.navigationBar.isUserInteractionEnabled = true
Enjoy...!
For version iOS 10.3, swift 3:
self.navigationItem.rightBarButtonItem?.isEnabled = false.
Try this code:
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
This will stop user to interaction with app and after service call, write this code again:
UIApplication.sharedApplication().endIgnoringInteractionEvents()
Sure this will help.
The simplest way to truly disable a UIBarButtonItem would be as followed:
barButtonVar.isEnabled = false
I solved this by just adding a property to my viewcontroller:
#property (nonatomic, strong) IBOutlet UIBarButtonItem * RightButton;
I then connected it to the button on the storyboard.
You can then at will set its properties like:
self.RightButton.enabled=true;
One line solution
self.navigationItem.leftBarButtonItem.enabled = NO;
self.navigationItem.rightBarButtonItem.enabled = NO;
Swift 5 & iOS 13 :
To remove all left buttons or just a specified one just remove from leftBarButtonItems Array.
self.navigationItem.leftBarButtonItems = []
Navigation bar button items must be toggled by referring to them via the navigationItem property.
For example:
func setupNav() {
let saveButton = UIBarButtonItem.init(barButtonSystemItem: .save, target: self, action: #selector(onSavePressed))
navigationItem.rightBarButtonItem = saveButton
saveButton.isEnabled = false
}
func validateSave() {
saveButton.isEnabled = isConditionMet // WON'T work
navigationItem.rightBarButtonItem.isEnabled = isConditionMet // WORKS!
}
Swift 5
self.navigationItem.rightBarButtonItem?.isEnabled = true;
self.navigationItem.rightBarButtonItem?.isEnabled = false;
var menuBtn = new UIButton(UIButtonType.Custom);
menuBtn.Frame = new CGRect(x: 0.0, y: 0.0, width: 20, height: 20);
menuBtn.SetImage(new UIImage("filter"), UIControlState.Normal);
menuBtn.Alpha = 0.05f; //to set the Alpha
menuBtn.Enabled = false;
tested on Mvvmcross Xamarin.iOS only
Swift 5
It's working for Navigation controller
//Disable
self.navigationController?.navigationBar.isUserInteractionEnabled = false
//Enable
self.navigationController?.navigationBar.isUserInteractionEnabled = true

Is it possible to show a real Popover instead of default slide-out menu when using a UISplitViewController?

I am currently working with a UISplitViewController and instead of having this default slide-out-menu, i want a real UIPopover that appears if i click the UIBarButtonItem. What do i have to do and is there an easy way of configuring this ?
You first need to overwrite the left bar button item, with the button that can be used to display the popover.
Use the following -
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithTitle:#"Popover" style:UIBarButtonItemStylePlain target:self action:#selector(presentPopover:)];
self.navigationItem.hidesBackButton = NO;
self.navigationItem.leftBarButtonItem = nil;
Now you can use the target added, and then perform the associated function, as per your choice.
-(IBAction)presentPopover:(id)sender
{
// Perform your operations
}
Then just use a popover view controller instead with the same content you would have used in the splitview pane that slides out.

UINavigationBar with a 'Back' button. Create with XIB

My question is next:
In interface builder i create UINavigationBar and I want to create 'Back' button item, but I dont see any button.
I use this code:
UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init];
myBarButtonItem.title = #"Back";
mynavBar.backItem.backBarButtonItem = myBarButtonItem;
mynavBar - this is my IBOutlet.
Thanks for help!
You can use a navigation bar as a standalone control or in conjunction with a navigation controller. When you use a navigation bar as a standalone control you use a navigation item (an instance of the UINavigationItem class) to specify what buttons or custom views you want displayed.
So in your case you would use something like this:
UIBarButtonItem *myBarButtonItem = [[[UIBarButtonItem alloc] init] autorelease];
myBarButtonItem.title = #"Back";
UINavigationItem *right = [[[UINavigationItem alloc] initWithTitle:#"Hello!"] autorelease];
right.leftBarButtonItem = myBarButtonItem;
[mynavBar pushNavigationItem:right animated:YES];
You may want to look into using UINavigationViewController though.
If you want a custom button on the left, use mynavBar.leftBarButtonItem instead of backItem.
The backItem will only be visible, after you presented another viewcontroller via pushViewController:. (If you didn't set you own backbutton, the default backButton with the title of the previous viewController will be created automatically.)
//edit: perhaps you look for that:
Draw custom Back button on iPhone Navigation Bar
I think this is the actual way apple want this to be implemented.
Put UINavigationBar
Set outlet to the UINavigationItem
This is the catch
Override navigationItem property to return the UINavigationItem you created.
That's it.
-(UINavigationItem *) navigationItem
{
return self.navigationItem1;
}
If your navigationItem is still in the UINavigationBar, I think you will need to have a strong outlet to the UINavigation Bar too. Please correct me if I am wrong here.

UISearchBar in navigationbar

How can I show a UISearchBar in the NavigationBar?
I can't figure out how to do this.
Your help is very much appreciated.
To put searchBar into the center of navigationBar:
self.navigationItem.titleView = self.searchBarTop;
To put searchBar to the left/right side of navigationBar:
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;
As of iOS 7, the UISearchDisplayController supports this by default. Set the UISearchDisplayController's displaysSearchBarInNavigationBar = YES to get this working easily.
Per the documentation:
Starting in iOS 7.0, you can use a search display controller with a navigation bar (an instance of the UINavigationBar class) by configuring the search display controller’s displaysSearchBarInNavigationBar and navigationItem properties.
As one commenter noted, using searchDisplayController.displaysSearchBarInNavigationBar = true ends up hiding any existing left/right bar button items.
I've found two different ways of adding a searchBar to a navigationBar using iOS7's new property on searchDisplayController.
1) Nib Based Approach
If you're using a .xib, you can set a User Defined Runtime Attribute for this value and for whatever reason, the leftBarButtonItem stays in tact. I have not tested it with a rightBarButtonItem.
2) Code (Timing Matters)
If you want to implement in code, timing seems to matter. It seems that you must add the searchBar to the navigationBar first, then set your barButtonItem.
- (void)viewDidLoad
{
...
self.searchDisplayController.displaysSearchBarInNavigationBar = true;
self.navigationItem.leftBarButtonItem = [UIBarButtonItem new];
...
}
Check out Apple's UICatalog sample code. It shows how to use the new UISearchController in three different ways: modally, in nav bar, and below the navigation bar.
Objective C code snippet for UISearchBar in NavigationBar
- (void)viewDidLoad {
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
if (#available(iOS 11.0, *)) {
self.navigationItem.searchController = searchController;
} else {
self.navigationItem.titleView = searchController.searchBar;
}
}
Read more here

Resources