Reputation: 19204
How can I have a single view (UIButton) visible in all tabs of a UITabViewController?
For example I need to keep a single "info" button visible in all tabs, and not add it to XIBs of all tabs and not rewrite repeated code in all tabs.
Upvotes: 2
Views: 266
Reputation: 968
Implement a Tab Bar Controller class and in the viewDidLoad
method, loop through all the Tab Bar View Controllers and programmatically add your button.
- (void)viewDidLoad
{
[super viewDidLoad];
for(UIViewController *viewController in [self viewControllers]){
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
CGRect buttonRect = infoButton.frame;
// This will place the button 20px away from the top right corner
buttonRect.origin.x = self.view.frame.size.width - buttonRect.size.width - 20;
buttonRect.origin.y = 20;
[infoButton setFrame:buttonRect];
[infoButton addTarget:self action:@selector(showInfo:) forControlEvents:UIControlEventTouchUpInside];
[viewController.view addSubview:infoButton];
}
}
Upvotes: 1
Reputation: 3197
I have 2 solutions for you:
Save the button (or rather a custom UIView with buttons) as a shared instance (save persistently). You could then access it from every viewController. Every viewController (each tab) will have a UIView in the same size, that will be set each time to that buttons View. (or if you choose add it as a subview). viewWillAppear: would be a wise choice to do that.
If the tabBar is a custom one, you can add delegate methods, that will be called every time you are trying to display a different one. you can then pass the buttons view there to the next viewController. the downsize in this option is that the next viewcontroller need to be initialized before the setting of the buttons.
Upvotes: 1