【已解决】swift添加tab页面出错:Cannot convert value of type UITabBarController to expected argument type UIView

【问题】

折腾:

[未解决]swift实现在特定一块矩形区域内添加tab按钮页面

期间,参考:

Swift Swift: Using Tab Bar Controllers in Swift | Making App Pie

去写代码:

        let recordTabBarView = UITabBarController()
        let allRecordTab = AllRecordTabViewController(nibName: "全部记录", bundle: nil)
        let incomeRecordTab = IncomeRecordTabViewController(nibName: "收入记录", bundle: nil)
        let expenseRecordTab = ExpenseRecordTabViewController(nibName: "支出记录", bundle: nil)
        let tabControllers = [allRecordTab, incomeRecordTab, expenseRecordTab]
       
        recordTabBarView.viewControllers = tabControllers
       
       
        self.view.addSubview(recordTabBarView)

结果出错:

/MySchoolCoinViewController.swift:99:30: Cannot convert value of type ‘UITabBarController’ to expected argument type ‘UIView’

如图:

Cannot convert value of type UITabBarController to expected argument type UIView

 

【解决过程】

1.搜:

swift tab view programmatically

参考:

uiviewcontroller – How add tabs programmatically in UITabBarController with swift? – Stack Overflow

ios – Programatically switching between tabs within Swift – Stack Overflow

结果都不太符合我此处要求:

我此处是:

在一个view的页面下半部分,添加一个tab的view,而不是整个页的都是tab。

2.然后参考:

uiviewcontroller – How add tabs programmatically in UITabBarController with swift? – Stack Overflow

去把:

UITabBarController, UITabBarControllerDelegate

加入到class中,结果又出错:

MySchoolCoinViewController.swift:11:53: Multiple inheritance from classes ‘UIViewController’ and ‘UITabBarController’

error Multiple inheritance from classes UIViewController and UITabBarController

3.总之是:

想要在一个UIView内部添加tab

搜:

swift UIView internal add tab view

swift inside UIView add tab

swift tabbar inside UIView

swift tabbar inside view

参考:

ios – UICollectionView inside UIView container: didSelectRowAtIndexPath doesn’t get called after scroll – Stack Overflow

ios – Swift-How do I add Tab Bar AND Navigation Bar to a single view controller? – Stack Overflow

看到了有说,embed view

4.去搜:

swift embed tab inside view

参考:

ios – How to use Navigation Controller inside of UITabBarController with Storyboard on Swift – Stack Overflow

5.自己瞎试代码:

self.addChildViewController(recordTabBarView)
//self.view.addSubview(recordTabBarView)

结果可以编译,但是无法运行,进入界面后就挂了:

Thread 1:signal SIGABRT

【已解决】swift代码运行出错:Thread 1 signal SIGABRT

 

6.去搞清楚:

 

7.去搜:

Cannot convert value of type UITabBarController to expected argument type UIView

不过后来参考之前看到的:

iphone – Tab bar controller inside a navigation controller, or sharing a navigation root view – Stack Overflow

去试试:

        self.view.addSubview(recordTabBarView.view)

结果代码是没有错误提示了,算是解决了此问题了。

 

【总结】

此处就是因为,参数类型不对:

let recordTabBarView = UITabBarController()
。。。
self.view.addSubview(recordTabBarView)

改为:

let recordTabBarView = UITabBarController()
。。。
self.view.addSubview(recordTabBarView.view)

至少语法上是对了。

 

至于达到所需要的,在特定大小的矩形区域内创建tab视图,则继续去折腾:

[未解决]swift实现在特定一块矩形区域内添加tab按钮页面



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量