Custom text in navigation bar - ios

I want to set a text on the position of leftBarButtonItem/rightBarButtonItem in the UINavigationBar. You have to use UIBarButtonItem, but you can put different UIView objects into it. First I tried to use a label, but that doesn't work. The code is in C#, but you should get the idea:
UILabel textLabel = new UILabel();
textLabel.Text = "My custom text which should be displayed in navigation bar";
UIBarButtonItem customBarButtonItem = new UIBarButtonItem (textLabel);
NavigationItem.RightBarButtonItem = customBarButtonItem;
The label is not shown. If I use a button it seems to work (except the styling has to be adapted).
UIBarButtonItem customBarButtonItem = new UIBarButtonItem ("My custom text which should be displayed in navigation bar", UIBarButtonItemStyle.Plain,null);
NavigationItem.RightBarButtonItem = customBarButtonItem;
Why isn't it possible to use a UILabel? What is the correct way of showing a text like a header in a UIBarButtonItem?
Edit:
My current findings are:
Either set the title of UIBarButtonItem or set the frame of UILabel (thanks to Anbu.Karthik for this).

change
UILabel testLabel = new UILabel();
testLabel.frame=CGRectMake(0, 0, 200, 21);
testLabel.Text = "yuhu";
UIBarButtonItem test = new UIBarButtonItem (testLabel.text);

Change this line of code UIBarButtonItem test = new UIBarButtonItem (testLabel);
UILabel testLabel = new UILabel();
testLabel.Text = "yuhu";
testLabel.frame=CGRectMake(x, y, width, height);
//change this line
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:testLabel];
NavigationItem.RightBarButtonItem = test;

