2

我想以编程方式将按钮而不是条形按钮项添加到工具栏中。我选择它是因为按钮有图像 infodark defaut。

那么这是如何以编程方式完成的呢?

4

2 回答 2

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];
于 2012-05-04T07:14:39.153 回答
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];
于 2012-05-04T13:04:51.903 回答