[英]Move Tab Bar to top of screen
我有一個標簽欄 Controller,眾所周知,它在屏幕底部顯示標簽欄。 我正在尋找一種將其移至頂部的方法。 我認為我不能為此使用簡單的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
(在斯威夫特)
在 TabBarViewController.swift 文件中(每個人都將這個文件命名為他想要的名稱):
首先:創建一個標簽欄的 IBOutlet,然后將其連接到 storyboard 或 nib 文件中的相應標簽欄。
@IBOutlet var profileTabBar: UITabBar!
第二:在viewDidLoad() function 中添加此代碼以將標簽欄放置在您想要的位置(在這種情況下,我在導航控制器下添加了標簽欄)。 要修改 position 更改CGRectMake初始化程序的x和y 。
// [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 ......
如果您正在考慮為為不同選項卡加載的每個視圖嵌套 UINavigationControllers,這可能是一個更好的選擇。
我使用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.
}
}
這是所需結果的屏幕簡短附件...標簽欄視圖控制器頂部的標簽欄
注意:在 swift 3 中,您可以自行將語法更改為 swift 2.*。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.