Ab'initio
Ab'initio

Reputation: 5418

Rotate navigation bar button

I want to rotate navigation bar button image by 45 degree. I used the following code to do this

    CGAffineTransform cgaRotateHr = CGAffineTransformMakeRotation(-(3.141/2));
    [self.button setTransform:cgaRotateHr];

It is working fine for other button images, but not working for navigation bar button. I dont understand what is the cause of this unexpected behavior. Any help is greatly appreciated ....

Upvotes: 1

Views: 1614

Answers (2)

Ab'initio
Ab'initio

Reputation: 5418

I have solved this problem in following way...

  • Drag and drop an UIView at Bar button item.
  • Drag and drop UIButton on this view.
  • Then code to rotate,

    CGAffineTransform cgaRotateHr = CGAffineTransformMakeRotation(-(3.141/4));
    [self.button setTransform:cgaRotateHr];
    

Upvotes: 0

Quang Hà
Quang Hà

Reputation: 4746

try this code to create new bar button item, then set it to left/right of navigation bar

+ (UIBarButtonItem *) barItemWithImage:(UIImage *)img size:(CGSize)size target:(id)target action:(SEL)selector
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    [button setImage:img forState:UIControlStateNormal];

    button.frame= CGRectMake(0.0, 0.0, size.width, size.height);

    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

    // your transform code
    CGAffineTransform cgaRotateHr = CGAffineTransformMakeRotation(-(3.141/2));
    [button setTransform:cgaRotateHr];

    UIBarButtonItem *forward = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

    return forward;
}

Upvotes: 1

Related Questions