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

发布于 2024-11-26 12:58:19 字数 959 浏览 2 评论 0原文

我尝试了以下代码,试图获取显示在选项卡栏控制器上方的自定义视图(该控制器恰好在所有选项卡中都有一个导航控制器)。

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

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

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

有什么想法吗?

I tried the following code, in an attempt to get the custom view displaying above the tab bar controller (which happens to have a navigation controller within all of it's tabs).

The problem is that it overlays on top of the navigation bar, and I want the navigation bar to be moved down.

I tried setting the frame of the tab bar controller, but that didn't move it at all.

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

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

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

[self.tabBarController.view addSubview:yourView];

Add the view to the tabBarController View itself:

[self.tabBarController.view addSubview:yourView];
热情消退 2024-12-03 12:58:19

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

[self.view.window addSubview:myView];

You could simple addSubview to window:

[self.view.window addSubview:myView];
送君千里 2024-12-03 12:58:19

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

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

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

There isn't really any good way to do this. The various subviews of UINavigationController and UITabBarController are both private, and trying to mess with them is likely to not work correctly. And Apple doesn't give us the tools to create "container" view controllers, so you can't easily embed the UINavigationController/UITabBarController inside another view controller or recreate UINavigationController/UITabBarController yourself.

Your best bet is probably to go ahead and try to create your own "container" view controller, and deal with some things not working right. In particular, the contained view controller's parentViewController will return nil, and therefore various other things on the contained view controller or its sub-controllers will be broken (e.g. the interfaceOrientation property will be wrong, presentModalViewController:animated: might not work right). Other things may be broken too.

Or you could wait until some future version of iOS actually has support for us to create container view controllers (if ever), and then support only that version and up.

一杯敬自由 2024-12-03 12:58:19

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

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

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

I had the same problem and ended up doing something like this:

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

Adding the view to the tabBarController and using the frame of the current tabbar as the position of the new view did the trick.

葵雨 2024-12-03 12:58:19

对于那些想要添加视图(或者在我的例子中是选项卡栏视图上的指示器视图)的人,这里是 GitHub 上的代码片段,非常容易实现

预览

在此处输入图像描述

Github 要点

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

For those who are looking to add a view, or in my case an indicator view over the tab bar view, here is the code snipet on GitHub with very easy implementation.

Preview

enter image description here

Github gist

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

妖妓 2024-12-03 12:58:19

你可以这样做:

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)
}

You can do something like that:

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)
}
盛装女皇 2024-12-03 12:58:19

您的意思是上面,即垂直位于其余内容之上吗?

或者位于其余内容之上?

有 presentModalViewController:animated: 但我怀疑这就是你想要的?

Do you mean above as in vertically above the rest of the contents?

Or above as in sitting on-top of the rest of the content?

There's presentModalViewController:animated: but I doubt that's what you want?

ゞ记忆︶ㄣ 2024-12-03 12:58:19
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)
  }
}
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)
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文