无法使用自定义视图触发条形按钮项目?

时间:2012-01-24 13:03:07

标签: iphone objective-c ios cocoa-touch ios5

在我的iphone应用程序中,

我有一个导航栏要设置的图像我已将其设置为此....

   if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]){

        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationBarReady.png"] forBarMetrics:UIBarMetricsDefault];
    }

    UIBarButtonItem *bbiLeft=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btnBack.png"] style:UIBarButtonItemStylePlain target:self action:@selector(btnBackPressed:)]; 
    [bbiLeft setTintColor:[UIColor clearColor]];
    [bbiLeft setBackgroundVerticalPositionAdjustment:7.0f forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.leftBarButtonItem=bbiLeft;

看起来像这样......

enter image description here

我想将其颜色设置为导航栏的背景

如何? 感谢..

2 个答案:

答案 0 :(得分:2)

得到答案......

/* Back Button setted*/    
    UIButton *btnBack=[UIButton buttonWithType:UIButtonTypeCustom];
    [btnBack addTarget:self action:@selector(btnBackPressed:) forControlEvents:UIControlEventTouchUpInside];
    [btnBack setImage:[UIImage imageNamed:@"btnBack.png"] forState:UIControlStateNormal];
    [btnBack setFrame:CGRectMake(0, 0, 51,16)];
    UIView *backModifiedView=[[UIView alloc] initWithFrame:btnBack.frame];
    [btnBack setFrame:CGRectMake(btnBack.frame.origin.x, btnBack.frame.origin.y+7, btnBack.frame.size.width, btnBack.frame.size.height)];
    [backModifiedView addSubview:btnBack];
    UIBarButtonItem *bbiLeft=[[UIBarButtonItem alloc] initWithCustomView:backModifiedView];
      self.navigationItem.leftBarButtonItem=bbiLeft;



/* Arpit */
//set Navgation Bar.   
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]){

        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationBarReady.png"] forBarMetrics:UIBarMetricsDefault];
    }

答案 1 :(得分:0)

将tintColor设置为clear不起作用,您应该选择与条形背景相匹配的颜色。

此外,您可能需要设置UINavigationBar的tintColor,而不是UIBarButtonItem本身的tintColor,因为导航栏负责对其自己的按钮项进行着色。