我可以使用创建一个UINavigationController自定义栏类initWithNavigationBarClass:toolbarClass:.似乎没有相应的UITabBarController,所以如何让它使用自定义UITabBar类?
到目前为止我看到的每个解决方案都不合适,因为它们也是如此
UITabBarController而不是更改其现有标签栏,或UITabBarController抛弃并创建一个新的控制器类.我希望UITabBarController在代码栏中使用自定义类创建一个真正的代码.我该如何实现这一目标?
基本上我想更改选项卡栏的高度,为此,我找到了多个通过子类化 UITabBar 来工作的解决方案。
然而,要在选项卡控制器中实际使用该子类,他们(其他SO线程中的人)似乎告诉我在故事板中更改它(子类)。但是我正在使用的应用程序以编程方式创建选项卡控制器,因此我无法更改故事板中的子类。
这是一个 SO 问题的示例,其中发布了解决方案,但它使用故事板来使用新的选项卡栏控制器子类。
所以。有没有办法在我的 UITabBarController 中使用我的子类 UITabBar?
例如。假设我从这个线程中获取解决方案,改变 iOS7/8 中 UITabBar 的高度?
@implementation MyTabBar
-(CGSize)sizeThatFits:(CGSize)size
{
CGSize sizeThatFits = [super sizeThatFits:size];
sizeThatFits.height = 100;
return sizeThatFits;
}
@end
Run Code Online (Sandbox Code Playgroud)
现在。如何使用此类 (MyTabBar) 作为我以编程方式构建的 UITabBarController 中选择的选项卡栏。
我的第一个想法是做类似的事情
self.tabBar = [[MyTabBar alloc] init];
Run Code Online (Sandbox Code Playgroud)
但该属性是只读的。
由于以下问题,我需要为我的项目使用UITabBar的子类:为什么页面Push动画Tabbar在iPhone X中向上移动。
我不使用情节提要。如何以编程方式完成此操作?
-更新-
我的CustomTabBarController.swift文件现在看起来像这样:
import UIKit
@objc class customTabBarController: UITabBarController {
override var tabBar: UITabBar {
return customTabBar
}
}
Run Code Online (Sandbox Code Playgroud)
我的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
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这给了我以下错误:
Cannot convert return expression of type 'customTabBar.Type' to return type 'UITabBar'
Run Code Online (Sandbox Code Playgroud)