How to implement a Badge for a UIBarButtonItem?
I want to pu a badge on the UIBarButtonItem that controls popover in a SplitView
This code did do nothing:
- (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;
}
while the following attempt throws an error:
- (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.popoverControl开发者_运维百科ler = pc;
}
Error: 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.'
How can I add a badge to an UIBarButtonItem?
This is how i did it, instead of redirecting the target and action, we created a method for it, see
-(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];
}
精彩评论