在 UI 视图中嵌入标签栏

Embedding a Tab Bar inside a UI View

我正在使用 SplitView 控制器。在我的 DetailView 中,我想实现类似于下面给出的图像的东西

在我的 DetailView Controller 中,我将整个视图划分为 3 个子视图。在我的中间子视图中,我想实现一个选项卡栏,以便在按下这三个选项卡项(错误、警告和状态)时加载不同的视图。

无论如何我都试过搜索来实现它,但失败了。任何关于如何实现它的指南将不胜感激。

编辑:

我发现我可以使用委托方法获取选项卡项的操作
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;

我通过以下方式在 DetailViewController.m 中实现了上述方法:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{

if (_myTabBar.selectedItem.tag == 1) {
    NSLog(@"TAB 1");
}

else if (_myTabBar.selectedItem.tag == 2) {

    NSLog(@"TAB2");

}

else if (_myTabBar.selectedItem.tag == 3)
{
    NSLog(@"TAB3");
}

else
{
    NSLog(@"TAB NOT WORKING");
}

}

但是上面的方法是行不通的。如果我遗漏了什么,请有人指导我。

提前致谢!

有多种方法可以实现您想要的效果,但我不建议您使用 TabBar 来实现。

  1. 你应该使用 UISegmentedControl。您可能无法完全按照您想要的方式为其设置外观,但这是实现您想要的功能的最简单方法。

  2. 使用 3 个不同的按钮,并根据需要将它们放在您的内部。在这种情况下,您需要自己手动管理选择(显示一个打开另一个关闭)、背景图像和其他详细信息。

无论哪种方式,标签栏可能都不是正确的选择。希望这可以帮助。谢谢

如果你真的想使用 UITabBar(但是,正如 Gurtej 所建议的那样,UISegmentedControl 似乎更合适),这里有一个例子:

// This is to calculate the proper position of each UILabel in your middle view (you don't really care about it)
static CGFloat labelYCurrentPosition;
static CGFloat labelYFirstPosition;
static CGFloat xMargin = 5.f;
static CGFloat labelXPosition;
static CGFloat labelWidth = 120.f;


- (void)viewDidLoad {
    [super viewDidLoad];

    self.middleView = [[UIView alloc] initWithFrame:CGRectMake(0, 150.f, self.view.frame.size.width, 250.f)];
    self.middleView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.middleView];

    UITabBar *tb = [[UITabBar alloc] initWithFrame:CGRectMake(0, 0, self.middleView.frame.size.width, 49)];
    tb.backgroundColor = [UIColor greenColor];
    UITabBarItem *tbi1 = [[UITabBarItem alloc] initWithTitle:@"Error" image:nil tag:0];
    UITabBarItem *tbi2 = [[UITabBarItem alloc] initWithTitle:@"Warning" image:nil tag:1];
    UITabBarItem *tbi3 = [[UITabBarItem alloc] initWithTitle:@"Status" image:nil tag:3];
    [tb setItems:@[tbi1, tbi2, tbi3]];
    [tb setDelegate:self];  // Delegate of your UITabBar is your detailViewController
    [self.middleView addSubview:tb];

    labelYFirstPosition = tb.center.y + tb.frame.size.height;
    labelYCurrentPosition = labelYFirstPosition;
    labelXPosition = xMargin;
}


- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{

    CGFloat labelHeight = 20.f;

    // This is to calculate x position of each label (you don't really care about it)
    if (labelYCurrentPosition + labelHeight > self.middleView.frame.size.height){
        labelXPosition += labelWidth;
        labelYCurrentPosition = labelYFirstPosition;
    }

    UILabel *lab = [[UILabel alloc]  initWithFrame:CGRectMake(labelXPosition, labelYCurrentPosition, labelWidth - 2 * xMargin, labelHeight)];
    lab.text = [NSString stringWithFormat:@"%@ with tag %ld", item.title, (long)item.tag];
    lab.font = [UIFont systemFontOfSize:12.f];
    [self.middleView addSubview:lab];
    labelYCurrentPosition += labelHeight;
}

您可以通过以下步骤实现上述图片功能:

  1. Make the UIVIEW with three or more buttons (title will be same as your button name).
  2. Give the tag value to each button i.e 1, 2, and 3 respectively
  3. Make IBOutlet as well as action method of UIButton
  4. When user press the button 1 (Errors) then make view1 visible else view2 and view3 as hidden
  5. When user press the button 2 (Warning) then make view2 visible else view1 and view3 as hidden
  6. Same goes for other button.

如有任何疑问,请告诉我。