Setting image for UINavigationitem (Leftbarbutton item) in ios - ios

I m trying to setting image for UIBarButtonItem using the below code. But it appears as a blank image in the navigation bar. please help me to resolve the problem. i m entirely new to ios.
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:38.0/255.0 green:126.0/255.0 blue:202.0/255.0 alpha:0.5]];
UIBarButtonItem *lftbarbtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"Cross_Icon.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(additem)];
[self.navigationItem setLeftBarButtonItem:lftbarbtn];
thank u in advance.......

UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(popToBack)]; //create the left bar button
self.navigationItem.leftBarButtonItem = leftBarButton; //assign into navigation item
self.navigationItem.leftBarButtonItem.tintColor=[UIColor colorWithRed:125.0/255.0 green:90.0/255.0 blue:146.0/255.0 alpha:1]; //set the tint color
[leftBarButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor purpleColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal]; //text color
-(void)popToBack
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
in another choice
UIButton *leftbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftbutton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[leftbutton addTarget:target action:#selector(buttonAction)forControlEvents:UIControlEventTouchUpInside];
[leftbutton setFrame:CGRectMake(0, 0, 53, 31)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(3, 5, 50, 20)];
[label setFont:[UIFont fontWithName:#"Arial-BoldMT" size:13]];
[label setText:title];
label.textAlignment = UITextAlignmentCenter;
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor clearColor]];
[leftbutton addSubview:label];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView: leftbutton];
self.navigationItem.leftBarButtonItem = barButton;
-(void) buttonAction
{
[self.navigationController popToRootViewControllerAnimated:YES];
}

You do not need to use the navigation bar directly. Use the current view controller's navigation item property to set the UIBarButtomItems. Make sure you are actually using a navigationController to set these properties.

