2016-02-26 39 views
0

其实我想将图像和标签作为UIBarButtonItem放置在工具栏上,并为该按钮提供可点击的效果。UIBarButtonItem目标方法不起作用

所以我在这里完成的是我创建了一个自定义视图,并将图像和标签放在同一个自定义视图上,最后将这个自定义视图作为UIBarButtonItem CustomView放置。

但是,当我设置相同的UIBarButtonItem目标和行动,它不是调用选择器方法。

整个代码如下。

有人可以建议我在我的代码中有什么错误吗?并有没有其他方法来实现相同的?

早期的建议将不胜感激。

- (void)viewDidLoad 

{ 

[super viewDidLoad]; 
[self.navigationController setToolbarHidden:NO]; 


UIView *customView = [[UIView alloc] 
initWithFrame:CGRectMake(0,0,self.navigationController.toolbar.frame.size.width,self.navigationController.toolbar.frame.size.height)]; 

customView.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:187.0/255.0 blue:150.0/255.0 alpha:1.0]; 

[self.navigationController.toolbar addSubview:customView]; 


UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(60,0,44,44)]; 
imgView.image = [UIImage imageNamed:@"documentImage.png"]; 
[customView addSubview:imgView]; 


    UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(104,0,145,44)]; 

    lbl.text = @"Scan Document"; 

    lbl.textAlignment = NSTextAlignmentLeft; 

    [customView addSubview:lbl]; 

UIBarButtonItem *bar = [[UIBarButtonItem alloc]initWithCustomView:customView]; 
bar.target = self; 
bar.action = @selector(scanDocument); 
self.toolbarItems = [NSArray arrayWithObjects:bar, nil]; 
} 
+0

看到它它可以帮助你http://stackoverflow.com/questions/2796438/uibarbuttonitem-target-action-not-working –

+0

你的scanDocument被定义为一个接受参数或不接受? - (void)scanDocument:(UIBarButtonItem *)sender'或者 - (void)scanDocument' –

+0

No Parameter Just - (void)ScanDocument –

回答

0

如果你想创建按钮的右边这里是代码。我已经实施它并且工作正常。

//Button Right side 
    UIImage *imgFavRest = [UIImage imageNamed:@"documentImage.png"]; 
    UIButton *cart = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [cart setImage:imgFavRest forState:UIControlStateNormal]; 
    cart.frame = CGRectMake(0, 0, imgFavRest.size.width, imgFavRest.size.height); 
    [cart addTarget:self action:@selector(showCart) forControlEvents:UIControlEventTouchUpInside]; 

    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithCustomView:cart]; 
    self.navigationItem.rightBarButtonItem = rightBtn; 

然后使用这样的方法。这是所有

-(void)cartItemCount{ 
// Method 
} 
+0

图像应显示在Label之前。所以,它应该像图像第一个标签下一个。我想在调用选择器方法之前将target-action应用于两者并提供可点击的效果。 –

+0

谢谢@Cade。它工作正常,但我总是在viewcontroller的底部总是显示这个按钮总是在顶部(意味着我有一个滚动视图viewcontroller我想总是显示这个botton的顶部滚动查看)。 –

0
float textwidth = 50; // according to your text 
    UIImage *image = [UIImage imageNamed:@"logout"]; 
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    btn.bounds = CGRectMake(10, 0, image.size.width+textwidth, image.size.height); 
    [btn addTarget:self action:@selector(scanDocument) forControlEvents:UIControlEventTouchUpInside]; 
    [btn setImage:image forState:UIControlStateNormal]; 
    [btn setTitle:@"test" forState:UIControlStateNormal]; 
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0.0, 5.0, 0.0, 0.0)]; 

    //Adjust the coordinates and size of your button as your requirement.This is a sample code to guide you. 
    //PS:Take a UIButton in the nib file>Make it a custom button>Connect the IBOutlet to your custom button in the nib file.Thats it. 

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:btn]; 
    self.navigationItem.rightBarButtonItem = rightButton; 

我看的很复杂的答案,他们正在使用的代码。但是,如果您正在使用Interface Builder,则可以使用以下方法:

  • 选择按钮并设置标题和图像。请注意,如果您设置 的背景,而不是图像,然后将图像将被告知是否 它比button.IB基本图像和标题
    enter image description here
  • 通过改变边缘和插图集两个项目的位置大小小。你甚至可以在控制部分控制两者的对齐。 enter image description here
  • IB位置设置IB控制设置 enter image description here

你甚至可以使用通过代码相同的方法,无需内部创建UILabels和UIImages作为提出其他解决方案。始终保持简单!

编辑:附加具有正确的插图按钮预览的3件事设定(标题,图像和背景)一个小例子

注::从UIBarItemNSObjectUIBarButtonItem继承所以它不知道任何接触。如果文档提到动作和目标属性仅适用于自定义视图是UIButton,那将会很好。