在所有选项卡中都有一个可见的视图(UITabBarController) [英] Having a single view visible in all tabs (UITabBarController)

查看:46
本文介绍了在所有选项卡中都有一个可见的视图(UITabBarController)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在UITabViewController的所有选项卡中显示单个视图(UIButton)?

How can I have a single view (UIButton) visible in all tabs of a UITabViewController?

例如,我需要在所有选项卡中保持单个信息"按钮可见,而不是将其添加到所有选项卡的XIB中,并且不重写所有选项卡中的重复代码.

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.

推荐答案

实现选项卡栏控制器类,并在 viewDidLoad 方法中,遍历所有选项卡栏视图控制器并以编程方式添加按钮.

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];
    }
}

这篇关于在所有选项卡中都有一个可见的视图(UITabBarController)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