繁体   English   中英

支持 RTL 语言的项目上的 UICollectionView iOS 9 问题

[英]UICollectionView iOS 9 issue on project with RTL languages support

在使用UICollectionView时,似乎 Apple 在 RTL 语言上的自动翻转界面的新功能会导致问题。

我在集合视图中使用了 Trailing/Leading 类型的约束,并且它们在 RTL 语言上按应有的方式切换了它们的值。

问题是实际呈现的数据是集合数据源中最后一个indexPath的数据,但第一个单元格的UIScrollView.contentOffset.x为 0。

正确的行为应该是以下之一:

  1. 正确显示第一个indexPath并切换滚动方向(向右)-最佳选择
  2. 不翻转UI /约束,因此呈现的数据/ indexPath / scrollView.contentOffset.x将被同步-选项,禁用了RTL支持。
  3. 呈现最后一个 indexPath 的单元格和数据,但固定scrollView.contentOffset.x以表示最后一个单元格位置。

我猜苹果可能会在未来的某个时候修复它,但同时我们将不得不使用诸如反转数组和/或滚动到最后一个对象之类的解决方法。

我遇到了类似的情况,并为此找到了解决方案。 如果您使用 swift,请将以下代码段添加到您的项目中,它将确保 bounds.origin 始终遵循集合视图的前沿。

extension UICollectionViewFlowLayout {

    open override var flipsHorizontallyInOppositeLayoutDirection: Bool {
        return true
    }
}

如果您使用的是 Objective-C,只需继承 UICollectionViewLayout 类,并覆盖 flipsHorizo​​ntallyInOppositeLayoutDirection,并返回 true。 使用这个子类作为你的集合视图的布局对象。

我迟到了,但如果您不想创建扩展,因为它会影响我们应用程序中的所有集合视图。 只需创建您自己的自定义类,即。

class customLayoutForLocalization : UICollectionViewFlowLayout{
    open override var flipsHorizontallyInOppositeLayoutDirection: Bool {
         return true
    } 
}

要使用这个类:

// your way of deciding on whether you need to apply this layout may vary depending on use of other tools like LanguageManager_iOS to handle multi-language support
if myCollectionView.effectiveUserInterfaceLayoutDirection == .rightToLeft  {
    let customLayout = customLayoutForRTL()
    // if your elements are variable size use the following line
    customLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
    // if you want horizontal scroll (single line)
    customLayout.scrollDirection = .horizontal
    myCollectionView.collectionViewLayout = customLayout
}

该问题有一个对我有用的通用解决方案,请按照以下步骤解决该问题,

  • 根据您的要求提供自动布局约束,然后从属性检查器将集合视图的语义控制属性更改为从故事板从右到左强制。

在此处输入图片说明

  • 然后打开故事板作为源代码,找到相关集合视图的“前导”属性并将其替换为“左”,同样将“尾随”替换为“右”。 现在你差不多完成了。

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

  • 现在,这将根据您的要求为您提供结果。
import UIKit

extension UICollectionViewFlowLayout {

    open override var flipsHorizontallyInOppositeLayoutDirection: Bool {
        return UIApplication.shared.userInterfaceLayoutDirection == UIUserInterfaceLayoutDirection.rightToLeft
    }

虽然简单的数学可以解决问题,但并不漂亮。 (用于水平集合视图)

- (void)switchSemanticDirection:(UISwitch*)sender {
    //TEST switch the semantic direction between LTR and RTL.
    if (sender.isOn) {
        UIView.appearance.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
    } else {
        UIView.appearance.semanticContentAttribute = UISemanticContentAttributeForceRightToLeft;
    }

    [self.myContent removeFromSuperview];
    [self.view addSubview:self.myContent];
    
    //reload your collection view to apply RTL setting programmatically
    [self.list reloadData];
    //position your content into the right offset after flipped RTL
    self.list.contentOffset = CGPointMake(self.list.contentSize.width - self.list.contentOffset.x - self.list.bounds.size.width, 
    self.list.contentOffset.y);
}

暂无
暂无

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

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