bar button item add toolbar programmatically

孤人 提交于 2019-12-11 02:46:28

问题


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?


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



回答2:


To add this button in UIToolbar you need this code. first you can initialize UIToolbar after that

NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:4];

[buttons addObject: barButtonItem];
[buttons addObject: btnFav];
[buttons addObject: btnGeneralPopOverController];
[buttons addObject: showInfo];   

[mytoolbar setItems:buttons animated:NO];


来源:https://stackoverflow.com/questions/10444104/bar-button-item-add-toolbar-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!