1

我试过这个代码:自定义barbuttonitem:

-(id)initWithImage:(UIImage *)image title:(NSString*)title target:(id)target action:(SEL)action {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.titleLabel.font = [UIFont boldSystemFontOfSize:12];
button.titleLabel.shadowOffset = CGSizeMake(0, -1);
button.titleLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.5];
button.titleLabel.textColor = [UIColor redColor];
[button setTitle:title forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];
[view addSubview:button];
self = [[UIBarButtonItem alloc] initWithCustomView:view];
return self;

}

并使用它:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"my_account_icon.png"] title:@"" target:self action:@selector(buttonClick:)];

但是在 ios 6 事件中没有激活,ios 5 运行正常,请帮助我!!!!

4

2 回答 2

0

尝试以下操作:

-(id)initWithImage:(UIImage *)image title:(NSString*)title target:(id)target action:(SEL)action 
{
    //create whatever custom views you need

    UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];

    UITapGestureRecognizer * gestureRec = [[UITapGestureRecognizer alloc] initWithTarget: target action: action];
    [view addGestureRecognizer: gestureRec];

    //add your custom subviews to the view

    self = [[UIBarButtonItem alloc] initWithCustomView:view];
    return self;
}

将手势识别器添加到 UIBarButtonItem 的子视图对我有用,即使 UIBarButtonItem 是使用自定义视图初始化的。祝你好运!

于 2013-11-15T16:08:55.313 回答
0

UIBarButtonItem带有customViewset 的 s 不会触发动作。

于 2013-06-27T23:40:11.607 回答