1

是否可以添加UIButtonwith UIToolbar?或者我们只能使用UIBarButtonItemUIButton如果是这样如何添加一个UIToolbar

4

2 回答 2

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]];
于 2013-01-10T05:24:12.710 回答
0

你可以添加UIBarButtonItemUIToolBar下面的类似..

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

只需在 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];

检查此链接以获取有关UIBarButtonItemUIToolbar_Class的更多信息

更新:

对于您的此要求,您可以删除该按钮并添加新按钮而不是旧按钮..参见示例..

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

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

于 2013-01-10T05:20:26.180 回答