11
votes

Does anybody know how to transform a UIBarButtonItem ?

I tried this but with no results :( It's not working on both UIBarButtonItem and its customview.

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:5.0f];
CGAffineTransform myTransform = CGAffineTransformMakeRotation(M_PI_2);
UIBarButtonItem * currentItem =  [self.toolbarItems objectAtIndex:4];
currentItem.customView.transform = myTransform;
[UIView commitAnimations];

I confirm the transform works on other views (I tried with self.view).

Thanks !

2
Have you initialized UIBarButtonItem with custom view?Mikayel Aghasyan
Ooops! Haven't noticed the date :)Mikayel Aghasyan

2 Answers

16
votes

use:

UIView *view = [backItem valueForKey:@"view"];
view.transform = CGAffineTransformMakeScale(-1, 1);
1
votes

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;