Search code examples
iphoneuitoolbarbadge

iphone IOS Anyway to get Added Control in ToolBar to come to front


This is a bit of a hack, but trying to get a badge on a toolbaritem. Almost there, but it appears in the back, tried normal methods to get to front with no luck.

Behind a toolbar

deletedCountBadge = [CustomBadge customBadgeWithString:@"0"];
deletedCountBadge.frame = CGRectMake(100,10,25,25);

UIView *view = (UIView *)[bottomToolBar.subviews objectAtIndex:0];
[view addSubview:deletedCountBadge];

Solution

  • Got it to work with a couple of different pointers on the net.

    1) If you are moving the toolbar around at all (.frame=) make sure to set the items after the layout

    2) Can use negative spacing items to get it to show up where you want.

    enter image description here

    UIBarButtonItem *deleteBadge = [BarButtonItemBadge barButtonItemBadge:@"1" insideColor:[UIColor blueColor]];
    
    UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSeperator.width = -30;
    
    [bottomToolBarDictionaryButtonItems addObject:deleteBadge];
    [bottomToolBarDictionaryButtonItems addObject:negativeSeperator];
    
    // after any layout, of tool bar
    
    [bottomToolBar setItems:bottomToolBarDictionaryButtonItems];