我想以编程方式将按钮而非条形按钮项添加到工具栏中。我选择它是因为按钮有图像infodark defaut。
那么如何以编程方式完成?
答案 0 :(得分:7)
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];
答案 1 :(得分:0)
要在UIToolbar
中添加此按钮,您需要此代码。
首先,您可以在此之后初始化UIToolbar
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:4];
[buttons addObject: barButtonItem];
[buttons addObject: btnFav];
[buttons addObject: btnGeneralPopOverController];
[buttons addObject: showInfo];
[mytoolbar setItems:buttons animated:NO];