我需要添加一个透明的工具栏并添加 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];
关于iphone - 添加按钮到工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8606910/