UIButton *ReqMenuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
ReqMenuBtn.frame = CGRectMake(0,0,32,32);
[ReqMenuBtn setBackgroundImage:[UIImage imageNamed:#"reselect.png"] forState:UIControlStateNormal];
[ReqMenuBtn addTarget:self action:#selector(somemethodName:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *MenuButton = [[UIBarButtonItem alloc]initWithCustomView:ReqMenuBtn];
[self.navigationItem setLeftBarButtonItem:MenuButton];
MenuButton = nil;
ReqMenuBtn = nil;

Related

How to change Bar Button Item colour?

In my detail view controller (part of navigation controller application) I've added custom "Back" button, like this:
- (void)loadView
{
[super loadView];
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 10.0f, 24.0f)];
UIImage *backImage = [UIImage imageNamed:#"left_arrow_icon"];
[backButton setBackgroundImage:backImage forState:UIControlStateNormal];
[backButton setTitle:#"" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
}
-(void) popBack {
[self.navigationController popViewControllerAnimated:YES];
}
As you can see I added left bar button and assign "popBack" action. Basically left_arrow_icon is silver but when I press it, iOS change it darker grey.
My question is, can I (and how) change initial colour to white? Is that possible?
Edit. I use xcode5
Edit2:
That does't work too
Try this code. It works perfectly for me -
UIImageView *navigationBarImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Navigation.png"]];
navigationBarImage.frame = CGRectMake(0, 0, 320, 44);
navigationBarImage.userInteractionEnabled = YES;
navigationBarImage.backgroundColor = [UIColor colorWithRed:28.0/255.0 green:53.0/255.0 blue:102.0/255.0 alpha:1.0];
[self.view navigationBarImage];
[navigationBarImage release];
UIButton *backBarBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
backBarBtn1.frame = CGRectMake(13, 12, 20, 20);
[backBarBtn1 setImage:[UIImage imageNamed:#"ic_back3.png"] forState:UIControlStateNormal];
backBarBtn1.backgroundColor = [UIColor clearColor];
[backBarBtn1 addTarget:self action:#selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBarBtn1];
Try this code -
[backbutton setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:HELVETICA_REGULAR_FONT size:17.0], UITextAttributeFont, nil] forState:UIControlStateNormal];

How to change the title color of UIToolbar buttons?

Is there any easy method to change the title color of UIToolBar button title like what we are using for UIBarButtonItem:
[[UIBarButtonItem appearance]
setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],
UITextAttributeTextColor,nil]
forState:UIControlStateNormal];
UITextAttributeTextColor is deprecated in iOS >= 7.0.
Use setTintColor:(UIColor *) instead to change color of button's title.
Set default title color for UIToolBar:
[_myUIToolBar setTintColor:[UIColor redColor]];
or set title color of single UIBarButtonItem:
[_myUIBarButtonItem setTintColor:[UIColor blueColor]];
+(UIBarButtonItem*)barButtonItemWithTint:(UIColor*)color andTitle:(NSString*)itemTitle andTarget:(id)theTarget andSelector:(SEL)selector
{
UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:itemTitle, nil]] autorelease];
button.momentary = YES;
button.segmentedControlStyle = UISegmentedControlStyleBar;
button.tintColor = color;
[button addTarget:theTarget action:selector forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *removeButton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
return removeButton;
}
used this code or http://fredandrandall.com/blog/2011/03/31/how-to-change-the-color-of-a-uibarbuttonitem/ check this link
Throughtout APP:
UIToolbar.appearance().barTintColor = TOOLBAR_BACKGROUND_COLOR
UIToolbar.appearance().tintColor = TOOLBAR_TITLE_COLOR
Also
if let font = UIFont(name: "AvenirNext-DemiBold", size: 15) {
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font,NSForegroundColorAttributeName:TOOLBAR_TITLE_COLOR], forState: UIControlState.Normal)
}
//Best Way.. Try with setting the images for UIBarButtonItem..
UIButton *a1 = [UIButton buttonWithType:UIButtonTypeCustom];
[a1 setFrame:CGRectMake(0.0f, 0.0f, 18.0f, 18.0f)];
[a1 addTarget:self action:#selector(BarButton_Clicked:) forControlEvents:UIControlEventTouchDown];
[a1 setImage:[UIImage imageNamed:#"UnSelected.png"] forState:UIControlStateNormal];
[a1 setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateSelected];
UIBarButtonItem *btn_Image = [[UIBarButtonItem alloc] initWithCustomView:a1];
UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
[flexibleSpace setWidth:25.0];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:flexibleSpace,btn_Image,nil];
here the sample code:
[UIToolbar appearance]
Adding after your text attribute:
Here a UIToolBar class reference:
UIToolBar class

Custom the Navigation Item on the navigationBar

As the pic shows, I have successfully custom the backgroud of the navigationBar of the UINavigationController by the code:
UINavigationController *nav = ......
[[nav navigationBar] setBackgroundImage:[UIImage imageNamed:#"bg_daohang"] forBarMetrics:UIBarMetricsDefault];
Now, I want to custom the item on the navigationBar (in the red circle). Is it possible to do this and how to do it?
UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(0.0, 0, 60.0, 30.0);
[button setBackgroundImage:[UIImage imageNamed:#"logout.png"] forState:UIControlStateNormal];
[button setTitle:#"Logout" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont boldSystemFontOfSize:13];
[button addTarget:self action:#selector(logout) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem= item;
[item release];

How to set UIBarButtonItem with out border

I have created view with barbutton as follows
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"settingsicon.png"] style:UIBarButtonItemStylePlain target:self action:#selector(settingsButtonClicked:)];
It is comming like this
but I want it as
how can I remove the border of the button?
Use this :
UIBarButtonItem *barButton= [[UIBarButtonItem alloc] initWithCustomView:customButton];
You can initialize a button with whatever view you would like. You can make a button with any (acceptable) view inside it.
eg.
// GRAPHICAL BUTTON FRAMEWORK
UIButton* btton = [UIButton buttonWithType:UIButtonTypeCustom];
[btton setFrame:CGRectMake(0, 0, 30, 30)];
[btton addTarget:self action:#selector(SOMEROUTINE) forControlEvents:UIControlEventTouchUpInside];
[btton setImage:[UIImage imageNamed:#"SOME IMAGE"] forState:UIControlStateNormal];
UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithCustomView:btton];
Hope it helps you.
Another way is to remove button's background image:
// portrait
[self.navigationItem.leftBarButtonItem setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
// landscape
[self.navigationItem.leftBarButtonItem setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsLandscapePhone];

Change backgroundcolor for UIButton IPhone

I have a problem. I use a Button as a BarButtonItem. It works fine so far, but my backgroundcolor works only if I click on my button. How can I make it so that my backgroundcolor will be set every time ?
UIButton *redEmergencyButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
redEmergencyButton.frame = CGRectMake(100, 100, 100, 50);
redEmergencyButton.backgroundColor = [UIColor colorWithRed:0.8
green:0.898039215686274509803
blue:1.0
alpha:1.0];
[redEmergencyButton setTitle:#"Emergency"
forState:UIControlStateNormal];
[redEmergencyButton addTarget:self
action:#selector(doEmergency)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rButton = [[UIBarButtonItem alloc]
initWithCustomView:redEmergencyButton];
You can use this line of code for your navigation bar
[self.navigationcontroller.navigationbar setTintColor:[UIColor bluecolor]];
for button use
navigation button there.
Try to use this button instead:
UIBarButtonItem* emergencyButton = [[UIBarButtonItem alloc]initWithTitle:#"Emergency" style:UIBarButtonItemStyleBordered target:self action:#selector(doEmergency)];
[emergencyButton setTintColor: [UIColor colorWithRed:0.8
green:0.898039215686274509803
blue:1.0
alpha:1.0];
UIButtonTypeRoundedRect is deprecated in iOS7, Use UIButtonTypeCustom instead for your custom bgColor.
UIButton *redEmergencyButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
redEmergencyButton.frame = CGRectMake(100, 100, 100, 50);
redEmergencyButton.backgroundColor = [UIColor colorWithRed:0.8
green:0.898039215686274509803
blue:1.0
alpha:1.0];
[redEmergencyButton setTitle:#"Emergency"
forState:UIControlStateNormal];
[redEmergencyButton addTarget:self
action:#selector(doEmergency)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rButton = [[UIBarButtonItem alloc]
initWithCustomView:redEmergencyButton];

Resources