How to remove/avoid small white splash light on UIBarButtonItem programmatically - iphone

My issue is same as question: How to disable the the light that appears when touching a UIBarButtonItem?
I have read answer if we use Interface builder. What if I add toolbar programmatically? How can I avoid the light on the bar button item?

To do what you want programatically you can do
UIBarButtonItem *item;
UIButton *b2=[[UIButton alloc] initWithFrame:frame];
[b2 addTarget:self action:#selector(settings:) forControlEvents:UIControlEventTouchUpInside];
//you can set the background image or whatnot
[b2 setBackgroundImage:[UIImage imageNamed:#"settings.png"] forState:UIControlStateNormal];
item=[[UIBarButtonItem alloc] initWithCustomView:b2];
//then set the button on the UIToolbar through the items property

Related

Adding pagination to the right side of a UINavigationbar

A designer asked me to add pagination to a UINavigation bar in a project with its target building for ios4.3+.
I know you can add two buttons to the right side of a Navigation bar as explained here in this blog : enter link description here
But to get the appearance requested to by the designer
Do I need to add images to my button or is this some kind of native control(Special UISegmentedControl?) I haven't seen before? Or Just 2 custom buttons with background images.
Thanks,
-Code
I implemented the same function with a segmented control and without defining custom buttons with images.Instead of adding images for arrows i used the the corresponding Unicode chars and instantiated the segmented control with them.
UISegmentedControl *toggleSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"\u25B2",#"\u25BC",nil]];
UIBarButtonItem *toggleSegmentedControlBarItem = [[UIBarButtonItem alloc] initWithCustomView:toggleSegmentedControl];
self.navigationItem.leftBarButtonItem = toggleSegmentedControlBarItem;
Then your leftBarButtonItem is replaced by the segmented control.
If I undestand, you want put a image in each UIBarButtonItem and setting button status (when is clicked or normal condition). If yes, you can try this code
`UIImage *buttonImageNormal = [UIImage imageNamed:#"image.png"];
UIImage *buttonImagePressed = [UIImage imageNamed:#"imagepressed.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:buttonImage forState:UIControlStateNormal];
[myButton setImage:buttonImage forState: UIControlStateHighlighted];
UIBarButtonItem aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myButton];
// Create and Set the Target and Action for aButton
[myButton addTarget:self action:#selector(myButtonClicked:) forControlEvents:UIControlEventTouchUpInside];`

How to change the frame of UIBarButtoItem when inserted in the navigation bar?

I have a navigation controller in my app. I am adding UIBarButtonItem in the navigation bar of a view. The default width and height of UIBarButtonItem when I added them into the nav bar is 44*30. I want a UIBarButtonItem of size 30*30 in the navigation bar. How can I achieve this?
I have tried changing the width property of UIBarbutton, but while exploring I come to know that it is possible only when we insert UIBarButtons in the ToolBar to change the default width and not possible when we insert into the navigation bar. Is there any way through which I can achieve this?
My basic need is that I want two square (30*30) bar buttons at the left of the nav bar and one on right of the nav bar. Yes I can insert simple button there and set there frames instead of bar buttons. But I just want to know is it possible to change the width of bar buttons.If yes,What is the way?
Create One UIButton according your want and assign it as left and right bar button.You can change frame as per your want.In the same way you can do for right button.
UIButton *mybutton = [UIButton buttonWithType: UIButtonTypeCustom];
[mybutton setImage:[UIImage imageNamed:#"anyimage.png"]];
[mybutton setFrame:CGRectMake(0,0,30,30)];
[mybutton addTarget:self action:#selector(onclick:) forControlEvents: UIControlEventTouchUpInside];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView: mybutton];
[self.navigationItem setLeftBarButtonItem:customItem];
Updated:-Please use this as per your Border style.
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc]initWithTitle:#"Test" style:UIBarButtonItemStyleBordered target:self action:#selector(test:)]];
UIButton *desiredBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:desiredBtn];
then add this barbutton to navigationBar
self.navigationItem.rightBarButtonItem = aBarButtonItem;

are customViews for UIBarButtonItems ignored for the leftBarButtonItem

From the documentation, I see this for the backBarButtonItem:
When this property is nil, the navigation item uses the value in its
title (page 10) property to create an appropriate back button. If you
want to specify a custom image or title for the back button, you can
assign a custom bar button item (with your custom title or image) to
this property instead. When configuring your bar button item, do not
assign a custom view to it; the navigation item ignores custom views
in the back bar button anyway.
I didn't know if this was the same for leftBarButtonItem? Basically I have this code:
UIButton *homeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
UIImage *homeImage = [UIImage imageNamed:#"icon_house.png"];
[homeButton setImage:homeImage forState:UIControlStateNormal];
[homeButton addTarget:self action:#selector(homePressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *homeBBI = [[UIBarButtonItem alloc] initWithCustomView:homeButton];
Prior to iOS 5, I put this in a UIToolBar and it showed up fine. Now I want to put this as the leftBarButtonItem, to the right of the UINavigationController's backButton. When I set it, it does not show up at all. There is no image. However, when I create some button like this:
UIBarButtonItem *hButton = [[UIBarButtonItem alloc] initWithTitle:#"home" style:UIBarButtonItemStylePlain target:self action:#selector(homePressed:)];
and set it as the leftBarButtonItem, it shows up. I didn't know how I could get my custom icon for my home button without the borders. When I use:
UIBarButtonItem *hButton2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"icon_house.png"] style:UIBarButtonItemStylePlain target:self action:#selector(homePressed:)];
I get a border around my house_icon that i do not want. Thanks.
try this
UIButton *TastoVersamento = [UIButton buttonWithType:UIButtonTypeCustom];
[TastoVersamento setImage:[UIImage imageNamed:#"Versamento.png"] forState:UIControlStateNormal];
[TastoVersamento addTarget:self action:#selector(Click_Versamento:) forControlEvents:UIControlEventTouchUpInside];
[TastoVersamento setFrame:CGRectMake(0, 0, 40, 40)];
[[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:TastoVersamento] autorelease]];
backBarButtonItem and leftBarButtonItem are different. backBarButtonItem is the default if no leftBarButtonItem is given. It will also show up even if leftBarButtonItem is set if leftItemsSupplementBackButton is YES.
In your first example, you could try [homeButton sizeToFit]. I think the frame may not be getting set correctly.
But as to your last comment, I think you won't be succesful in removing the border from navigation item buttons. I'm pretty sure they're hard coded. In fact, if you add a button that has a border, you'll get two, one from the button and one from the navigation item.

Is it possible to have a UIBarButtonItem as a cell accessoryView?

I have a tableview, where one of the cells is supposed to display a thumbnail image. However, if that image is not available (hasn't been taken yet), I'd LIKE for the cell's accessoryView to display the camera UIBarButtonItem...but when I try my usual approach I get 'incompatible types' warnings. Is there any way to use those items outside of a nav bar?
It is not possible to directly use UIBarButtonItem as the cell's accessoryView, because UIBarButtonItem is not a UIView.
A suboptimal solution could be to use a UIToolbar to hold your UIBarButtonItem. You can then add the UIToolbar as the cell's accessoryView, but you'll end up with the unwanted outline of the UIToolbar. This SO answer explains how to make a transparent UIToolbar subclass. That way you won't see the toolbar's background, but you'll also lose the outline of the UIBarButtonItem - it will only show the white camera icon:
UIToolbar *toolbar = [[TransparentToolbar alloc] init];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(someAction)];
toolbar.frame = CGRectMake(0, 0, 40, 30);
[toolbar setItems:[NSArray arrayWithObject:item] animated:NO];
cell.accessoryView = toolbar;
[item release];
[toolbar release];
I think that is impossible. Since it is called BarButtonItem it can only go into a bar. The old method allthough works: Take a screenshot of the camera button (just add it to any navigationbar for the screenshot), cut it out from the screenshot, add a regular UIButton to your cell and add the image of your camera button as backgroundimage for your UIButton using
[button setBackgroundImage:[UIImage imageWithImage:#"camerabutton.png"] forState:UIControlStateNormal];
And also take a screenshot of the button when its pressed and save it and set it as image for selected state:
[button setBackgroundImage:[UIImage imageWithImage:#"camerabuttonpressed.png"] forState:UIControlStateHighlitedl];

iPhone SDK: UIBarButtonItem is not displaying border

I have a UIToolbar which I am trying to put some custom UIBarButtonItems on. However, when I use the code below, the button shows up with NO border.
UIImage *cameraRollButtonImage = [UIImage imageNamed:#"Flash.png"];
UIButton *cameraRollButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cameraRollButton setImage:cameraRollButtonImage forState:UIControlStateNormal];
cameraRollButton.frame = CGRectMake(0.0, 0.0, cameraRollButtonImage.size.width, cameraRollButtonImage.size.height);
// Initialize the UIBarButtonItem
cameraRollButtonItem = [[UIBarButtonItem alloc] initWithCustomView:cameraRollButton];
[cameraRollButtonItem setStyle:UIBarButtonItemStyleBordered];
//Add the Buttons to the toolbar
NSArray *toolbarItems = [NSArray arrayWithObject:cameraRollButtonItem];
[self.cameraTabBar setItems:toolbarItems];
This displays the button just fine, however, there is NO button border (like standard the UIBarButtonItem). So the line [cameraRollButtonItem setStyle:UIBarButtonItemStyleBordered]; doesn't seem to do anything.
Does anyone have any experience with this?
I would like to be able to eventually rotate the image in the button when the device orientation is changed (keeping the toolbar static), so simply adding an image to the UIBarButtonItem doesn't work; I need to get this to work with by using the customView property.
Many thanks!
Brett
Have you considered creating your own button image with a border? You can use it as the backgroundImage of a UIButton:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:_backgroundImage_ forState:UIControlStateNormal];
// So that the button does not gray out when disabled
[button setBackgroundImage:_backgroundImage_ forState:UIControlStateDisabled];
[button setImage:_cameraImage_ forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 125, 30);
You could then use button with initWithCustomView:.
The PSD file here might give you an overview of how to create your own button.