ios 更改 UITabBar 高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23044218/
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 23:57:06  来源:igfitidea点击:

Change UITabBar height

iosobjective-cuitabbarcontrollerheightuitabbar

提问by Geek

I use UITabBarControlleras a root view and app supports iOS 6 and above. Project class hierarchy is as below.

UITabBarController用作根视图,应用程序支持 iOS 6 及更高版本。项目类层次结构如下。

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

I used below code to change height of UITabBarin one of the UIViewControllers (which is inside UINavigationController) in above hierarchy.

我使用下面的代码来更改上面层次结构中UITabBarUIViewControllers 之一(在里面UINavigationController)的高度。

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

But its not changing the height. UITabBaris displayed with default height. Though logging its value prints changed value as shown below.

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

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

How can I change UITabBar's height to achieve something like this:?

我怎样才能改变UITabBar的高度来实现这样的目标:?

enter image description here

在此处输入图片说明

回答by Rushikesh

I faced this issue and I was able to solve it.

我遇到了这个问题,我能够解决它。

You have to add following code to your subclass of UITabBarControllerclass.

您必须将以下代码添加到类的子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;
}

Swift:

迅速:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

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

回答by MB_iOSDeveloper

For iOS 8.2, Xcode 6.2Swift language:

对于iOS 8.2Xcode 6.2Swift 语言:

Create a "DNMainTabVC.swift" (DeveloperNameMainTabViewController.swift file) for your UITabBarController(of type UITabBarController) and connect it to your storyboard VC.

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

Add the following lines:

添加以下几行:

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
}

This worked for me.

这对我有用。

回答by Kiarash Asar

Swift3.0, Swift 4.0 compatible

Swift3.0、Swift 4.0 兼容

Pre-iPhone Xdefault tab bar height: 49pt

iPhone X 之前的默认标签栏高度:49pt

iPhone Xdefault tab bar height: 83pt

iPhone X默认标签栏高度:83pt

A universal solution supporting every iOS device including iPhone Xscreen size would look like this:

支持包括iPhone X屏幕尺寸在内的所有 iOS 设备的通用解决方案如下所示:

  1. Capture UITabBar's default height:

    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
        }
    
  1. 捕获 UITabBar 的默认高度:

    fileprivate lazy var defaultTabBarHeight = { tabBar.frame.size.height }()
    
  2. 调整 UITabBar 的高度:

        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
        }
    

回答by sKhan

Create a custom subclass of type UITabBar, then implement the following method :

创建 type 的自定义子类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

Hope this will work.

希望这会奏效。

回答by msrdjan

Tested in XCode 9.0and Swift 4

XCode 9.0Swift 4 中测试

As suggested in previous answers - inherit UITabBarand override sizeThatFits, but mark heightas @IBInspectable, so it could be set in the Interface Builder:

正如之前的答案所建议的 - 继承UITabBar和覆盖sizeThatFits,但标记height@IBInspectable,因此它可以在界面生成器中设置:

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

Set CustomTabBarclass for the UITabBarin the Identity Inspector(??3):

Identity Inspector(??3) 中设置CustomTabBar类:UITabBar

Tab Bar Identity Inspector

标签栏身份检查器

Then set desired Height(greater than 0.0) in the Attributes Inspector(??4):

然后在属性检查器(??4) 中设置所需的Height(大于):0.0

Tab Bar Attributes Inspector

标签栏属性检查器

回答by Ramazan Karami

For Swift 4

对于 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
    }
 }

回答by A.G

Swift 2.0:

斯威夫特 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
}

回答by William Hu

Swift 3.0+Replace 200 to your desired height in below code.

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

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

回答by seggy

Swift 4 & compatible with iphone x

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

回答by Kqtr

Building up on previous answers and updating for Swift 3.

建立在以前的答案的基础上,并针对 Swift 3 进行更新。

Subclass UITabController and make sure to assign your new custom class to the Identity Inspector of your UITabController.

子类化 UITabController 并确保将新的自定义类分配给 UITabController 的 Identity Inspector。

Swift 3.0

斯威夫特 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
    }
}

Warning: if you get a blank space below your tab bar, make sure you did put this code in viewWillLayoutSubviews() and not viewDidLoad().

警告:如果在标签栏下方有一个空白区域,请确保将此代码放在 viewWillLayoutSubviews() 而不是 viewDidLoad() 中。