Reputation: 189
I am adding a view to the navigation bar
UIView *mySubView = [UIView alloc] initwithFrame:frame];
[self.navigationController.navigationBar addSubview:mySubView];
I want to remove the view before pushing to secondviewController.
[mySubView removeFromSuperView];
When App launch first time it did not remove the view, so view also visible on secondview navigation bar
I searched and tried many approaches, but didn't find any solution.
Upvotes: 0
Views: 3378
Reputation: 487
-(void)viewWillDisappear:(BOOL)animated
{
[self.mySubView removeFromSuperview];
//[_mySubView removeFromSuperview];
}
Upvotes: 0
Reputation: 4208
Do following for the ViewController
in which you are having subView for NavigationBar
in .h
@property (nonatomic, retain) UIView *mySubView;
in .m
- (void)viewDidLoad {
//Your Existing Code + following 2 lines
self.mySubView = [[UIView alloc] initWithFrame:yourFrame];
self.mySubView.backgroundColor = [UIColor whiteColor];
}
- (void)viewWillAppear:(BOOL)animated {
//Your Existing Code + following 1 line
[self.navigationController.navigationBar addSubview:self.mySubView];
}
- (void)viewWillDisappear:(BOOL)animated {
//Your Existing Code + following 1 line
[self.mySubView removeFromSuperview];
}
And you are good to go. Tried and tested.
Upvotes: 0
Reputation: 324
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 it will help !!!!
Upvotes: 2
Reputation: 9913
Assign a tag to you mysubview like
UIView *mySubView = [UIView alloc] initwithFrame:frame];
mySubView.tag =1;
[self.navigationController.navigationBar addSubview:mySubView];
Then add this line when you push to secondViewController.
[[self.navigationController.navigationBar viewWithTag:1] removeFromSuperview];
Hope it helps you.
Upvotes: 4