Papagalli
Papagalli

Reputation: 111

Transform (rotate) a UIBarButtonItem

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 !

Upvotes: 11

Views: 4644

Answers (2)

Vil
Vil

Reputation: 185

use:

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

Upvotes: 16

Brody Robertson
Brody Robertson

Reputation: 8604

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;

Upvotes: 1

Related Questions