如何让单个 View (UIButton) 在 UITabViewController 的所有选项卡中可见?
例如,我需要在所有选项卡中保持单个“信息”按钮可见,而不是将其添加到所有选项卡的 XIB 中,并且不重写所有选项卡中的重复代码。
最佳答案
实现一个选项卡栏 Controller 类,并在 viewDidLoad
方法中循环遍历所有选项卡栏 View Controller 并以编程方式添加按钮。
- (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];
}
}
关于iphone - 在所有选项卡中显示单个 View (UITabBarController),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17748899/