Use this
UIImage *buttonImage = [UIImage imageNamed:#"back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;

Related

UIBarButtonItem Target Method is not working

Actually i want to place image and label on the toolbar as a UIBarButtonItem and provide a clickable effect to that Button.
So what i have done here is I have created one custom view and placed Image and Label on the same custom view and finally placed this custom view as UIBarButtonItem CustomView.
But when i set target and action for the same UIBarButtonItem, it is not calling the selector method.
The entire code is below.
Can anybody suggest me what's the mistake in my code ? and is there any other approach to achieve the same ????
Early suggestions would be much appreciated.
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setToolbarHidden:NO];
UIView *customView = [[UIView alloc]
initWithFrame:CGRectMake(0,0,self.navigationController.toolbar.frame.size.width,self.navigationController.toolbar.frame.size.height)];
customView.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:187.0/255.0 blue:150.0/255.0 alpha:1.0];
[self.navigationController.toolbar addSubview:customView];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(60,0,44,44)];
imgView.image = [UIImage imageNamed:#"documentImage.png"];
[customView addSubview:imgView];
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(104,0,145,44)];
lbl.text = #"Scan Document";
lbl.textAlignment = NSTextAlignmentLeft;
[customView addSubview:lbl];
UIBarButtonItem *bar = [[UIBarButtonItem alloc]initWithCustomView:customView];
bar.target = self;
bar.action = #selector(scanDocument);
self.toolbarItems = [NSArray arrayWithObjects:bar, nil];
}
If you want to create button right hand side here is the code. i have already implemented it and working fine.
//Button Right side
UIImage *imgFavRest = [UIImage imageNamed:#"documentImage.png"];
UIButton *cart = [UIButton buttonWithType:UIButtonTypeCustom];
[cart setImage:imgFavRest forState:UIControlStateNormal];
cart.frame = CGRectMake(0, 0, imgFavRest.size.width, imgFavRest.size.height);
[cart addTarget:self action:#selector(showCart) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithCustomView:cart];
self.navigationItem.rightBarButtonItem = rightBtn;
Then use method like this . that's all
-(void)cartItemCount{
// Method
}
float textwidth = 50; // according to your text
UIImage *image = [UIImage imageNamed:#"logout"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.bounds = CGRectMake( 10, 0, image.size.width+textwidth, image.size.height );
[btn addTarget:self action:#selector(scanDocument) forControlEvents:UIControlEventTouchUpInside];
[btn setImage:image forState:UIControlStateNormal];
[btn setTitle:#"test" forState:UIControlStateNormal];
[btn setTitleEdgeInsets:UIEdgeInsetsMake(0.0, 5.0, 0.0, 0.0)];
//Adjust the coordinates and size of your button as your requirement.This is a sample code to guide you.
//PS:Take a UIButton in the nib file>Make it a custom button>Connect the IBOutlet to your custom button in the nib file.Thats it.
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = rightButton;
I see very complicated answers, all of them using code. However, if you are using Interface Builder, there is a very easy way to do this:
Select the button and set a title and an image. Note that if you set
the background instead of the image then the image will be resized if
it is smaller than the button.IB basic image and title
Set the position of both items by changing the edge and insets. You
could even control the alignment of both in the Control section.
IB position set IB Control set
You could even use the same approach by code, without creating UILabels and UIImages inside as other solutions proposed. Always Keep It Simple!
EDIT: Attached a small example having the 3 things set (title, image and background) with correct insets Button preview
Note:: UIBarButtonItem inherits from UIBarItem and NSObject so it doesn't know anything about touches. It would be nice if the docs mentioned that the action and target properties only apply if the custom view is a UIButton.

Changing vertical position of UIBarButtonItem on iOS 8.2

Here is my navigation bar
I had a problem about changing vertical position of Settings UIBarButtonItem on my navigation bar. I would like to move the button item "Settings" down
Here is my code
UIBarButtonItem *settingsItem = [[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStylePlain target:self action:nil];
self.navItem.rightBarButtonItem = settingsItem;
[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0,-10) forBarMetrics:UIBarMetricsDefault];
I had tried it again and again. It seemed that it's not work
Could anyone suggest me how to move the button item "Settings" down ?
You can easily add a Custom Button to your NavigationBarItem, Here is the way,
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; //create custom Button
[button setTitle:#"Settings" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
[button setTitleColor:[UIColor colorWithRed:179.0/255.0 green:40.0/255.0 blue:18.0/255.0 alpha:1] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 50, 20); //Button frame
[button addTarget:self action:#selector(yourCustomSelectorHere) forControlEvents:UIControlEventTouchUpInside]; //Add action method to the button here
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 63, 33)];
backButtonView.bounds = CGRectOffset(backButtonView.bounds, -14, -7);
[backButtonView addSubview:button];
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithCustomView: backButtonView]; //set button as UIBarButtonItem
self.navigationItem.rightBarButtonItem = barBtnItem; //set barBtnItem to rightBarButtonItem
By changing backButtonView.bounds values you can change the origin of the Button
Cited from apple's documentation on UIAppearance protocol
iOS applies appearance changes when a view enters a window, it doesn’t
change the appearance of a view that’s already in a window.
So if your code of setting UIBarButtonItem's appearance is after the navigationItem assignment code. It won't work

UIBarButtonItem backButton setting image with no back text causes title alignment issues

I am wanting to use an image for my backButton in my navigationController. I just want the image used, with no "back" text shown at all, so I did:
UIImage *backButtonImage = [[UIImage imageNamed:#"ZSSBackButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 25, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, backButtonImage.size.height*2) forBarMetrics:UIBarMetricsDefault];
This works well, but seems to have the side effect to making long navigationItem titles appear to be off center, even though it really is just leaving white space where the backbutton text should be:
Is there a way that I can bring the title a little more to the left?
You can manage position of UINavigationbar title using titleView property.
UIView *backView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];// Here you can set View width and height as per your requirement for displaying titleLbl position in navigationbar
[backView setBackgroundColor:[UIColor clearColor]];
UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
[backView addSubview:titleLbl];
titleLbl.text = #"YOUR TITLE";
titleLbl.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = backView;
EDIT : you can create custom button and assign it to leftbutton as :
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(0,0,51,31);
[btnBack setBackgroundImage:[UIImage imageNamed:#"ZSSBackButton"] forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(btnBackProfile_TouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
[self.navigationItem setLeftBarButtonItem:leftBtn];
Hope it helps you.

Set a button to right of backbutton of navigation bar iPad monotouch

I want to have a uibarbuttonitem in right of back button in navigation bar ( if it is not wrong with apples guide line). I use this code:
UIBarButtonItem listButton = new UIBarButtonItem ("List View",UIBarButtonItemStyle.Bordered ,null);
//assigning left bar buttons
this.NavigationItem .LeftBarButtonItems =
new UIBarButtonItem[] { this.NavigationItem .BackBarButtonItem ,listButton } ;
But this.NavigationItem .BackBarButtonItem always is null. What solution is right for this purpose?
This is the correct implement the same :
UIButton *leftButtonIcon = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, width, height)];
[leftButtonIcon setImage:[UIImage imageNamed:#"yourButtonImageName.png"] forState:UIControlStateNormal];
[leftButtonIcon addTarget:self.viewDeckController action:#selector(YourSelectorMethod) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftbtn = [[UIBarButtonItem alloc] initWithCustomView:leftButtonIcon];
[self.navigationItem setLeftBarButtonItem:leftbtn];

How to have iOS set a gradient on UIBarButtonItem image?

just wondering, I customised the UIBarButtonItems the following way
+ (UIBarButtonItem *)createBarButtonItemWithTitle:(NSString *)t target:(id)tgt action:(SEL)a
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [[UIImage imageNamed:#"blabla.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
CGRect buttonFrame = [button frame];
buttonFrame.size.width = 35;
buttonFrame.size.height = buttonImage.size.height;
[button setFrame:buttonFrame];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button addTarget:tgt action:a forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
return buttonItem ;
}
But now, I don't have the shading/glossy effect on the button background image....
Is there a way to have it done automatically, or I have to do it programatically ?
EDIT:
To totally customize the bar button in such a way you'd need to provide something other than the flat green image in "blabla.png".
A different way to handle this would be to create a bordered style button with your image. Something like:
UIBarButtonItem* barButton = [UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"blabla.png"] style:UIBarButtonItemStyleBordered target:tgt selector:a];
The UIBarButtonItemStyleBordered style gives it the 3D raised button-y look. You can tint its appearance to match your bar if desired.

Resources