更改 UITabBar 高度

我使用 UITabBarController作为根视图,应用程序支持 iOS6及以上版本。

UITabBarController
- tab1
- UINavigationController
- UIViewController
- UIViewController
.
.
- tab2
- UINavigationController
- UIViewController
- UIViewController
.
.
.
- tab3
- UIViewController
- tab4
- UIViewController

下面的代码用于更改上面层次结构中的一个 UIViewController (位于 UINavigationController内部)中的 UITabBar高度。

CGRect tabbarFrame = self.tabBarController.tabBar.frame;
tabbarFrame.size.height += 60;
self.tabBarController.tabBar.frame = tabbarFrame;

但它并没有改变高度。以默认高度显示 UITabBar。虽然记录它的值会打印出如下所示的更改后的值。

<UITabBar: 0xb528f60; frame = (0 431; 320 109); autoresize = W+TM; layer = <CALayer: 0xb529080>>

我如何改变 UITabBar的高度来达到这样的效果: ?

enter image description here

104673 次浏览

我面对这个问题,我能够解决它。

您必须向 UITabBarController类的子类添加以下代码。

const CGFloat kBarHeight = 80;


- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];


CGRect tabFrame = self.tabBar.frame; //self.TabBar is IBOutlet of your TabBar
tabFrame.size.height = kBarHeight;
tabFrame.origin.y = self.view.frame.size.height - kBarHeight;
self.tabBar.frame = tabFrame;
}

斯威夫特:

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()


tabBar.frame.size.height = kBarHeight
tabBar.frame.origin.y = view.frame.height - kBarHeight
}

对于 IOS 8.2Xcode 6.2快速语言:

为您的 UITabBarController(类型为 UITabBarController)创建一个“ DNMainTabVC.swift”(DeveloperNameMainTabViewController.swift 文件)并将其连接到故事板 VC。

添加以下内容:

override func viewWillLayoutSubviews() {
var tabFrame = self.tabBar.frame
// - 40 is editable , the default value is 49 px, below lowers the tabbar and above increases the tab bar size
tabFrame.size.height = 40
tabFrame.origin.y = self.view.frame.size.height - 40
self.tabBar.frame = tabFrame
}

这招对我很管用。

创建类型为 UITabBar的自定义子类,然后实现以下方法:

@implementation CustomTabBar
#define kTabBarHeight = // Input the height we want to set for Tabbar here
-(CGSize)sizeThatFits:(CGSize)size
{
CGSize sizeThatFits = [super sizeThatFits:size];
sizeThatFits.height = kTabBarHeight;


return sizeThatFits;
}
@end

希望能成功。

Swift 2.0:

var tabBar:UITabBar?


override func viewWillLayoutSubviews() {
var tabFrame: CGRect = self.tabBar!.frame
tabFrame.size.height = 60
tabFrame.origin.y = self.view.frame.size.height - 60
self.tabBar!.frame = tabFrame
}

您可以通过子类化来修改标签栏的高度

override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: super.sizeThatFits(size).width, height: 60)
}

这将返回它的默认宽度,高度为60点。

在以前答案的基础上更新 Swift 3。

子类 UITabController,并确保将新的自定义类分配给 UITabController 的标识检查器。

Swift 3.0

class MainTabBarController: UITabBarController {


override func viewWillLayoutSubviews() {
var newTabBarFrame = tabBar.frame


let newTabBarHeight: CGFloat = 60
newTabBarFrame.size.height = newTabBarHeight
newTabBarFrame.origin.y = self.view.frame.size.height - newTabBarHeight


tabBar.frame = newTabBarFrame
}
}

警告 : 如果选项卡栏下面有空白,请确保将此代码放在 viewWillLayoutSubviews ()中,而不是 viewDidLoad ()中。

Xamarin 实现:

public override void ViewWillLayoutSubviews()
{
base.ViewWillLayoutSubviews();
const float newTabBarHeight = 40f;
TabBar.Frame = new CGRect(TabBar.Frame.X, TabBar.Frame.Y + (TabBar.Frame.Height - newTabBarHeight), TabBar.Frame.Width, newTabBarHeight);
}

Swift 3.0 + 在下面的代码中将200替换到您想要的高度。

   extension UITabBar {
override open func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: 200)
}
}

