如何为 UIBarButtonItem 实现徽章?
Posted
技术标签:
【中文标题】如何为 UIBarButtonItem 实现徽章?【英文标题】:How to implement a Badge for a UIBarButtonItem? 【发布时间】:2010-09-08 17:19:51 【问题描述】:我想在 UIBarButtonItem 上放置一个徽章,用于控制 SplitView 中的弹出框
这段代码什么也没做:
- (void)splitViewController: (UISplitViewController*)svc
willHideViewController:(UIViewController *)aViewController
withBarButtonItem:(UIBarButtonItem*)barButtonItem
forPopoverController: (UIPopoverController*)pc
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
[view setBackgroundColor:[UIColor greenColor]];
UIView *badge = [[UIView alloc] initWithFrame:CGRectMake(36, 0, 8, 8)];
[badge setBackgroundColor: [UIColor redColor]];
[view addSubview:badge];
[badge release];
UIBarButtonItem *aBBItem = [[UIBarButtonItem alloc] initWithCustomView:view];
[aBBItem setTarget:barButtonItem.target];
[aBBItem setAction:barButtonItem.action];
NSLog(@"%@ %s", aBBItem.target, aBBItem.action);
[view release];
barButtonItem.title = @"Bars";
NSMutableArray *items = [[toolbar items] mutableCopy];
[items insertObject:aBBItem atIndex:0];
[aBBItem release];
[toolbar setItems:items animated:YES];
[items release];
self.popoverController = pc;
而以下尝试引发错误:
- (void)splitViewController: (UISplitViewController*)svc
willHideViewController:(UIViewController *)aViewController
withBarButtonItem:(UIBarButtonItem*)barButtonItem
forPopoverController: (UIPopoverController*)pc
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
UIView *badge = [[UIView alloc] initWithFrame:CGRectMake(36, 0, 8, 8)];
[badge setBackgroundColor: [UIColor redColor]];
[button addSubview:badge];
[badge release];
[button addTarget:barButtonItem.target action:barButtonItem.action forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Bars" forState:UIControlStateNormal];
UIBarButtonItem *aBBItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[button release];
barButtonItem.title = @"Bars";
NSMutableArray *items = [[toolbar items] mutableCopy];
[items insertObject:aBBItem atIndex:0];
[toolbar setItems:items animated:YES];
[aBBItem release];
[items release];
self.popoverController = pc;
错误: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Popovers cannot be presented from a UIBarButtonItem that is not in a toolbar or navigation bar already.'
如何向 UIBarButtonItem 添加徽章?
【问题讨论】:
【参考方案1】:我就是这样做的,我们没有重定向目标和动作,而是为它创建了一个方法,请参阅-(void)pop:(UIButton *)sender
。
-(void)splitViewController: (UISplitViewController *)svc
willHideViewController: (UIViewController *)aViewController
withBarButtonItem: (UIBarButtonItem *)barButtonItem
forPopoverController: (UIPopoverController *)pc
// configure barButton
UIButton * button = [UIButton buttonWithType : UIButtonTypeCustom];
UIImage * image = [UIImage imageNamed:@ "barbutton.png"];
[button setImage :image forState: UIControlStateNormal];
button.frame = CGRectMake(0, 0, image.size.width, image.size.width);
[button addTarget: self
action: @selector(pop:)
forControlEvents: UIControlEventTouchUpInside];
barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
NSMutableArray * items = [[toolbar items] mutableCopy];
[items insertObject: barButtonItem atIndex: 0];
[toolbar setItems: items animated: YES];
[items release];
self.popoverController = pc;
[barButtonItem release];
-(void)pop: (UIButton *)sender
[self.popoverController presentPopoverFromRect: sender.frame
inView: self.view
permittedArrowDirections: UIPopoverArrowDirectionUp
animated: YES];
【讨论】:
以上是关于如何为 UIBarButtonItem 实现徽章?的主要内容,如果未能解决你的问题,请参考以下文章