0

I've created header using UICollectionViewComposotionalLayout like below


 //header
        let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(30))
        
        let header = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerSize, elementKind: "header", alignment: .top)
        
        section.boundarySupplementaryItems = [header]

and works fine but without any reason when I change the language of the app. all the view changes it's location from left to right using this func : UIView.appearance().semanticContentAttribute = .forceRightToLeft except the collectionView header


so I've to trigger it by language and I updated the code to : ▼ but it didn't work


let headerTrailing = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerSize, elementKind: "header", alignment: .topTrailing)
    
    let headerLeading = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerSize, elementKind: "header", alignment: .topLeading)
    
    if AppLocalization.currentAppleLanguage() == "en" {
        section.boundarySupplementaryItems = [headerLeading]
    } else {
        section.boundarySupplementaryItems = [headerTrailing]
    }

so I've tried to set the textAlignment of the header label in viewForSupplementaryElementOfKind func like below ▼ but it didn't work :

view.headerLabel.textAlignment = .natural

also tried to set it to natural in it's viewCell ▼ but it didn't work :


override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        headerLabel.textAlignment = .natural
       
        
    }

and I also tried this extension to it also ▼ but it didn't work :


extension UILabel {
    open override func awakeFromNib() {
        super.awakeFromNib()
        if AppLocalization.currentAppleLanguage() == "ar" {
            if textAlignment == .natural {
                self.textAlignment = .right
            }
        }
    }
}

so what should I do to make the header change it's location every time I change the language of the app without relaunching the app again !!?

may be I guess there is a func like viewWillAppear but for UICollectionReusableView to reload the view every time I change the language but I've tried lot of methods there instead of awakeFromNib Also but it doesn't work!!?

  • Have you tried myCollectionView.collectionViewLayout.invalidateLayout() – Osama Remlawi Oct 28 '21 at 06:13
  • What happens if you add: UILabel.appearance().semanticContentAttribute = .forceRightToLeft – Rob C Oct 28 '21 at 08:05
  • @OsamaRemlawi sorry but nothing works from your answer there: tried myCollectionView.collectionViewLayout.invalidateLayout() in viewWillAppear to update the collection every time and tried also in ViewDidLoad but both failed – abdallah eslah Oct 28 '21 at 19:32
  • @Rob and tried this -> UILabel.appearance().semanticContentAttribute = .forceRightToLeft when triggering the language in action button but it doesn't work also – abdallah eslah Oct 28 '21 at 19:32
  • May be there is a func to update the collectionViewHeader EveryTime reloaded to the screen – abdallah eslah Oct 28 '21 at 19:33

0 Answers0