在标签栏控制器/导航控制器上方添加自定义视图

时间:2011-07-25 19:47:27

标签: iphone uinavigationcontroller uitabbarcontroller

我尝试了以下代码,试图让自定义视图显示在标签栏控制器上方(恰好在其所有选项卡中都有导航控制器)。

问题是它覆盖在导航栏的顶部,我希望导航栏向下移动。

我尝试设置标签栏控制器的框架,但根本没有移动它。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    // Override point for customization after application launch.
    // Add the tab bar controller's current view as a subview of the window
    //self.tabBarController.view.frame = CGRectMake(0, 62, 320, 320);
    self.window.rootViewController = self.tabBarController;

    [self.window makeKeyAndVisible];

    // setting up the header view
    self.headerView = [[HeaderView alloc] initWithFrame:CGRectMake(0, 20, 320, 42)];
    [self.window addSubview:self.headerView];

    // setting up facebook stuff
    AgentSingleton *agentSingleton = [AgentSingleton sharedSingleton];
    agentSingleton.facebook = [[Facebook alloc] initWithAppId:APP_ID];

    return YES;
}

有什么想法吗?

9 个答案:

答案 0 :(得分:28)

将视图添加到tabBarController视图本身:

[self.tabBarController.view addSubview:yourView];

答案 1 :(得分:8)

您可以简单地将addSubview添加到窗口:

[self.view.window addSubview:myView];

答案 2 :(得分:7)

没有任何好方法可以做到这一点。 UINavigationController和UITabBarController的各个子视图都是私有的,并且试图弄乱它们可能无法正常工作。 Apple没有为我们提供创建“容器”视图控制器的工具,因此您无法轻松地将UINavigationController / UITabBarController嵌入到另一个视图控制器中或自己重新创建UINavigationController / UITabBarController。

你最好的选择可能是继续尝试创建自己的“容器”视图控制器,并处理一些不正常的事情。特别是,包含的视图控制器的parentViewController将返回nil,因此包含的视图控制器或其子控制器上的各种其他内容将被破坏(例如,interfaceOrientation属性将是错误的,{{1可能无法正常工作)。其他事情也可能被打破。

或者您可以等到iOS的某个未来版本实际上支持我们创建容器视图控制器(如果有的话),然后仅支持该版本。

答案 3 :(得分:2)

你可以这样做:

if let indeedTabBarController = self.tabBarController {

    let buttonHeight: CGFloat = 49 // height of tab bar
    let buttonWidth = UIScreen.main.bounds.width / 3 // in case if you have 3 tabs
    let frame = CGRect(x: 0, y: UIScreen.main.bounds.height - buttonHeight, width: buttonWidth, height: buttonHeight)
    let button = UIButton(frame: frame)
    button.addTarget(self, action: #selector(self.someAction), for: .touchUpInside)

    indeedTabBarController.view.addSubview(button)
}

答案 4 :(得分:1)

对于那些想要添加视图的人,或者在我看来是选项卡视图上方的指示器视图的人,这是GitHub上的代码片段,具有非常容易实现

预览

enter image description here

Github要点

https://gist.github.com/egzonpllana/cc5538f388d8a530e7c393e7344e57a5

答案 5 :(得分:0)

你的意思是上面是在垂直于其他内容的上方吗?

或者高于其他内容的坐在上面?

现在有一个模态视频控制器:动画:但我怀疑这是你想要的吗?

答案 6 :(得分:0)

我无法发表评论,所以我会在这里为其他遇到此页面的人写这篇文章。被投票的Borut的答案实际上是更容易和更正确的答案。

他根本没有为您提供正确的指示。您需要将自定义视图添加到您的应用程序的窗口中,该窗口在AppDelegate中声明(在Mono中,这将是AppDelegate.Window.Add(myView),不是在当前视图中窗口为null(例如this.View.Window)。

这在Xamarin / Mono中运行良好,我无法理解为什么在正确的代码中Obj-C不会相同。

答案 7 :(得分:0)

我遇到了同样的问题,最终做了这样的事情:

self.newView.frame = self.tabBarController.tabBar.frame
self.tabBarController.view.addSubview(self.newView)

将视图添加到tabBarController并使用当前选项卡的框架作为新视图的位置即可。

答案 8 :(得分:0)

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let indeedTabBarController = self.tabBarController {
    let buttonHeight: CGFloat = view.safeAreaInsets.bottom + 44
    let buttonWidth = UIScreen.main.bounds.width
    let frame = CGRect(x: 0, y: UIScreen.main.bounds.height - buttonHeight, width: buttonWidth, height: 44)
    let vw = UIView(frame: frame)
    vw.backgroundColor = .red
    indeedTabBarController.view.addSubview(vw)
  }
}