将图像添加到UIBarButtonItem按钮

时间:2011-07-25 14:13:24

标签: iphone image uibarbuttonitem

我已使用以下代码将UITabButtonItem添加到我的视图控制器上。有人可以告诉我是否可以将图像与它联系起来?

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(favouriteButtonClicked:)]; 
viewController2.navigationItem.rightBarButtonItem = button; 
[button release];
[self.navigationController pushViewController:viewController2 animated:YES];

我在其他地方使用以下代码但是在上面的代码中我将viewcontroller推到我当前的视图中,这段代码不起作用。我不得不使用上面的代码。

UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(280, 25, 30, 30)];
viewController2.navigationController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

[self.navigationController pushViewController:viewController2 animated:YES];

我不知道出了什么问题但是当我用initWithBarButtonSystemItem替换这个自定义按钮代码时,它工作得很好!!!

最后我整理了这个问题,并在这里发布了工作代码,但我仍然不明白我是否在baseviewcontroller中有一个按钮,我添加如下,它不起作用!我必须使用在viewwillappear中接受的答案中给出的代码!!

    [favButton addTarget:self action:@selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
     self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];  

以下代码仍无效!!!不知道发生了什么。

MPMoviePlayerViewController *videoController = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:filePath]] autorelease];

UIButton* favouritebutton =  [UIButton buttonWithType:UIButtonTypeCustom];
[favouritebutton setBackgroundImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
[favouritebutton addTarget:self action:@selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[favouritebutton setFrame:CGRectMake(280, 25, 30, 30)];
videoController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:favouritebutton] autorelease]; 

[self presentMoviePlayerViewControllerAnimated:videoController];

感谢。

7 个答案:

答案 0 :(得分:21)

UIButton *favButton = [[UIButton alloc] init];

[favButton setImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
[favButton addTarget:self action:@selector(favouriteButtonClicked:)
   forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *button = [[UIBarButtonItem alloc]
                                initWithCustomView:favButton];

self.navigationItem.rightBarButtonItem = button;

[button release];
[favButton release];

答案 1 :(得分:9)

UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
 [button addTarget:self action:@selector(methodAction:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 49, 30)];
 navController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

将其分配给导航控制器,您也可以按照我上面的代码中的说明设置图像。

答案 2 :(得分:5)

好的。最后我解决了这个问题谢谢Maggie和Praveen。只有以下代码才能正常工作。来自你的两个答案的比特:)

UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(280, 25, 30, 30)];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

感谢。

答案 3 :(得分:2)

适用于Swift的

Maggie的优秀解决方案:

let favButton = UIButton()
favButton.setImage(UIImage(named: "unselected"), forState: .Normal)
favButton.addTarget(self, action: "favouriteButtonClicked:", forControlEvents: .TouchUpInside)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: favButton)

答案 4 :(得分:1)

您也可以直接从XIB设置,方法是在工具栏上放置一个圆角按钮,然后更改该圆角按钮的图像并将按钮设置为自定义类型。同时使按钮的背景为清晰的颜色

答案 5 :(得分:1)

这是Swift中的功能,它使用了Maggie的答案:

class func returnBarButtonItemWithImage(theImageName:String, andDownImage theDownImageName:String, andSize theSize:CGFloat, withAction theAction:Selector, andTarget theTarget:AnyObject)->UIBarButtonItem? {

    if let myImage = UIImage(named:theImageName) {

        var myButton = UIButton(frame: CGRectMake(0, 0, theSize, theSize))

        myButton.setImage(myImage, forState: UIControlState.Normal)

        myButton.addTarget(theTarget, action: theAction, forControlEvents: UIControlEvents.TouchUpInside)

        if let myDownImage = UIImage(named: theDownImageName) {

            myButton.setImage(myDownImage, forState: UIControlState.Selected)
        }

        return UIBarButtonItem(customView: myButton)
    }

    return nil
}

答案 6 :(得分:0)

UIButton * button = [[UIButton alloc] init];

[button setImage:[UIImage imageNamed:@"settings"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(RightBarMethod:)
    forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 20, 20)];

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]
                           initWithCustomView:button];

self.navigationItem.rightBarButtonItem = barButtonItem;