iphonexcodeuibuttonuitoolbaruitoolbaritem

bar button item add toolbar programmatically


I want to add button instead of bar button item into toolbar programmatically. I select it because button has image infodark defaut.

so how is this done programmatically?


Solution

  • UIImage* image = [UIImage imageNamed:defaultImage];
    CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
    UIButton* button = [[UIButton alloc] initWithFrame:frame];
    [button setTitle:@"Display" forState:UIControlStateNormal & UIControlStateHighlighted];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(button_Clicked:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];