1

I am working on a TVOS app that has a UICollectionView acting as a tab bar. Each cell in the collection view has a UILabel with a string. I want the UICollectionView to be RightToLeft aligned. In order to do so I use the semanticContentAttribute on the collection view in viewDidLoad() and assign it to .ForceLeftToRight

This all works fine until I try to set each cells width differently, then the collection view reverses the order of cells.

Maybe someone can help explain to me why this happens.

here's my ViewDidLoad() :

  override func viewDidLoad() {

    super.viewDidLoad()


    self.collectionView.semanticContentAttribute = .ForceRightToLeft // make collection view right to left

    } 

and the sizeForItem method:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {   

    let width:CGFloat = self.widthForView(self.tabNames[indexPath.row])

    return CGSizeMake(width, 110)

}

this is the custom width method:

func widthForView(text:String) -> CGFloat{

    let label:UILabel = UILabel(frame: CGRectMake(0, 0, CGFloat.max, 110))

    label.numberOfLines = 0

    label.lineBreakMode = NSLineBreakMode.ByWordWrapping

    label.font = UIFont().myCustomFontWithSize(33)

    label.text = text

    label.sizeToFit()

    let width = label.frame.size.width

    return width

}
Sosily
  • 706
  • 1
  • 10
  • 26

0 Answers0