简体   繁体   中英

Change UICollectionViewCell label textColor on orientation changes in Swift iOS

I want to change the colour of a UILabel inside a UICollectionViewCell when the device orientation changes. How can I achieve this.

NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange),
                                           name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)

I wanted to observer orientation changed notification inside of the cell but i though it's not right, because there would be no way to remove the observer. The question is if I observe orientation changes in view controller, how do I get a reference to the UICollectionView cells inside the notification selector to change the cell's label textColor? This is what I was tried to use:

let indexPath = IndexPath(item: 0, section: 0)
if let cell = collectionView.cellForItem(at: indexPath) as? CommentCell {
    cell.displayNameLabel.textColor = .white
    cell.commentLabel.textColor = .white
}

but i can only access one cell view that indexPath. When I set an Int variable and set it value inside cellForRowAtIndexPath, nothing happens because it holds the value of the last item.

Put this in cellForRowAt

 if(orinatedLogic)
 {
         cell.displayNameLabel.textColor = .white
         cell.commentLabel.textColor = .white
  }
  else
  {
         cell.displayNameLabel.textColor = .blue
          cell.commentLabel.textColor = .blue
  }

and reload the collection whenever orientation changed

If you are using Storyboard you can use Vary for traits to change your label colour depending upon the orientation. 在此处输入图片说明

There is lot of possibilities depends on you requirements. I assume you don't want to reload entire collection view. But you can enumerate all visible cells and either reload them,

collectionView.reloadItems(at: collectionView.indexPathsForVisibleItems)

or change them directly.

collectionView.visibleCells.flatMap{ $0 as? CommentCell}.forEach {
    $0.commentLabel.textColor = ...
}

For Orientation change you can simply add

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {

    super.viewWillTransition(to: size, with: coordinator)

       collectionView.reloadItems(at: collectionView.indexPathsForVisibleItems)

}

In your collection view cell for item

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "item", for: indexPath) as! YourCellType

    cell.displayNameLabel.textColor = UIDevice.current.orientation.isPortrait ? .white : .red

    cell.commentLabel.textColor = UIDevice.current.orientation.isPortrait ? .white : .red

}

That's It , You did it !!

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