1

嗨要从 UINavigation 栏中删除子视图,我正在使用以下代码。我在导航栏中将标签、按钮和 imageVIew 作为子视图。

   for (UIView *view in self.navigationController.navigationBar.subviews) {


    [view removeFromSuperview];
}

当我运行它时,它正在删除我添加为的导航栏的背景图像

     [self.navigationController.navigationBar setBackgroundImage:[UIImagem imageNamed:@"header-background"] forBarMetrics:UIBarMetricsDefault];

after removing subViews i am adding the background agin,But it is not adding.

Is there any way to remove only subViews of navigation bar without removing the background.

4

2 回答 2

4

一个快速的选项是向tag要删除的视图的属性添加一个值,并在删除子视图之前检查它,例如,假设您向子视图添加一个非零值:

for (UIView *view in self.navigationController.navigationBar.subviews) {
    if (view.tag != 0) {
        [view removeFromSuperview];
    }
}
于 2013-02-12T09:11:49.917 回答
1

尝试这个,

for (UIView *view in self.navigationController.navigationBar.subviews) {

if([view isKindOfClass:[UIImageView class]])
{
  //change your bar image
 }
else
 {
[view removeFromSuperview];
 }
 }
于 2013-02-12T09:19:02.210 回答