繁体   English   中英

向工具栏添加按钮

[英]Adding buttons to toolbar

我需要添加一个透明的工具栏,并添加4个带有图像的按钮。 我该如何通过代码做到这一点?

到目前为止我的工作;

我已经添加了工具栏并使其透明,现在我正尝试添加4个带有图像的按钮。 我怎样才能做到这一点 ? (这4个按钮也应具有action方法,因此当有人单击该按钮时,应触发操作)

toolBar1 = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 320 , 320 , 60)];

[toolBar1 setBarStyle:UIBarStyleBlack];

[toolBar1 setTranslucent:YES];

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

UIBarButtonItem *buttonWithImage = [[UIBarButtonItem alloc] ...... // Now what ??

[self.view addSubview:toolBar1];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleBordered target:self action:@selector(action];

UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"Item1" style:UIBarButtonItemStyleBordered target:self action:@selector(action];

NSArray *buttons = [NSArray arrayWithObjects: item1, item2, nil];
[toolBar setItems: buttons animated:NO];

[item1 release];
[item2 release];

只需为4个按钮制作以上代码

更新1:

使用以下代码获取按钮中的图像

UIImageView *btn1Img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Btn1Img"]];

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:btn1Img];
NSMutableArray *buttonsArray = [[NSMutableArray alloc] init];
UIBarButtonItem *myButton1=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"email.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toolbarButtonPressed1:)];
[buttonsArray addObject:myButton1];
UIBarButtonItem *myButton2 = [[UIBarButtonItem alloc] initWithTitle:@"button 2" style:UIBarButtonItemStylePlain target:self action:@selector(toolbarButtonPressed2:)];  
[buttonsArray addObject:myButton2];
[self setToolbarItems:buttonsArray animated:YES];

请注意,对于上述内容,您将需要使用导航视图控制器上的工具栏。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM