Sunday, November 8, 2026
HomeiOS Developmentios - Tips on how to present CollectionViewCell in CollectionViewController

ios – Tips on how to present CollectionViewCell in CollectionViewController

[ad_1]

I wish to make an software that makes use of tab bar construction and I wish to use assortment view in my first tab bar. I am unable to present the gathering view cell inside the gathering view controller.

I’ve navigation bar in assortment view and I wish to present 3 crimson assortment view cell below navigation bar however I can’t. I’m utilizing programmatically auto format.

I don’t get any error or warning. Right here is my code in controllers.

class MainTabController: UITabBarController {
    // MARK: - Init
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        view.backgroundcolor = .blue
        configureUI() 
        configureViewControllers()
    }

    //MARK: - Helpers
    
    func configureUI() {
        view.addSubview(actionButton)
        actionButton.anchor( backside: view.safeAreaLayoutGuide.bottomAnchor, proper: view.rightAnchor, paddingBottom: 64, paddingRight: 16, width: 56, peak: 56)
        actionButton.layer.cornerRadius = 56/2
    }
    
    func configureViewControllers() {
        let feed = FeedController(collectionViewLayout: UICollectionViewLayout())
        let navFeed = templateNavigationController(picture: UIImage(named: "home_unselected"), rootViewController: feed)
        
        let discover = ExploreController()
        let navExplore = templateNavigationController(picture: UIImage(named: "search_unselected"), rootViewController: discover)
        
        let notifications = NotificationsController()
        let navNotifications = templateNavigationController(picture: UIImage(named: "like_unselected"), rootViewController: notifications)
        
        let conversations = ConversationsController()
        let navConversations = templateNavigationController(picture: UIImage(named: "ic_mail_outline_white_2x-1"), rootViewController: conversations)
        
        viewControllers = [navFeed, navExplore, navNotifications, navConversations]
    }
    
    func templateNavigationController(picture: UIImage?, rootViewController: UIViewController) -> UINavigationController {
        
        let nav = UINavigationController(rootViewController: rootViewController)
        nav.tabBarItem.picture = picture
        nav.navigationBar.backgroundColor = .white
        return nav
    }
    
}
personal let reuseIdentifier = "MyCell"

class FeedController: UICollectionViewController {
    
    
    
    //MARK: - Properties
    
    var consumer: Person? {
        didSet {
            configureLeftBarButton()
        }
    }
    
    
    //MARK: - Lifecycle
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        configureUI()
        
        /*
        ----I attempt to add these traces of codes however didnt work.
        collectionView.delegate = self
        collectionView.dataSource = self
         */
    }
    
    //MARK: - API
    
    
    //MARK: - Helpers
    
    func configureUI() {
        view.backgroundColor = .white
        
        collectionView.register(TweetCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        /*
         Once I change backgroundcolor of collectionView. It really works 
        */
        collectionView.backgroundColor = .white
        
        showNavigationController()
        
    }
    
    func configureLeftBarButton() {
        
        guard let consumer = consumer else {return}
        
        let profilePhotoView = UIImageView()
        profilePhotoView.setDimensions(width: 32, peak: 32)
        profilePhotoView.layer.cornerRadius = 32 / 2
        profilePhotoView.layer.masksToBounds = true
        
        
        profilePhotoView.sd_setImage(with: consumer.profilePhotoUrl, accomplished: nil)
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: profilePhotoView)
    }
}

extension FeedController {
  
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {
        return 3
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! MyCell
        cell.backgroundColor = .crimson
        return cell
    }
}

extension FeedController: UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, format collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.body.width, peak: 200)
    }
} 
class MyCell: UICollectionViewCell {
    override init(body: CGRect) {
        tremendous.init(body: body)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been applied")
    }
}

//MARK: - UIViewController

extension UIViewController {
    func showNavigationController() {
        if #obtainable(iOS 13.0, *) {
            let navBarAppearance = UINavigationBarAppearance()
            navBarAppearance.configureWithOpaqueBackground()
            navBarAppearance.backgroundColor = .white
            navigationController?.navigationBar.standardAppearance = navBarAppearance
            navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
            
        }
    }
} 

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments