[英]UICollectionView iOS 9 issue on project with RTL languages support
在使用UICollectionView
时,似乎 Apple 在 RTL 语言上的自动翻转界面的新功能会导致问题。
我在集合视图中使用了 Trailing/Leading 类型的约束,并且它们在 RTL 语言上按应有的方式切换了它们的值。
问题是实际呈现的数据是集合数据源中最后一个indexPath
的数据,但第一个单元格的UIScrollView.contentOffset.x
为 0。
正确的行为应该是以下之一:
indexPath
并切换滚动方向(向右)-最佳选择indexPath
/ scrollView.contentOffset.x
将被同步-选项,禁用了RTL支持。scrollView.contentOffset.x
以表示最后一个单元格位置。我猜苹果可能会在未来的某个时候修复它,但同时我们将不得不使用诸如反转数组和/或滚动到最后一个对象之类的解决方法。
我遇到了类似的情况,并为此找到了解决方案。 如果您使用 swift,请将以下代码段添加到您的项目中,它将确保 bounds.origin 始终遵循集合视图的前沿。
extension UICollectionViewFlowLayout {
open override var flipsHorizontallyInOppositeLayoutDirection: Bool {
return true
}
}
如果您使用的是 Objective-C,只需继承 UICollectionViewLayout 类,并覆盖 flipsHorizontallyInOppositeLayoutDirection,并返回 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.