简体   繁体   中英

How to add two UIView with collection view inside to UIViewController

I have two ui view

class StoriesView : UIView {
   //collection view inside
}

class NewsFeedView : UIView {
//collection view inside
}

In my UIViewController I want to add these views

class HomeViewController : UIViewController {
 private var _newsFeedView : NewsFeedView{return view as! NewsFeedView}
    private var _storiesView : StoriesView{return view as! StoriesView}
//
override func loadView() {
        super.loadView()
        view = NewsFeedView(frame: UIScreen.main.bounds)
        view = StoriesView(frame: UIScreen.main.bounds)

        _newsFeedView.collectionView.delegate = self
        _newsFeedView.collectionView.dataSource = self

        _storiesView.storiesCollectionView.dataSource = self
        _storiesView.storiesCollectionView.delegate = self

    }

}

and I'm getting error saying Could not cast value of type 'Vidyoga.StoriesView' (0x104db7338) to 'Vidyoga.NewsFeedView' (0x104db7050)

// This is how I did it 

        private var _newsFeedView = NewsFeedView()
        private var _storiesView = StoriesView()
    //
        //MARK:- LIFECYCLE

        override func loadView() {
            super.loadView()
            view.addSubview(_storiesView)
            view.addSubview(_newsFeedView)

            _storiesView.anchor(top: view.topAnchor, leading: view.leadingAnchor, bottom: nil, trailing: view.trailingAnchor,padding: .init(top: 1, left: 0, bottom: 0, right: 0),size: .init(width: view.frame.width, height: 100))
            //
            _newsFeedView.anchor(top: _storiesView.bottomAnchor, leading: view.leadingAnchor, bottom: view.bottomAnchor, trailing: view.trailingAnchor,padding: .init(top: 10, left: 0, bottom: 0, right: 0),size: .init(width: view.frame.width, height: 0))

            _newsFeedView.collectionView.delegate = self
            _newsFeedView.collectionView.dataSource = self

            _storiesView.storiesCollectionView.dataSource = self
            _storiesView.storiesCollectionView.delegate = self

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM