旋转导航栏按钮

时间:2012-12-24 08:05:29

标签: iphone objective-c ios rotation uinavigationbar

我想将导航栏按钮图像旋转45度。我使用以下代码来执行此操作

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

它适用于其他按钮图像,但不适用于导航栏按钮。我不明白这种意外行为的原因是什么。非常感谢任何帮助....

2 个答案:

答案 0 :(得分:1)

尝试使用此代码创建新的条形按钮项,然后将其设置为导航栏的左/右

+ (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;
}

答案 1 :(得分:0)

我已按以下方式解决了这个问题...

  • UIView拖放到栏按钮项目。
  • UIButton拖放到此视图上。
  • 然后代码旋转,

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