本文介绍了如何以编程方式替换UITabBarController的UITabBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于以下问题,我需要为我的项目使用UITabBar的子类.
I need to use a subclass of the UITabBar for my project because of the following problem Why page Push animation Tabbar moving up in the iPhone X.
我不使用情节提要.如何以编程方式完成此操作?
I do not use storyboards. How can this be done programmatically?
-更新-
我的CustomTabBarController.swift文件现在看起来像这样:
My CustomTabBarController.swift file now looks like this:
import UIKit
@objc class customTabBarController: UITabBarController {
override var tabBar: UITabBar {
return customTabBar
}
}
我的CustomTabBar.swift文件如下所示:
And my CustomTabBar.swift file looks like this:
import UIKit
class customTabBar: UITabBar {
override var frame: CGRect {
get {
return super.frame
}
set {
var tmp = newValue
if let superview = superview, tmp.maxY !=
superview.frame.height {
tmp.origin.y = superview.frame.height - tmp.height
}
super.frame = tmp
}
}
}
但这给了我以下错误:
Cannot convert return expression of type 'customTabBar.Type' to return type 'UITabBar'
推荐答案
您应该创建自定义标签栏的实例,并用它覆盖UITabBarController子类中的tabBar属性.
You should create an instance of your custom tab bar and override the tabBar property inside your UITabBarController subclass with it.
class CustomTabBarController: UITabBarController {
let customTabBar = CustomTabBar()
override var tabBar: UITabBar {
return customTabBar
}
}
这篇关于如何以编程方式替换UITabBarController的UITabBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!