简体   繁体   中英

Removing subViews from uinavigationbar

Hi To remove SubViews from UINavigation bar i am using following code.I have label,button and imageVIew as subView in navigation bar.

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


    [view removeFromSuperview];
}

While i am running this it is removing the backGround image of the navigationBar which i added as

     [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.

a fast option is to add a value to the tag property of the views you want to remove and check for it before removing the the subview, for example, assuming that you add a non-zero value to your subviews:

for (UIView *view in self.navigationController.navigationBar.subviews) {
    if (view.tag != 0) {
        [view removeFromSuperview];
    }
}

Try this,

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

if([view isKindOfClass:[UIImageView class]])
{
  //change your bar image
 }
else
 {
[view removeFromSuperview];
 }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM