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
Move Tab Bar to top of screen
提问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 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?
有没有办法将 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 Controller
then I simply change the TabBar
position in the Tab Bar Controller
in the ViewDidLoad()
我使用Tab Bar Controller
然后我只是改变TabBar
了Tab Bar Controller
在ViewDidLoad()
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.*。