Adding a UILabel to a UIToolbar

前端 未结 8 1720
青春惊慌失措
青春惊慌失措 2020-11-27 10:24

I\'m trying to add a label to my toolbar. Button works great, however when I add the label object, it crashes. Any ideas?

UIBarButtonItem *setDateRangeButton         


        
8条回答
  •  猫巷女王i
    2020-11-27 11:04

    One of the things I'm using this trick for is to instantiate a UIActivityIndicatorView on top of the UIToolBar, something that otherwise wouldn't be possible. For instance here I have a UIToolBar with 2 UIBarButtonItem, a FlexibleSpaceBarButtonItem, and then another UIBarButtonItem. I want to insert a UIActivityIndicatorView into the UIToolBar between the flexible space and the final (right-hand) button. So in my RootViewController I do the following,

    - (void)viewDidLoad {
    [super viewDidLoad];// Add an invisible UIActivityViewIndicator to the toolbar
    UIToolbar *toolbar = (UIToolbar *)[self.view viewWithTag:767];
    NSArray *items = [toolbar items];
    
    activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 20.0f, 20.0f)];
    [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];    
    
    NSArray *newItems = [NSArray arrayWithObjects:[items objectAtIndex:0],[items objectAtIndex:1],[items objectAtIndex:2],
                         [[UIBarButtonItem alloc] initWithCustomView:activityIndicator], [items objectAtIndex:3],nil];
    [toolbar setItems:newItems];}
    

提交回复
热议问题