【问题标题】:Navigation Bar That stays over the TabBarController位于 TabBarController 上方的导航栏
【发布时间】:2012-07-01 02:45:02
【问题描述】:

我希望有一个带有设置按钮的导航栏,无论它在哪个选项卡上,它都保持在那里。

抱歉,短信来晚了,

追逐

【问题讨论】:

    标签: objective-c uitabbarcontroller uinavigationbar


    【解决方案1】:

    您可以将 UIToolBar 拖到情节提要控制器上,然后在其中设置所需的按钮和操作。你可以让你的所有控制器都从同一个基类扩展,这样就不必重复你的代码,如果你觉得这个工具栏更令人愉悦,也可以在代码中实例化这个工具栏。

    问题在于使用导航时,导航栏将替换/重叠此工具栏。

    UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
    
    UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"Title" 
                                                                 style:UIBarButtonItemStyleBordered 
                                                                target:self
                                                                action:@selector(myAction:)];
    
    
    [myToolbar setItems:[NSArray arrayWithObjects:myButton, nil]];
    
    [self.view addSubview:myToolbar];
    

    【讨论】:

    • 我不需要使用导航。我只是想要一个地方来放置看起来不错的设置按钮,并且可以从任何选项卡访问。
    • 那么在这种情况下,我提出的解决方案不会有问题,试试看告诉我。
    • 我用一些代码编辑了我的答案,同样可以简单地在界面生成器中完成并通过控制拖动将其连接起来
    【解决方案2】:

    在 AppDidFinishLaunching 中:

    Add Your TabBarcontroller (rootViewController) for UINavigationController
    

    现在在下面添加设置按钮是导航栏中的示例

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
    style:UIBarButtonSystemItemDone target:nil action:nil];
    
    navigationCOntroller.navigationItem.rightBarButtonItem = rightButton 
    

    rightButton 将通过应用程序可见

    【讨论】:

      【解决方案3】:

      如果您只想放置一个带有设置按钮的导航栏,可以使用 UITabBarController 从任何视图访问,您可以在 AppDelegate 中执行类似的操作 -->

      YourViewController1 *yourVC1 = [YourViewController1 alloc]  initWithNibName:@"YourViewController1" bundle:nil];
      UINavigationController *yourNVC1 = [[UINavigationController alloc] initWithRootViewController:yourVC1];
      
      YourViewController2 *yourVC2 = [YourViewController2 alloc]  initWithNibName:@"YourViewController2" bundle:nil];
      UINavigationController *yourNVC2 = [[UINavigationController alloc] initWithRootViewController:yourVC2];
      
      UITabBarController *tabBC = [[UITabBarController alloc] init];
      [tabBC setViewControllers:[NSArray arrayWithObjects:yourNVC1, yourNVC2, nil]];
      
      self.window.rootViewController = tabBC;
      

      并且您应该在所有视图上都有一个带有导航栏的标签栏。现在要将设置按钮放置在导航栏中,将其添加到视图控制器中要显示设置按钮的 viewDidLoad 方法中 -->

      UIBarButtonItem *selectBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Settings" style:UIBarButtonItemStyleBordered target:self action:@selector(selectorName:)];
      self.navigationItem.rightBarButtonItem = selectBarButton;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-17
        • 1970-01-01
        • 1970-01-01
        • 2014-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多