iPhone sdk如何在UIToolbar中使用UIButton?

时间:2013-01-10 05:14:56

标签: iphone uibutton uibarbuttonitem uitoolbar

是否可以使用UIButton添加UIToolbar?或者我们只能使用UIBarButtonItem?如果是这样,如何在UIButton中添加UIToolbar

2 个答案:

答案 0 :(得分:1)

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barBackButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
[toolBar setItems:[NSArray arrayWithObject:barBackButton]];

答案 1 :(得分:0)

您可以UIBarButtonItem添加UIToolBar,如下所示..

这个波纹管是一个例子试试这个..

只需在viewDidLoad方法中添加此下方代码,然后查看UIToolBar这两个UIBarButtonItems此处我还添加了Flixiblespace,用于设置UIToolBar <左右角的这两个按钮/ p>

UIBarButtonItem *flexiableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];

UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSArray *items = [NSArray arrayWithObjects:item1, flexiableItem, item2, nil];   
self.toolbarItems = items;

[flexiableItem release];
[items release];
[self.view addSubview:toolbar];
[toolbar release];

有关UIBarButtonItemUIToolbar_Class

的详情,请查看此链接

<强>更新

对于您的此要求,您可以删除这些按钮并添加新按钮而不是旧按钮。请参阅示例..

NSMutableArray     *items = [[yourToolbar.items mutableCopy] autorelease];
[items removeObject: yourButtons];
yourToolbar.items = items;

我希望这可以帮助你...