IOS:更改导航栏中的图像栏按钮

时间:2013-08-14 10:13:16

标签: iphone ios uinavigationcontroller uibarbuttonitem

我想将带有图像的条形按钮项目更改为另一个条形按钮,其中包含不同的图像并执行不同的功能。有什么帮助吗?

3 个答案:

答案 0 :(得分:3)

在代码块中使用这样的内容:

UIImage *buttonImage = [UIImage imageNamed:@"customImage.png"];// set your image Name 
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:buttonImage forState:UIControlStateNormal];
        btn.frame = CGRectMake(0, 0, bImage.size.width, bImage.size.height);
        UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:btn];

        self.navigationItem.barButtonItem = barButton;

答案 1 :(得分:1)

尝试此代码,它是导航栏中图像栏按钮的自定义视图

UIImage* image3 = [UIImage imageNamed:@"mail-48_24.png"];
    CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
    UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
    [someButton setBackgroundImage:image3 forState:UIControlStateNormal];
    [someButton addTarget:self action:@selector(sendmail)
         forControlEvents:UIControlEventTouchUpInside];
    [someButton setShowsTouchWhenHighlighted:YES];

    UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
    self.navigationItem.rightBarButtonItem=mailbutton;
    [someButton release];

答案 2 :(得分:0)

设置原始图像并按下按钮转到目标方法设置另一个图像并根据按钮文本重复此操作..