简体   繁体   English

将标签栏移动到屏幕顶部

[英]Move Tab Bar to top of screen

I have a Tab Bar Controller which, as we know, displays the tab bar at the bottom of the screen.我有一个标签栏 Controller,众所周知,它在屏幕底部显示标签栏。 I'm looking for a way to move it to the top.我正在寻找一种将其移至顶部的方法。 I don't think I can use a simple UITabBar for this as I need to nest UINavigationControllers under it.我认为我不能为此使用简单的UITabBar ,因为我需要在它下面嵌套UINavigationControllers

Is there any way to move the Tab Bar in a UITabBarController to the top of the screen?有没有办法将UITabBarController中的标签栏移动到屏幕顶部?

Try this code in methods "viewDidLayoutSubviews" your UITabBarController在您的UITabBarController方法“viewDidLayoutSubviews”中尝试此代码

Swift 2.X Swift 2.X

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

Swift 3.X Swift 3.X

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

Swift 4.X Swift 4.X

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

(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,然后将其连接到 storyboard 或 nib 文件中的相应标签栏。

     @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).第二:在viewDidLoad() function 中添加此代码以将标签栏放置在您想要的位置(在这种情况下,我在导航控制器下添加了标签栏)。 To modify the position change x and y of CGRectMake initializer.要修改 position 更改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)

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,这可能是一个更好的选择。

I use Tab Bar Controller then I simply change the TabBar position in the Tab Bar Controller in the ViewDidLoad()我使用Tab Bar Controller然后我只需在TabBar ViewDidLoad()中的Tab Bar Controller

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 3 you can change the syntax to swift 2.* on your own.注意:在 swift 3 中,您可以自行将语法更改为 swift 2.*。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM