How can I display a UIBarButtonItem without a border? - iphone

In IB I can select a UIBarButtonItem, set it to Plain, Custom and apply an Image and it looks fine in a UIToolbar or UINavigationBar.
When I try to do this in code, I get a border:
backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"arrowBack"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(backButtonClicked:)];
How can I get a UIBarButtonItem without a border? I want it to be Plain and Custom

This works for me (example for the leftButton):
UIButton *bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setFrame:CGRectMake(0, 0, 15, 30)];
[bt setImage:[UIImage imageNamed:#"backBT"] forState:UIControlStateNormal];
[bt addTarget:self action:#selector(popViewController:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftButton=[[UIBarButtonItem alloc] initWithCustomView:bt];
self.navigationItem.leftBarButtonItem=leftButton;

Related

UIBarButtonItem not showing image

I'm having a problem with my navigation bar. The issue is that I'm trying to overwride the LeftBarButtonItem that the NavigationBar has from the super viewController like this:
"Super ViewController"
UIImageView *logoApp = [[UIImageView alloc] initWithImage:[UIImageimageNamed:#"logo.png"]];
[logoApp setFrame:CGRectMake(5, 5, 55, 30)];
UIBarButtonItem *containingLogoApp = [[UIBarButtonItem alloc] initWithCustomView:logoApp];
self.navigationItem.leftBarButtonItem = containingLogoApp;
Now I'm trying to overwrite it in a different viewControllerB like this:
UIImageView *backImg = [[UIImageView alloc] initWithImage:[UIImageimageNamed:#"back.png"]];
[logoApp setFrame:CGRectMake(5, 5, 17, 22)];
UIBarButtonItem *containingBackButton = [[UIBarButtonItem alloc]initWithCustomView:backImg];
self.navigationItem.leftBarButtonItem = containingBackButton;
It's the same code, and it works only the first time the app runs. If I run it a second time, it doesn't show the image assigned in ViewControllerB anymore. This happens with every image that I have in my project, it only works the first time I run the app on the simulator, after that first run... it's like nothing happens at all.
How can I overwrite the leftBarButtonItem?
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(0, 0, 50, 30)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
try this code
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"dynamic_blue_left.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(rightSwipeClicked:)];
self.navigationItem.leftBarButtonItem = backButton;
It's working fine.. may be usefull for you

Cant set image to backBarButtonItem

i have UITableView based application, and i can't set backBarButtonItem properly.
Back button is button, which appears when i select any item, call second view with method didSelectRowAtIndexPath. When i try to set it like this:
UIButton *back1=[UIButton buttonWithType:UIButtonTypeCustom];
[back1 setFrame:CGRectMake(10.0, 2.0, 45.0, 40.0)];
[back1 addTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
[back1 setImage:[UIImage imageNamed:#"back_button.png"] forState:UIControlStateNormal];
UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:back1];
self.navigationItem.backBarButtonItem = back;
There is just back button with default style and somehow with title of my TableView controller window.
When i set it like this:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"back_button.png"]
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backButton;
There is an image, but, it look like "inserted" in default back button, doesn't look nice at all.
Please help me.. i tried many ways to solve this, but, in many cases there is just default Back button with title "Products" (there is title which appears on top of navigation bar in my UITableView). Any help would be appreciated!
UIImage *buttonImage = [UIImage imageNamed:#"back_button.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
backButton.font = [UIFont boldSystemFontOfSize:12];
backButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[backButton addTarget:self action:#selector(showMeter)
forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:backButton];
//Create resizable images
UIImage *myButton = [[UIImage imageNamed:#"yourButton"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
//Set the background image for *all* UIBackBarButtons
[[UIBarButtonItem appearance] setBackgroundImage:myButton forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
You should assign it to self.navigationItem.leftBarButtonItem.
self.navigationItem.leftBarButtonItem = backButton;

Is there any way to change text color of UIBarButtonItem?

I want to change text color of UIBarButtonItem,like Pinterest app(navigationItem.backBarButtonItem).
But here's almost same question.
Is there an easy way to change the text color of a UIBarButtonItem without using an image?
So,Why text color of UIBarButtonItem is changed in Pinterest app?
Try setTitleTextAttributes:forState:
You can learn more from here
#interface MyViewCOontroller : UIViewController {
UILabel *lblTotCaratteri;
}
In implementation
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
lblTotCaratteri = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 25, 15)];
lblTotCaratteri.textAlignment = UITextAlignmentCenter;
lblTotCaratteri.textColor =[UIColor greyColor];
hope this helps
You can use UISegmentedControl to simulate the UIBarButtonItem. Make sure you have a segment control with single segment.
- (void) viewDidLoad {
[YOUR_SEGMENTED_CONTROL removeAllSegments];
[YOUR_SEGMENTED_CONTROL insertSegmentWithTitle:#"MyButton" atIndex:0 animated:NO];
}
Retrieve the label of the UISegmentedControl and set the textColor to your preferred color
UILabel* lblText = [[[[YOUR_SEGMENTED_CONTROL subviews] objectAtIndex:0] subviews] objectAtIndex:0];
lblText.textColor = [UIColor blackColor];
I don't the direct way of doing this but you can achieve this by adding a UIBUtton to the customview of the UIBarButtonItem.
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 80, 40);
[backButton setBackgroundImage:[UIImage imageNamed:#"back-button.png"] forState:UIControlStateNormal];// Setting the background Image
[backButton setTitle:#"Back" forState:UIControlStateNormal];//Setting the title of the button
[backButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];// Setting the text color.
[backButton addTarget:self action:#selector(backButtonTaped) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
And this will give you a UIBarButtonItem with the text "Back" in red color.

How to give style in left nav bar button in iphone

I added 2 button with images in top left navigation bar. But I am getting problem in its style. Here is my screenshot of button. And this is code of button which I have written in application.
UIView *viewButtons = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 32)];
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,40,35);
[button1 setBackgroundImage:[UIImage imageNamed: #"remainder.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(sortByremainder) forControlEvents:UIControlEventTouchUpInside];
[viewButtons addSubview:button1];
UIButton *button2 = [[UIButton alloc] init];
button2.frame=CGRectMake(45,0,60,35);
[button2 setBackgroundImage:[UIImage imageNamed: #"like_symbol.jpg"] forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(sortByMostFaved) forControlEvents:UIControlEventTouchUpInside];
[viewButtons addSubview:button1];
[viewButtons addSubview:button2];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:viewButtons];
[viewButtons release];
But i want my button style should be like this image. I want to keep images in button but below screen is more specific with button. It has border and convex area.
For that you need to use grayscale images and that would look better. See example like attached.
You need to use UIBarButtonItem and set this type of images in that and that assign that UIBarButtonItem to self.navigationItem.leftBarButtonItem.
Also, Use BarButtonItemStyle Border like below so you will get as you want.
UIBarButtonItem *btnSetting = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"setting.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(setting:)];
self.navigationItem.rightBarButtonItem = btnSetting;

UIButton as UINavigationbar button

I have a Tab bar application. and in one of the tab bars I have a navigation contorller.
Im trying to set a UIButton with an image as the right button the navigation bar.
UIButton *refreshButton = [[UIButton alloc] init];
[refreshButton setImage:[UIImage imageNamed:#"refresh_icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:#selector(refreshSection) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:refreshButton];
//[rightButton setImage:[UIImage imageNamed:#"refresh_icon.png"]]; << DOESN'T WORK EITHER
self.navigationItem.rightBarButtonItem = rightButton;
[refreshButton release];
[rightButton release];
but the image doesn't show up. I get an invisible button.
EDIT:
I want the button style to be plain or background to be transparent, so that only the image shows. Hence I am using a UIbutton instead of using a UIBarButton directly.
You don't have to create a UIButton and use it as custom view in the UIBarButtonItem. You can create a UIBarButtonItem directly with an image:
UIImage *myImage = [UIImage imageNamed:#"myImage.png"];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStyleBordered target:self action:#selector(refreshSection)];
self.navigationItem.rightBarButtonItem = button;
[button release];
--
Updated, according to comments
Use a UIImageView as custom view in the UIBarButtonItem:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myImage.png"]];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.rightBarButtonItem = rightButton;
[imageView release];
[rightButton release];
--
Updated
Tried the last approach and while this does visually look like you want it to, I can't seem to get it to handle taps. Although the target and action has been set.
--
Final update
I've gotten your initial code in the question up & running. The reason the image is not showing, is because you haven't set the frame of the button. Once you set the frame, the image is shown in the navigation bar.
Here's my snippet I used & tested:
UIImage *myImage = [UIImage imageNamed:#"paw.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
[myButton addTarget:self action:#selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
[myButton release];
Note: I have set the showsTouchWhenHighlighted property of the UIButton to visually hightlight the button when it is pressed.
I ended up doing this
UIButton *refreshButton = [[UIButton alloc] initWithFrame:CGRectMake(292, 9, 27, 27)];
[refreshButton setImage:[UIImage imageNamed:#"refresh_icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:#selector(refresh) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:refreshButton];
[refreshButton release];
It works fine now but I am not really sure if this is the right way of doing it.