兼容 Swift 3.0和 Swift 4.0

Pre-iPhone X 默认标签栏高度: < strong > 49 pt

IPhone X 默认标签栏高度: < strong > 83 pt

支持所有 iOS 设备(包括 IPhone X屏幕大小)的通用解决方案如下:

  1. 捕获 UITabBar 的默认高度:

    fileprivate lazy var defaultTabBarHeight = { tabBar.frame.size.height }()
    
  2. Adjust UITabBar's height:

        override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    
    
    let newTabBarHeight = defaultTabBarHeight + 16.0
    
    
    var newFrame = tabBar.frame
    newFrame.size.height = newTabBarHeight
    newFrame.origin.y = view.frame.size.height - newTabBarHeight
    
    
    tabBar.frame = newFrame
    }
    

由于某些原因,@Rushikesh 的回答在 IOS10之前一直很好,但是我在 IOS11Swift 3.2上遇到了一些问题。

每次我触摸一个新的标签,标签栏就会改变它的框架。

我通过将代码放在 viewDidLayoutSubviews()函数中而不是 viewWillLayoutSubviews()中来修复这个问题

斯威夫特3:

override func viewDidLayoutSubviews() {


super.viewDidLayoutSubviews()
var tabFrame            = tabBar.frame
tabFrame.size.height    = 65
tabFrame.origin.y       = view.frame.size.height - 65
tabBar.frame            = tabFrame
}

XCode 9.0Swift 4中测试

如前面的答案所示,继承 UITabBar并覆盖 sizeThatFits,但将 height标记为 @IBInspectable,因此它可以在 Interface Builder中设置:

import UIKit


class CustomTabBar : UITabBar {
@IBInspectable var height: CGFloat = 0.0


override func sizeThatFits(_ size: CGSize) -> CGSize {
var sizeThatFits = super.sizeThatFits(size)
if height > 0.0 {
sizeThatFits.height = height
}
return sizeThatFits
}
}

身份调查员(something something 3)中的 UITabBar设置 CustomTabBar类:

Tab Bar Identity Inspector

然后在 属性督察(something 4)中设置所需的 Height(大于 0.0) :

Tab Bar Attributes Inspector

IPhoneX 有不同的高度,所以如果我们移动到较小的高度,那么在 iPhoneX 中标签栏的形状将是不好的

- (void)viewWillLayoutSubviews
{
int requiredHeight = 55;
CGRect tabFrame = self.tabBar.frame;
if (tabFrame.size.height < requiredHeight)
{
tabFrame.size.height = requiredHeight;
tabFrame.origin.y = self.view.frame.size.height - requiredHeight;
self.tabBar.frame = tabFrame;
}
}

Swift 4

extension UITabBar {
override open func sizeThatFits(_ size: CGSize) -> CGSize {
var sizeThatFits = super.sizeThatFits(size)
sizeThatFits.height = 60 // adjust your size here
return sizeThatFits
}
}

这也是一种方法

extension UITabBar {


override public func sizeThatFits(size: CGSize) -> CGSize {
super.sizeThatFits(size)
var sizeThatFits = super.sizeThatFits(size)
sizeThatFits.height = 71 // or whatever height you need
return sizeThatFits
}
}

使用安全区域编辑 Kiarash Asar 的回答:

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()


var safeAreaBottomInset: CGFloat = 0.0


if #available(iOS 11.0, *) {
safeAreaBottomInset = view.safeAreaInsets.bottom
}


let newTabBarHeight: CGFloat = \{\{myDesiredHeight}} + safeAreaBottomInset


var newFrame = tabBar.frame
newFrame.size.height = newTabBarHeight
newFrame.origin.y = view.frame.size.height - newTabBarHeight


tabBar.frame = newFrame
}

Swift 4 & 与 iphone x 兼容

class CustomTabBar : UITabBar {


@IBInspectable var height: CGFloat = 65.0


override open func sizeThatFits(_ size: CGSize) -> CGSize {
guard let window = UIApplication.shared.connectedScenes
.compactMap({$0 as? UIWindowScene})
.first?.windows
.filter({$0.isKeyWindow}).first else {
return super.sizeThatFits(size)
}
var sizeThatFits = super.sizeThatFits(size)
if height > 0.0 {
        

if #available(iOS 11.0, *) {
sizeThatFits.height = height + window.safeAreaInsets.bottom
} else {
sizeThatFits.height = height
}
}
return sizeThatFits
}
}

