![](/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.