我需要添加一个透明的工具栏,并在其上添加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];
答案 0 :(得分:1)
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];
答案 1 :(得分:0)
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];
注意,对于上述内容,您需要使用导航视图控制器上的工具栏。