ios 将标签栏移动到屏幕顶部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6684425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:44:33  来源:igfitidea点击:

Move Tab Bar to top of screen

iosiphoneinterface-builderuitabbarcontroller

提问by emachine

I have a Tab Bar Controller which, as we know, displays the tab bar at the bottom of the screen. I'm looking for a way to move it to the top. I don't think I can use a simple UITabBarfor this as I need to nest UINavigationControllersunder it.

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

Is there any way to move the Tab Bar in a UITabBarControllerto the top of the screen?

有没有办法将 a 中的 Tab Bar 移动UITabBarController到屏幕顶部?

回答by Ujesh

Try this code in methods "viewDidLayoutSubviews" your UITabBarController

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

Swift 2.X

斯威夫特 2.X

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

Swift 3.X

斯威夫特 3.X

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

Swift 4.X

斯威夫特 4.X

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

回答by Albert Pérez Farrés

(in Swift)

(在斯威夫特中)

In the TabBarViewController.swift file (everyone has named this file as he wants):

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

  • First: create an IBOutlet of a tab bar and then connect it to the appropiate tab bar in the storyboard or in the nib file.

    @IBOutlet var profileTabBar : UITabBar!
    
  • Second: add this code in the viewDidLoad()function to situate the tab bar where you want (in this case I add de tab bar under the navigation controller). To modify the position change xand yof CGRectMakeinitializer.

    // [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)
    
  • 首先:创建一个标签栏的IBOutlet,然后将其连接到故事板或nib文件中的相应标签栏。

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

    // [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)
    

回答by Kal

I dont think so. The only thing I can think of is to use UITabBar instead of UITabbarController ...

我不这么认为。我唯一能想到的是使用 UITabBar 而不是 UITabbarController ...

This might be a better option anyway if you are considering nesting UINavigationControllers for each view loaded for the different tabs.

如果您正在考虑为为不同选项卡加载的每个视图嵌套 UINavigationControllers,这可能是一个更好的选择。

回答by Umar Hayat Khan

I use Tab Bar Controllerthen I simply change the TabBarposition in the Tab Bar Controllerin the ViewDidLoad()

我使用Tab Bar Controller然后我只是改变TabBarTab Bar ControllerViewDidLoad()

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

Here is the Screen Short attached of the required result... Tab bar on top in tab bar view conmtroller

这是所需结果的屏幕简短附加... 选项卡栏视图控制器顶部的选项卡栏

Note: It is in swift 3you can change the syntax to swift 2.* on your own.

注意:在 swift 3 中,您可以自行将语法更改为 swift 2.*。