如何以编程方式替换 UITabBarController 的 UITabBar
How can I substitute the UITabBar of UITabBarController programmatically
由于以下问题Why page Push animation Tabbar moving up in the iPhone X,我的项目需要使用 UITabBar 的子类。
我不使用故事板。如何以编程方式完成此操作?
--更新--
我的 CustomTabBarController.swift 文件现在看起来像这样:
import UIKit
@objc class customTabBarController: UITabBarController {
override var tabBar: UITabBar {
return customTabBar
}
}
我的 CustomTabBar.swift 文件如下所示:
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 属性。
class CustomTabBarController: UITabBarController {
let customTabBar = CustomTabBar()
override var tabBar: UITabBar {
return customTabBar
}
}
您收到 'customTabBar.Type'
错误的原因是因为您要返回 class 的名称,这与通常的惯例相反,是驼峰式的。您想要返回一个 object - class 的一个实例 - 相反。
import UIKit
@objc class customTabBarController: UITabBarController {
let myCustomTabBar = customTabBar() // If we're writing conventional swift,
// this should be CustomTabBar()
override var tabBar: UITabBar {
// Because your class name is customTabBar instead of CustomTabBar,
// this is returning a TYPE instead of an OBJECT
// return customTabBar
// If you wanted to return a new version of a tab bar on every
// get of this object, you'd use the following code which is similar to yours:
// return customTabBar()
// More likely, you want to return the SAME tabBar throughout this object's
// lifecycle.
return myCustomTabBar
}
}
中给出了唯一可行的解决方案,方法是在 UITabBarController
上使用 setValue(:forKey:)
。
由于以下问题Why page Push animation Tabbar moving up in the iPhone X,我的项目需要使用 UITabBar 的子类。
我不使用故事板。如何以编程方式完成此操作?
--更新--
我的 CustomTabBarController.swift 文件现在看起来像这样:
import UIKit
@objc class customTabBarController: UITabBarController {
override var tabBar: UITabBar {
return customTabBar
}
}
我的 CustomTabBar.swift 文件如下所示:
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 属性。
class CustomTabBarController: UITabBarController {
let customTabBar = CustomTabBar()
override var tabBar: UITabBar {
return customTabBar
}
}
您收到 'customTabBar.Type'
错误的原因是因为您要返回 class 的名称,这与通常的惯例相反,是驼峰式的。您想要返回一个 object - class 的一个实例 - 相反。
import UIKit
@objc class customTabBarController: UITabBarController {
let myCustomTabBar = customTabBar() // If we're writing conventional swift,
// this should be CustomTabBar()
override var tabBar: UITabBar {
// Because your class name is customTabBar instead of CustomTabBar,
// this is returning a TYPE instead of an OBJECT
// return customTabBar
// If you wanted to return a new version of a tab bar on every
// get of this object, you'd use the following code which is similar to yours:
// return customTabBar()
// More likely, you want to return the SAME tabBar throughout this object's
// lifecycle.
return myCustomTabBar
}
}
UITabBarController
上使用 setValue(:forKey:)
。