最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

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

Swift crifan 6121浏览 0评论

【问题】

折腾:

[未解决]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.去搞清楚:

【已解决】iOS中UITabBarController和UINavigationController的区别
 

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按钮页面

转载请注明:在路上 » 【已解决】swift添加tab页面出错:Cannot convert value of type UITabBarController to expected argument type UIView

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.171 seconds, using 22.07MB memory