我使用UITabBarController
作为根视图,应用程序支持iOS 6及更高版本。项目类层次结构如下。
UITabBarController
- tab1
- UINavigationController
- UIViewController
- UIViewController
.
.
- tab2
- UINavigationController
- UIViewController
- UIViewController
.
.
.
- tab3
- UIViewController
- tab4
- UIViewController
我使用下面的代码来改变上面层次结构中的一个UIViewControllers(在UITabBar
里面)中的UINavigationController
的高度。
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
的高度来达到这样的目的:?
我遇到了这个问题,我能够解决它。
您必须将以下代码添加到UITabBarController
类的子类中。
const CGFloat kBarHeight = 80;
- (void)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;
}
存储实施:
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 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
}
}
出于某种原因,@ Rushikesh的答案在iOS 10之前运行良好,但我在iOS 11和Swift 3.2上遇到了一些问题。
每次触摸新标签时,tabBar都会更改其框架。
我通过将代码放在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 6.0
override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: super.sizeThatFits(size).width, height: 60)
}
这应该返回其默认宽度,高度为60pts。
这也是一种方法
extension UITabBar {
override public func sizeThatFits(size: CGSize) -> CGSize {
super.sizeThatFits(size)
var sizeThatFits = super.sizeThatFits(size)
sizeThatFits.height = 71
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
}
使用安全区编辑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
}
iPhone X有不同的高度,所以如果我们移动到较小的高度,那么iPhone X中的tabbar形状会很糟糕
- (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;
}
}
对于iOS 8.2,Xcode 6.2 Swift语言:
为你的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
}
这对我有用。
Swift3.0,兼容Swift 4.0
Pre-iPhone X默认标签栏高度:49pt
iPhone X默认标签栏高度:83pt
支持每个iOS设备(包括iPhone X屏幕尺寸)的通用解决方案如下所示:
fileprivate lazy var defaultTabBarHeight = { tabBar.frame.size.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
}
创建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
希望这会奏效。
在XCode 9.0和Swift 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
}
}
在Identity Inspector中为CustomTabBar
设置UITabBar
类(⌥⌘3):
然后在Attributes Inspector(⌥⌘4)中设置所需的Height
(大于0.0
):
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
}
对于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
}
}
构建以前的答案并更新Swift 3。
子类UITabbarController并确保将新的自定义类分配给UITabbarController的Identity Inspector。
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()中。
Swift 3.0+在下面的代码中将200替换为您想要的高度。
extension UITabBar {
override open func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width, height: 200)
}
}