![](/img/trans.png)
[英]Adding a Navigation Controller to a Tab Bar Application (Programmatically)
[英]Adding custom view above tab bar controller/navigation controller?
我尝试了以下代码,试图让自定义视图显示在选项卡栏控制器(它的所有选项卡中恰好有一个导航控制器)上方。
问题是它覆盖在导航栏的顶部,我希望导航栏向下移动。
我尝试设置标签栏控制器的框架,但这根本没有移动它。
- (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;
}
有任何想法吗?
将视图添加到 tabBarController 视图本身:
[self.tabBarController.view addSubview:yourView];
你可以简单的 addSubview 到窗口:
[self.view.window addSubview:myView];
真的没有什么好方法可以做到这一点。 UINavigationController 和 UITabBarController 的各种子视图都是私有的,试图弄乱它们很可能无法正常工作。 并且 Apple 没有给我们创建“容器”视图控制器的工具,因此您无法轻松地将 UINavigationController/UITabBarController 嵌入另一个视图控制器中或自己重新创建 UINavigationController/UITabBarController。
您最好的选择可能是继续尝试创建自己的“容器”视图控制器,并处理一些无法正常工作的事情。 特别是,包含的视图控制器的parentViewController
将返回 nil,因此包含的视图控制器或其子控制器上的各种其他东西将被破坏(例如interfaceOrientation
属性将是错误的, presentModalViewController:animated:
可能无法正常工作)。 其他东西也可能被破坏。
或者你可以等到 iOS 的某个未来版本真正支持我们创建容器视图控制器(如果有的话),然后只支持该版本及更高版本。
我遇到了同样的问题,最终做了这样的事情:
newView.frame = tabBarController.tabBar.frame
tabBarController.view.addSubview(newView)
将视图添加到 tabBarController 并使用当前标签栏的框架作为新视图的位置就可以了。
你可以这样做:
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)
}
对于那些希望添加视图的人,或者在我的情况下是选项卡栏视图上的指示器视图,这里是 GitHub 上的代码片段,实现起来非常简单。
预习
Github 要点
https://gist.github.com/egzonpllana/cc5538f388d8a530e7c393e7344e57a5
你的意思是在其他内容的垂直上方?
或高于坐在其余内容的顶部?
有 presentModalViewController:animated: 但我怀疑那是你想要的吗?
我无法发表评论,所以我会在这里为遇到此页面的任何其他人写这篇文章。 Borut 被否决的答案实际上是更简单、更正确的答案。
他只是没有为您提供正确的方向。 您需要将自定义视图添加到您的 AppDelegate 中声明的应用程序的窗口中(在 Mono 中,这将是 AppDelegate.Window.Add(myView),而不是在当前视图的窗口中,该窗口为空(例如 this.View.Window )。
这在 Xamarin/Mono 中工作正常,我不明白为什么在正确的代码中 Obj-C 会不一样。
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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.