如何在ipad中为UIBarButtonitem设置Image

时间:2013-06-25 10:16:13

标签: ios ipad uibarbuttonitem

我在名为login的项目中有标签栏按钮我想为此分配自定义图像任何想法如何可能?

我正在按照以下方式进行操作但应用程序崩溃。

以下是代码:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStyleBordered target:self action:@selector(loginPressed)];
self.navigationItem.leftBarButtonItem = nil;
[self.navigationItem.rightBarButtonItem setBackgroundImage:[UIImage imageNamed:@"loginN.png"] f

2 个答案:

答案 0 :(得分:1)

以这种方式尝试:

UIImage *image = [UIImage imageNamed:@"facebook.png"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.bounds = CGRectMake( 0, 0, image.size.width, image.size.height);
[btn setImage:image forState:UIControlStateNormal];
[btn addTarget:self action:@selector(loginPressed) forControlEvents:UIControlEventTouchDragInside];
UIBarButtonItem *rbBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = rbBtn;

答案 1 :(得分:0)

试试此代码

UIImage* image = [UIImage imageNamed:@"facebook.png"];
    CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
    UIButton *someButton = [[UIButton alloc] initWithFrame:frame];
    [someButton setBackgroundImage:image forState:UIControlStateNormal];
    [someButton addTarget:self action:@selector(facebookshare)
        forControlEvents:UIControlEventTouchUpInside];
    [someButton setShowsTouchWhenHighlighted:YES];

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

这对你有帮助......