栏按钮项添加工具栏程序(bar button item add toolbar programmat

2019-09-17 00:36发布

我要添加按钮,而不是栏按钮项到工具栏编程。 我选择它是因为按钮具有图像infodark defaut。

所以这是怎么做编程?

Answer 1:

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];


Answer 2:

要添加此按钮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];


文章来源: bar button item add toolbar programmatically