适用于所有屏幕大小: 将 tabBarHeight 设置为(tabBar-20的原始高度)这很重要,因此您可以稍后在 viewDidLayoutSubviews 中使用它,这也比硬编码所需的大小要好。因为这种尺寸可能不适用于所有屏幕。

窗口安全区域插入在标签栏的底部保持必要的填充高度,以保持与屏幕底部边缘的距离。

var tabBarHeight = CGFloat()


override func viewDidLoad() {
super.viewDidLoad()
tabBarHeight = self.tabBar.frame.height - 20
}


override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
var tabFrame = self.tabBar.frame
guard let window = UIApplication.shared.keyWindow else {return}
tabFrame.size.height = tabBarHeight + window.safeAreaInsets.bottom
self.tabBar.frame = tabFrame
}

Swift 5.3.1,XCode 11 + ,iOS 14:

import UIKit


class CustomTabBar: UITabBar {
let height: CGFloat = 62
    

override open func sizeThatFits(_ size: CGSize) -> CGSize {
guard let window = UIApplication.shared.connectedScenes
.filter({$0.activationState == .foregroundActive})
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows
.filter({$0.isKeyWindow}).first else {
return super.sizeThatFits(size)
}


var sizeThatFits = super.sizeThatFits(size)
if #available(iOS 11.0, *) {
sizeThatFits.height = height + window.safeAreaInsets.bottom
} else {
sizeThatFits.height = height
}
return sizeThatFits
}
}
class TabBarVC: UITabBarController {


//MARK:- Variable
let HEIGHT_TAB_BAR:CGFloat = 500


override func viewDidLoad() {
super.viewDidLoad()
    

// Do any additional setup after loading the view.
}




override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
var tabFrame = self.tabBar.frame
tabFrame.size.height = HEIGHT_TAB_BAR
tabFrame.origin.y = self.view.frame.size.height - HEIGHT_TAB_BAR
self.tabBar.frame = tabFrame
}




}

我觉得挺好的。

只需在安全区域的底部添加更多插图:

additionalSafeAreaInsets.bottom = 40

enter image description here

它帮助了我

override func viewDidLayoutSubviews() {
super.viewWillLayoutSubviews()


tabBar.frame.size.height = 60
tabBar.frame.origin.y = view.frame.height - 60
}

用于 UITabBarController 子类

从 UITabBar 创建子类

class CustomTabBar : UITabBar {
override open func sizeThatFits(_ size: CGSize) -> CGSize {
super.sizeThatFits(size)
var sizeThatFits = super.sizeThatFits(size)
sizeThatFits.height = 200
return sizeThatFits
}
}

并在构造函数中设置类

init() {
super.init(nibName: nil, bundle: nil)
object_setClass(self.tabBar, CustomTabBar.self)
}

对于 SwiftUI,我使用内省 自省

然后在 TabView 视图修饰符中,通过添加高度来增加视图的大小。在这里,我添加7分,然后另外2.5分为一个自定义分隔线。而且我为 iPhone 和 iPad 添加了不同的功能。

.introspectTabBarController { UITabBarController in
if sizeClass == .compact {
let barIncrease = UIView(frame: CGRect(x: 0, y: -7, width: UITabBarController.tabBar.frame.size.width, height: 9))
barIncrease.backgroundColor = UIColor(hex: "#508FD6FF")
UITabBarController.tabBar.addSubview(barIncrease)
let lineView = UIView(frame: CGRect(x: 0, y: -9.5, width: UITabBarController.tabBar.frame.size.width, height: 2.5))
lineView.backgroundColor = UIColor(hex: "#508FD6FF")
UITabBarController.tabBar.addSubview(lineView)
} else {
let lineView = UIView(frame: CGRect(x: 0, y: -2.5, width: UITabBarController.tabBar.frame.size.width, height: 2.5))
lineView.backgroundColor = UIColor(hex: "#508FD6FF")
UITabBarController.tabBar.addSubview(lineView)
}
}