I want to remove the title and use only custom image for back button in Navigation Controller.
I came across questions like this and I have tried this:
navigationItem.backBarButtonItem = UIBarButtonItem(image: UIImage(named: Constants.Image.kClose), style: .plain, target: nil, action: nil)
I am putting this in viewDidLoad() of the View Controller which is pushing a UITabbarController. ( I know its not a very good idea to push Tab Bar in a Navigation Controller, but I need to do this).
I am getting the following result:
But I don't want that default blue back button. I just want my custom close button to appear there. How can I achieve this?
Create a bar button
- (UIBarButtonItem *)backButton {
UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 4, 34, 34)];
leftButton.layer.cornerRadius = leftButton.frame.size.width / 2;
leftButton.layer.masksToBounds = YES;
[leftButton setImage:[UIImage imageNamed:#"back"] forState:UIControlStateNormal];
[leftButton addTarget:self action:#selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
return leftBarButton;
}
and set like this
self.navigationItem.leftBarButtonItem = [self backButton];
self.navigationController?.navigationBar.backIndicatorImage = backImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = backImage
self.navigationController?.navigationBar.backItem?.title = ""
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: UIControl.State.highlighted)
Related
I done the following code for customizing the navigation bar back button. but I am facing the problem on setting the original image. Whatever the color of bar tint, same color is applied to that image also. But i want my original image on the Navigation Bar in Objective-C.
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
UIImage *image = [UIImage imageNamed:#"logoHeader"];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:nil];
self.navigationItem.leftBarButtonItem = backButton;
Use imageWithRenderingMode and get the original image.
UIImage *image = [[UIImage imageNamed:#"logoHeader"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:nil];
self.navigationItem.leftBarButtonItem = backButton;
This code 100% working in Swift iOS (for back button custom image set)
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.backgroundColor = .white
self.navigationItem.title = "Nitish"
let backBtn = UIBarButtonItem(image: UIImage(systemName: "arrow.backward"), style: .plain, target: self, action: #selector(tap))
self.navigationItem.hidesBackButton = true
self.navigationItem.leftBarButtonItem = backBtn
}
#objc func tap() {
print("Tap button Clicked")
}
I'm using JSQmessageViewController and trying to programmatically add "back" button & user image on the navigation bar. I'm using the following codes. After running it, there's no "back" button or image. Attached is the screenshot of the simulator. I tested these codes with the normal UIViewController, and they worked.
May I know why they don't work with the JSQmessageViewController? And what should I do to add the "back" button & image on the navigation bar? Thanks a lot!
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 64))
navigationBar.backgroundColor = UIColor.whiteColor()
navigationBar.delegate = self;
let navigationItem = UINavigationItem()
navigationItem.title = strValue
let leftButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "back:")
self.navigationItem.leftBarButtonItem = leftButton
let imgButton = UIButton()
imgButton.setImage(image, forState: .Normal)
imgButton.addTarget(self, action: "EditProfile:", forControlEvents: UIControlEvents.TouchDown)
imgButton.frame = CGRectMake(self.view.frame.width - 60, 0, 41, self.view.frame.height)
var rightButton = UIBarButtonItem(customView: imgButton)
self.navigationItem.rightBarButtonItem = rightButton
navigationBar.items = [navigationItem]
self.view.addSubview(navigationBar)
}
So if you use a navigation controller to present your instantiation of the JSQMessagesViewController then the navigation bar should actually already be there. You can just provide it with those buttons and they will be in the correct place. It also seems that you may be doing things in an outdated syntax. Here is the latest Syntax.
Create a function to add your back button.
func addCancelButtonLeft() {
let button = UIBarButtonItem(barButtonSystemItem: .back, target: self, action: #selector(dismissView))
navigationItem.leftBarButtonItem = button
}
Create the action for the button.
func dismissView() {
dismiss(animated: true, completion: nil)
}
Then for your Image you are actually trying to put a button in that title view. Which is fine.
func titleView() {
let imgButton = UIButton()
imgButton.setImage(image, forState: .Normal)
imgButton.addTarget(self, action: #selector(EditProfile), forControlEvents: .touchUpInside)
navigationItem.titleView = imgButton
}
func EditProfile() {
navigationController?.present(EditProfileViewController(), animated: true, completion: nil)
}
let me know if you have more questions and I will see what I can do. Good luck 👍🏼
I want to create a navigation item "Refresh" with the default XCode refresh Icon programmatically. I know how to create one with the word "Refresh" with the following code. However, I want to have the icon insetead of the word "Refresh" Thanks for helping in advance
let refreshButton = UIBarButtonItem(title: "Refresh", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(FollowVC.refresh))
Superb question.I tried sample one for your question.I got the solution and It works fine.
what you made mistake in your coding is
First your
let refreshButton = UIBarButtonItem(title: "Refresh", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(FollowVC.refresh))
is wrong.You have to use or write
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem, target: AnyObject?, action:Selector)
Second you used UIBarButtonItemStyle.Plain.If you want refresh button you have to set UIBarButtonSystemItem.Refresh.
SWIFT
override func viewDidLoad()
{
// Create the navigation bar
let navigationBar = UINavigationBar(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: self.view.frame.size.width, height: 64))) // set frame here
//set the background color
navigationBar.backgroundColor = UIColor.white
navigationBar.delegate = self as? UINavigationBarDelegate;
// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "title" //If you want to set a tilte set here.Whatever you want set here.
// Create button for navigation item with refresh
let refreshButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.refresh, target: self, action: Selector(("actionRefresh:")))
// I set refreshButton for the navigation item
navigationItem.leftBarButtonItem = refreshButton
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
}
#pragma action method
func actionRefresh(sender: UIBarButtonItem)
{
print("The action button is refreshed")
}
Print statement is
The action button is refreshed
OBJECTIVE C
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
navbar.backgroundColor = [UIColor whiteColor];
navbar.delegate = self;
UINavigationItem * navItem = [[UINavigationItem alloc] init];
navItem.title = #"Title"; //Set Title Here Wahtever You Want Here
UIBarButtonItem *buttonRefresh = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:#selector(actionRefresh:)];
navItem.leftBarButtonItem = buttonRefresh;
navbar.items = #[navItem];
[self.view addSubview:navbar];
}
The NSLog statement is
The button action refresh is performed
See the refresh button in screenshot
Good Solutions
Add Default Icons to Navigation Bar
Navigation Bar Item Programmatically
Adding Navigation Bar item Programmatically
Instead of using .Plain, you should be using .Refresh.
let refreshButton = UIBarButtonItem(barButtonSystemItem: .Refresh, target: self, action: #selector(FollowVC.refresh))
Also you should be using the init(barButtonSystemItem:target:action:) instead of the init(title:style:target:action:).
I'm trying to use a custom back button in UINavigationController but the image is not appearing.
Here's the code:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.translucent = true
self.navigationController!.view.backgroundColor = UIColor.clearColor()
self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "Back-50")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "Back-50")
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
I'm not sure what's going wrong with this code.
Maybe, you should use UIAppearance.
Like this
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UINavigationBar appearance] setBackIndicatorImage: [UIImage imageNamed:#"icon-back"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage: [UIImage imageNamed:#"icon-back"]];
return YES;
}
In swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "")
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "")
return true
}
If you want just your image to appear as the back button then set the tintColor of the navigationBar to clear color and then set an image based UIBarButtonItem as the backBarButtonItem of navigationItem of the controller.
let backButton = UIBarButtonItem(image: UIImage(named: "back"), style: .Plain, target: self, action: "back")
self.navigationController?.navigationBar.tintColor = UIColor.clearColor()
self.navigationItem.backBarButtonItem = backButton
This will make the default back button image (<) not appear (infact it appears but its in clear color so not visible) and just show the back image.
You have to implement those line of code in the targeted viewcontroller that means the controller you will push, because you want to customize back item button which active only when you push a viewcontroller into navigation controller stack. Your code works for me.
navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "Back-50"), style: .Plain, target: self, action: "back")
func back(){
self.navigationController?.popViewControllerAnimated(true)
}
Try this
self.navigationItem.hidesBackButton = YES;
UIButton *btnLeft=[UIButton buttonWithType:UIButtonTypeCustom];
btnLeft.frame=CGRectMake(0, 0, 65, 40);
[btnLeft addTarget:self action:#selector(onClickBackBarItem:) forControlEvents:UIControlEventTouchUpInside];
[btnLeft setImage:[UIImage imageNamed:#"image_name"] forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnLeft];
-(void)onClickBackBarItem:(id)sender
{
// ** your stuff ** //
}
May this help yoy
Try this it`s working for me.
var BackButton : UIBarButtonItem = UIBarButtonItem(image:
UIImage(named: "Back-50"), style: .Plain, target: self, action: "back")
self.navigationItem.leftBarButtonItem = BackButton
func back()
{
// your backcontroller code here....
}
Created a Custom UIBarButtonItem:
UIButton *favButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 42.0, 30.0)];
[favButton setImage:[UIImage imageNamed:#"iphone-navbar-icon-star-normal.png"] forState:UIControlStateNormal];
[favButton addTarget:self action:#selector(actionButtonFavorite:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barBtnFavorites = [[UIBarButtonItem alloc] initWithCustomView:favButton];
And on Button click I am opening a popover.
- (void)actionButtonFavorite:(UIBarButtonItem *)sender
{
self.selectedButtonTag = sender.tag;
favoriteOptionsVC.contentSizeForViewInPopover = CGSizeMake(favoriteOptionsVC.view.frame.size.width, (IS_iOS_VERSION_7?190.0:160.0));
UINavigationController *favoritesNavVC = [[UINavigationController alloc] initWithRootViewController:favoriteOptionsVC];
self.favoritesPopoverController = [[UIPopoverController alloc] initWithContentViewController:favoritesNavVC];
favoriteOptionsVC.containingPopoverController = self.favoritesPopoverController;
[self.favoritesPopoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
But the app is crashing saying:
[UIButton view]: unrecognized selector sent to instance 0x7a7445e0
Sender is instance of UIBarButtonItem:
Can anyone help?
Of course sender is a UIBarButtonItem, it is implicitly stated in your function. The real sender (the instance that is calling the method) is a UIButton.
Try this:
UIBarButtonItem *barBtnFavorites = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(actionButtonFavorite:)];
To make your "custom" UIBarButtonItem use this:
UIBarButtonItem *barBtnFavorites = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"iphone-navbar-icon-star-normal.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(actionButtonFavorite:)];
Made a barbutton item like this -
let dotImage = UIImage(named: "dot")?.withRenderingMode(.alwaysOriginal)
let searchImage = UIImage(named: "search")?.withRenderingMode(.alwaysOriginal)
// let shareImageButton = UIBarButtonItem(image: shareImage, style: .plain, target: self, action: #selector(didTapshareImageButton(sender:)))
let dotImageButton = UIBarButtonItem(image: dotImage, style: .plain, target: self, action: #selector(didTapdotImageButton(sender:)))
let searchButton = UIBarButtonItem(image: searchImage, style: .plain, target: self, action: #selector(didTapSearchButton(sender:)))
Added popover on this -
#objc func didTapdotImageButton(sender: UIBarButtonItem){
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ShareVC")
vc.modalPresentationStyle = .popover
let popover: UIPopoverPresentationController = vc.popoverPresentationController!
popover.barButtonItem = sender
popover.delegate = self
sender.image = UIImage(named: "close")?.withRenderingMode(.alwaysOriginal)
present(vc, animated: true, completion:nil)
}