NavigationController UIToolbar更改项目

时间:2011-11-02 20:54:43

标签: iphone objective-c xcode uinavigationcontroller uitoolbar

我想通过隐藏工具栏,更改项目(按钮,固定空间等)并再次显示来更改UIToolbar中的项目。

我目前在UIToolbar上有一个按钮,当按下该按钮时,会通过调用[[self navigationController]setToolbarHidden:YES animated:YES];来隐藏工具栏。

如何设置这些项目?是否可以使用界面构建器或我是否需要对它们进行硬编码?

3 个答案:

答案 0 :(得分:2)

这是非标准行为,但应该是可行的。您可以考虑使用而不是删除现有工具栏中的新按钮并添加新按钮,而是创建一个不同的工具栏。这将使代码/调试更容易。一般来说,它只需要更少的“混乱”。

要实现所需的行为,您可以执行以下操作:

float animationDuration = .25;

[UIView animateWithDuration:animationDuration animations:*{
    // Remove the old toolbar.
    self.oldToolbar.alpha = 0;

    // Fade the new toolbar in.
    self.newToolbar.alpha = 1;
}];

此示例假定您已将其他工具栏加载到newToolbar属性中。如果您需要进一步的帮助或任何解释,请与我们联系。

答案 1 :(得分:1)

您可以这样设置工具栏的新项目:

[toolbar setItems:<new_items_array> animated:YES];

它还会为更改设置动画,因此您可能不需要隐藏并再次显示它,这通常不是一个好的UI练习。

答案 2 :(得分:0)

有点奇怪......这有点hacky但应该完全没问题:

[UIView animateWithDuration:0.5f animations:^{
    // Remove the old toolbar.
    self.oldToolbar.alpha = 0;

} completion:^(BOOL finished) {
    //add code to change toolbar.
    [UIView animateWithDuration:0.5f animations:^{
        // Fade the new toolbar in.
        self.newToolbar.alpha = 1;
    }];
}];
相关问题