iPhone sdk如何在UIToolbar中创建UIButton?

iPhone sdk如何在UIToolbar中创建UIButton?,iphone,uibutton,uibarbuttonitem,uitoolbar,Iphone,Uibutton,Uibarbuttonitem,Uitoolbar,是否可以使用UIToolbar添加ui按钮?或者我们只能使用uibarbuttonim?如果是,如何在UIToolbar中添加ui按钮?您可以向UIToolbar添加UIBarButtonItem,如下所示 下面是一个例子,试试这个 只需在viewDidLoad方法中添加下面的代码,然后使用这两个UIBarButtonims查看UIToolBar的 UIBarButtonItem *flexiableItem = [[UIBarButtonItem alloc] initWithBarButto

是否可以使用
UIToolbar
添加
ui按钮
?或者我们只能使用
uibarbuttonim
?如果是,如何在
UIToolbar
中添加
ui按钮?

您可以向
UIToolbar
添加
UIBarButtonItem
,如下所示

下面是一个例子,试试这个

只需在viewDidLoad方法中添加下面的代码,然后使用这两个
UIBarButtonims
查看
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];
有关和的更多信息,请查看此链接

更新:

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

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

我希望这对您有所帮助…

您可以像下面那样将
UIBarButtonItem
添加到
UIToolBar

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]];
下面是一个例子,试试这个

只需在viewDidLoad方法中添加下面的代码,然后使用这两个
UIBarButtonims
查看
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];
有关和的更多信息,请查看此链接

更新:

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

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

我希望这能帮助你……

看看吧。。。也许对你有帮助。。不,那是给UILabel的…我就知道。如何使用UIToolbar添加UIButton?请查看。。。也许对你有帮助。。不,那是给UILabel的…我就知道。如何使用UIToolbar添加UIButton?目前我只使用BarButtonim。但我想隐藏几个按钮,并在某些情况下在相同位置添加新按钮。因为我们不能隐藏巴布通主义(只有我们可以启用/禁用)。我击中了。这就是为什么我要按UIButton。@Saranya看到更新的答案了。。在这里,您需要定义按钮和通行费全局,并在需要时删除和添加,如上文所述。。希望现在它对您有用:)也许他想要自定义按钮只是为了完成它,伙计:)目前我只使用巴布托石。但我想隐藏几个按钮,并在某些情况下在相同位置添加新按钮。因为我们不能隐藏巴布通主义(只有我们可以启用/禁用)。我击中了。这就是为什么我要按UIButton。@Saranya看到更新的答案了。。在这里,您需要定义按钮和通行费全局,并在需要时删除和添加,如上文所述。。希望现在它对您有用:)也许他想要自定义按钮只是为了完成它,伙计:)
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]];