简体   繁体   English

在Swift iOS中更改方向时更改UICollectionViewCell标签textColor

[英]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. 我想在设备方向更改时更改UICollectionViewCell内的UILabel的颜色。 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? 问题是,如果我在视图控制器中观察到方向变化,如何在通知选择器内获取对UICollectionView单元的引用以更改单元的标签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. 但是我只能访问该indexPath的一个单元格视图。 When I set an Int variable and set it value inside cellForRowAtIndexPath, nothing happens because it holds the value of the last item. 当我设置一个Int变量并将其值设置在cellForRowAtIndexPath内部时,什么都不会发生,因为它保存了最后一项的值。

Put this in cellForRowAt 把它放在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. 如果您使用的是Storyboard,则可以使用Vary for traits来更改标签颜色,具体取决于方向。 在此处输入图片说明

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 !! 就是这样,您做到了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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