是否可以使用UIButton
添加UIToolbar
?或者我们只能使用UIBarButtonItem
?如果是这样,如何在UIButton
中添加UIToolbar
?
答案 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];
有关UIBarButtonItem和UIToolbar_Class
的详情,请查看此链接<强>更新强>
对于您的此要求,您可以删除这些按钮并添加新按钮而不是旧按钮。请参阅示例..
NSMutableArray *items = [[yourToolbar.items mutableCopy] autorelease];
[items removeObject: yourButtons];
yourToolbar.items = items;
我希望这可以帮助你...