2011-11-02 49 views
2

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

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

如何设置这些项目?是否可以使用接口生成器,还是需要对它们进行硬编码?

+0

它使用IB原因很明显是不可能的。您需要将对象添加到UIToolbar,将它们设置为null(隐藏它们时),并在您想再次显示时将它们重新分配给UIToolbar。 – Legolas

回答

1

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

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

它也将动画的改变,所以你可能不需要隐藏,并再次显示它,这不是一般的好UI做法。

+0

我使用这种方法,我可以删除工具栏项目,但无法读取它们。为什么是这样? – blake305

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财产。让我知道你是否需要进一步的帮助或解释。

+0

我将如何将新的工具栏放入视图中? – Baub

0

奇数一个有点...这是一个有点哈克,但应该是完美的罚款:

[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; 
    }]; 
}];