是否有可能添加UIButton
与UIToolbar
? 或者我们可以只使用UIBarButtonItem
? 如果是的话如何添加UIButton
在UIToolbar
?
Answer 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]];
Answer 2:
您可以添加UIBarButtonItem
到UIToolBar
像波纹管..
这波纹管是一个例子试试这个..
就在viewDidLoad方法添加此娄代码,然后看到UIToolBar
与这两UIBarButtonItems
也是在这里我补充Flixiblespace的设定权和左角这两个按钮UIToolBar
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];
检查此链接,了解更多信息的UIBarButtonItem和UIToolbar_Class
更新:
对于您的这个要求,你可以删除按钮,并添加新的按钮,而不是老..见的例子..
NSMutableArray *items = [[yourToolbar.items mutableCopy] autorelease];
[items removeObject: yourButtons];
yourToolbar.items = items;
我希望这可以帮助您...
文章来源: iPhone sdk how to UIButton in UIToolbar?