iphone - 添加按钮到工具栏

标签 iphone objective-c ios xcode

我需要添加一个透明的工具栏并添加 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/

相关文章:

iphone sdk如何捕捉1秒后的长按?

ios - 以编程方式锁定和解锁 iPhone(未越狱)屏幕?

ios - UIView 透明度会影响应用程序的性能吗?

iphone - 非导出 iPhone 应用程序需要 CCATS 吗?

iphone - 创建自定义 UITableView

objective-c - 如何获取有关媒体文件的特定信息(持续时间、比特率、FPS 等)

iphone - TTCatalog(Three20)不会运行

ios - 无法从 iPhone 应用程序读取 WatchKit 设置包

ios - UICollectionView 与 UIImageViews 的糟糕表现与核心图像操作图像

ios - 如何在 ios 中使用谷歌地图实现位置搜索?