Rotate UIBarButtonItem - iphone

I want to rotate a UIBarButtonItem.
I have been able to do this with UIButtons using setTransform:CGAffineTransformMakeRotation(…), but UIBarButtonItem does not have this property.
I know the Camera-app does this, so it should be achieveable.
How can I achieve this?
Thanks.

Have you tried using a custom view inside the UIBarButtonItem that you then transform any way you want?

UIBarButtonItem does not extend UIView, so it cannot be transformed directly. You can add the UIBarButtonItem you wish to transform to a UIToolbar, transform the UIToolbar and then add the toolbar as a custom view to another UIBarButtonItem. This item can then be set as a navigation item or added to another UIToolbar. However, if you are using a custom view or image then Emil's approach in the comment above is best.
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:#selector(handleForwardItemTouch:)];
UIToolbar *backToolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 44, 44)] autorelease];
[backToolbar setTransform:CGAffineTransformMakeScale(-1, 1)];
UIBarButtonItem *backToolbarItem = [[[UIBarButtonItem alloc] initWithCustomView:backToolbar] autorelease];
self.navigationItem.rightBarButtonItem = backToolbarItem;

You could put a UIButton inside a Bar Button Item, than rotate the UIButton.

I extended UIToolBar, giving access to its subviews, and in it have a function rotate that rotates the buttons in the opposite direction of the bar:
- (void)rotate: (int)degrees{
//for the bar
self.transform=CGAffineTransformMakeRotation(DegreesToRadian(degrees));
//for the subviews (UIBarButtonItems)
for (UIView * subView in self.subviews)
{
if(!CGRectEqualToRect(subView.bounds, self.bounds))
subView.transform = CGAffineTransformMakeRotation(DegreesToRadian(-degrees));
}
}

Related

UIBarButtonItem:setBackgroundVerticalPositionAdjustment not working properly with custom button

I have the following code to customize the display of an UIBarButtonItem (locationButton):
UIButton *locationButtonAux = [UIButton buttonWithType:UIButtonTypeCustom];
[locationButtonAux setFrame:CGRectMake(0, 0, LOCATION_BUTTON_WIDTH, LOCATION_BUTTON_HEIGHT)];
[locationButtonAux setBackgroundImage:[UIImage imageNamed:#"location_button.png"] forState:UIControlStateNormal];
[locationButtonAux setBackgroundImage:[UIImage imageNamed:#"location_button.png"] forState:UIControlStateHighlighted];
[locationButtonAux addTarget:self action:#selector(userLocation) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *locationButton = [[UIBarButtonItem alloc] initWithCustomView:locationButtonAux];
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithTitle:#"title"
style:UIBarButtonItemStyleDone
target:nil action:#selector(someMssage)];
[locationButton setBackgroundVerticalPositionAdjustment:-20.0 forBarMetrics:UIBarMetricsDefault];
self.navigationItem.rightBarButtonItem = locationButton;
And I want to adjust the position of this button into the NavigationBar, because my NavigationBar is taller than normal (is customized using Appeareance).
I'm using the method setBackgroundVerticalPositionAdjustment, as you can see in the code, to do that. But is not working at all with my UIBarButtonItem, no matter what offset I put there, the button always appear close to the bottom of the bar (snapshot).
BUT, if I use a normal UIBarButtonItem with a normal style (the one I called button) I DO can see how the position of the button will be altered the offset of -20. Very strange... Any ideas?
Thank you!
Perhaps something like this would work for you:
UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 30)]; //change this frame to counteract for your taller navbar
[rightView addSubview:locationButtonAux];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightView];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
Hope this helps
You need to put the button inside a blank view and position it within that blank view. Now make that view the custom view of the bar button item.
You really don't need a button here, since this is just an image and you are not using any of the button capabilities (you can attach a gesture recognizer to spot the tap). So here's a simple solution using an image view:
UIImageView *locationButtonAux =
[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"location_button.png"]];
UIView* v = [[UIView alloc] initWithFrame:locationButtonAux.frame];
[v addSubview:locationButtonAux];
CGRect f = locationButtonAux.frame;
f.origin.y -= 10;
locationButtonAux.frame = f;
UIBarButtonItem *locationButton = [[UIBarButtonItem alloc] initWithCustomView:v];
self.navigationItem.rightBarButtonItem = locationButton;
I've omitted the gesture recognizer part but you can easily see how to add it.

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];

How can i rotate a UIBarButtonItem?

How can i rotate a uibarbuttonitem inside a uitoolbar? Because I rotated the uitoolbar (it's vertical now) and my bar button items are rotated too but i dont want that.
thanks
UIBarButtonItem does not extend UIView, so it cannot be transformed directly. You can add the UIBarButtonItem you wish to transform to a UIToolbar, transform the UIToolbar and then add the toolbar as a custom view to another UIBarButtonItem. This item can then be set as a navigation item or added to another UIToolbar.
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:#selector(handleForwardItemTouch:)];
UIToolbar *backToolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 44, 44)] autorelease];
[backToolbar setTransform:CGAffineTransformMakeScale(-1, 1)];
UIBarButtonItem *backToolbarItem = [[[UIBarButtonItem alloc] initWithCustomView:backToolbar] autorelease];
self.navigationItem.rightBarButtonItem = backToolbarItem;

How to add custom toolbar to UIPickerView Control?

I want to add a custom toolbar at the top of the UIPickerView control. I did this on Keyboard but not able to do with UIPickerView.
Nishant
make a toolBar by IBoutlet and set its y coordinate to 100 or more than view's height. then in particular action change y coordinate accordingly. this is done via animation.
You also use this with keyboard .
by using
-(void) keyboardWillShow:(NSNotification *) note
and
-(void) keyboardWillHide:(NSNotification *) note
UIToolBar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(doneBtnTapped:)];
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];
[popoverView addSubview:toolBar];
[popoverView addSubview:pickerView];
I created a custom view and added a done button on that along with picker view.
Thanks

how to integrate UINavigation bar with UIBarbuttonitem?

here is my code....?how to integrate UINavigation bar with UIBarbuttonitem?
UINavigationBar *nav= [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
nav.backgroundColor = [UIColor whitecolor];
//nav.tintColor = [UIColor darkGrayColor];
[self.view addSubview:nav];
[nav release];
Try init your item and then
nav.leftBarButtonItem = barButtonItem;
or
nav.rightBarButtonItem = barButtonItem;
You can only have two items on the navigation bar
Note that UIBarButtonItem buttons are properties of the UINavigationItem, not the UINavigationBar.
For example, when using a UINavigationController, it automatically provides its own UINavigationBar. But each of the views managed by the navigation controller can define a UINavigationItem, which controls the title, left and right buttons, etc.
Update: iOS 5 supports multiple buttons on the same side. From the UINavigationItem docs:
Customizing Views
titleView property
leftBarButtonItems property
leftBarButtonItem property
rightBarButtonItems property
rightBarButtonItem property
– setLeftBarButtonItems:animated:
– setLeftBarButtonItem:animated:
– setRightBarButtonItems:animated:
– setRightBarButtonItem:animated:
So, basically, you would write something like this to add two buttons on the left:
UIBarButtonItem *refreshBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshPlans:)];
UIBarButtonItem *selectYearBtn = [[UIBarButtonItem alloc] initWithTitle:#"Select Year" style:UIBarButtonSystemItemAction target:self action:#selector(selectYear)];
self.navigationItem.leftBarButtonItems = [[NSArray alloc] initWithObjects: refreshBtn, selectYearBtn, nil];
Unfortunately, I haven't seen a way to do this in the Storyboard. Hope this helps.