将标签栏移到屏幕顶部

我有一个标签栏控制器,正如我们所知,它在屏幕底部显示标签栏。 我正在寻找一种方法将其移到顶端。 我不认为我可以使用一个简单的UITabBar这个,因为我需要在它下面嵌套UINavigationControllers

有没有办法将UITabBarController的标签栏移动到屏幕的顶部?

在你的UITabBarController的 “viewDidLayoutSubviews”方法中试试这个代码

Swift 2.X

  self.tabbar.frame = CGRectMake(0,0,320,50) //example for iPhone 5 

Swift 3.X

  self.tabbar.frame = CGRect(0,0,320,50) //example for iPhone 5 

Swift 4.X

  self.tabbar.frame = CGRect( x: 0, y: 0, width: 320, height: 50) //example for iPhone 5 

(在Swift中)

在TabBarViewController.swift文件中(每个人都按照自己的意愿命名该文件):

  • 首先:创build一个标签栏的IBOutlet,然后将其连接到故事板或笔尖文件中的相应标签栏。

     @IBOutlet var profileTabBar : UITabBar! 
  • 第二:在viewDidLoad()函数中添加这个代码,将标签栏放置在你想要的位置(在这种情况下,我在导航控制器下添加了de标签栏)。 修改CGRectMake初始值设定项的位置变化xy

     // [Maybe you don't have a navigation controller] yNavBar indicates the height of the navigation bar. var yNavBar = self.navigationController?.navigationBar.frame.size.height // yStatusBar indicates the height of the status bar var yStatusBar = UIApplication.sharedApplication().statusBarFrame.size.height // Set the size and the position in the screen of the tab bar profileTabBar.frame = CGRectMake(0, yNavBar! + yStatusBar + profileTabBar.frame.size.height, profileTabBar.frame.size.width, profileTabBar.frame.size.height) 

我不这么认为。 我能想到的唯一的事情就是使用UITabBar而不是UITabbarController …

无论如何,这可能是一个更好的select,如果你正在考虑为每个视图嵌套UINavigationControllers为不同的标签加载。

我使用Tab Bar Controller然后我简单地改变ViewDidLoad()中的Tab Bar Controller中的TabBar位置

 import UIKit class TabbarViewController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() self.tabBar.frame = CGRect(origin: CGPoint(x: 0,y :64), size: CGSize(width: 400, height: 50)) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

这里是所需结果的屏幕短附加… 标签栏顶部的标签栏视图conmtroller

注意: 在swift 3中,你可以自己修改语法swift 2. *。