Ios 带约束和RTL的UIScrollView的奇怪行为
我有一个水平滚动视图,动态添加视图。 在LTR语言中,一切正常,我从左到右依次添加视图。 在RTL上,问题是视图总是添加到滚动的左侧,而不是像在其他控制器中一样添加到右侧,确实奇怪的是,视图的顺序被正确地添加到了第一个视图的左侧,因此它们是从右到左排序的,但在-x上的滚动视图之外 以下是我添加新视图时的代码:Ios 带约束和RTL的UIScrollView的奇怪行为,ios,uiscrollview,autolayout,right-to-left,nslayoutconstraint,Ios,Uiscrollview,Autolayout,Right To Left,Nslayoutconstraint,我有一个水平滚动视图,动态添加视图。 在LTR语言中,一切正常,我从左到右依次添加视图。 在RTL上,问题是视图总是添加到滚动的左侧,而不是像在其他控制器中一样添加到右侧,确实奇怪的是,视图的顺序被正确地添加到了第一个视图的左侧,因此它们是从右到左排序的,但在-x上的滚动视图之外 以下是我添加新视图时的代码: Tag* tag = [self.storyboard instantiateViewControllerWithIdentifier:@"tag" ]; [_scroller addSu
Tag* tag = [self.storyboard instantiateViewControllerWithIdentifier:@"tag" ];
[_scroller addSubview:tag.view];
[tags addObject:tag];
Tag* prev = nil
for (Tag* tag in tags)
{
if (prev == nil)
{
[_scroller addConstraint:[NSLayoutConstraint constraintWithItem:tag.view
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:_scroller
attribute:NSLayoutAttributeLeading
multiplier:1.0f
constant:0]];
}
else
{
[_scroller addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[prev]-10-[tag]"
options:0
metrics:nil
views:@{@"tag" : tag.view, @"prev" : prev.view}]];
}
[_scroller addConstraint:[NSLayoutConstraint constraintWithItem:tag.view
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:_scroller
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0]];
prev = tag;
}
下面是它如何在LTR和RTL上工作以及它如何实际工作的图像
试试这个
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
{
UIView *baseView;
UILabel *titleLabel;
NSMutableArray *infoArray ;
UIScrollView *mainscrollview;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
infoArray =[[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3", nil];
NSLog(@"%@",infoArray);
mainscrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 380)];
mainscrollview.delegate=self;
mainscrollview.contentSize=CGSizeMake(320*infoArray.count, 0);
[self.view addSubview:mainscrollview];
[self sscrollcontent:@"LTR"];//LTR for Lefttoright other than LTR it will show RTL
}
-(void)sscrollcontent:(NSString *)flowtype
{
int xaxis=0;
for (int i=0; i<infoArray.count; i++) {
baseView=[[UIView alloc]initWithFrame:CGRectMake(xaxis, 0, 320, 380)];
[mainscrollview addSubview:baseView];
titleLabel =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
titleLabel.textAlignment=NSTextAlignmentCenter;
if ([flowtype isEqualToString:@"LTR"]) {
titleLabel.text=infoArray[i];
}
else
{
titleLabel.text=infoArray[infoArray.count-i-1];
}
[baseView addSubview:titleLabel];
xaxis=xaxis+320;
}
}
@end
#导入“ViewController.h”
@界面视图控制器()
{
UIView*baseView;
UILabel*标题标签;
NSMutableArray*infoArray;
UIScrollView*主滚动视图;
}
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
infoArray=[[NSMutableArray alloc]initWithObjects:@“1”,“2”,“3”,nil];
NSLog(@“%@”,infoArray);
mainscrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0320380)];
mainscrollview.delegate=self;
mainscrollview.contentSize=CGSizeMake(320*infoArray.count,0);
[self.view addSubview:mainscrollview];
[self-sscrollcontent:@“LTR”];//对于Lefttoright而不是LTR,它将显示RTL
}
-(void)sscrollcontent:(NSString*)流类型
{
int-xaxis=0;
对于(inti=0;i这是我的示例代码
//
// ViewController.m
// testConstraint
//
// Created by stevenj on 2014. 3. 24..
// Copyright (c) 2014년 Steven Jiang. All rights reserved.
//
#import "ViewController.h"
@interface TagView : UILabel
- (void)setNumber:(NSInteger)num;
@end
@implementation TagView
- (void)setNumber:(NSInteger)num
{
[self setText:[NSString stringWithFormat:@"%d",num]];
}
@end
@interface ViewController ()
@property (nonatomic, strong) UIScrollView *scroller;
@property (nonatomic, strong) NSMutableArray *tags;
@property (nonatomic, strong) NSMutableArray *constraint;
@end
@implementation ViewController
@synthesize scroller = _scroller;
- (void)viewDidLoad
{
[super viewDidLoad];
_tags = [NSMutableArray new];
_constraint = [NSMutableArray new];
// Do any additional setup after loading the view, typically from a nib.
//step.1 create scroll view
_scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 60)];
[_scroller setBackgroundColor:[UIColor lightGrayColor]];
[_scroller removeConstraints:[_scroller constraints]];
[_scroller setTranslatesAutoresizingMaskIntoConstraints:YES];
[self.view addSubview:_scroller];
//step.2 add tag view
for (int i=0; i<10; i++) {
TagView *tag = [[TagView alloc] init];
[tag setFrame:CGRectMake(100, 30, 50, 30)];
[tag setNumber:i];
[tag.layer setBorderWidth:1.0];
[tag setTranslatesAutoresizingMaskIntoConstraints:NO];
[_scroller addSubview:tag];
[_tags addObject:tag];
}
//step.3 update contraints
[self myUpdateConstraints];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)myUpdateConstraints
{
[_constraint removeAllObjects];
TagView* prev = nil;
for (TagView* tag in _tags)
{
[tag setNumber:[_tags indexOfObject:tag]];
if (prev == nil)
{
[_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(<=300)-[tag]-20-|"
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:@{@"tag" : tag}]];
}
else
{
[_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"[tag]-10-[prev]"
options:0
metrics:nil
views:@{@"tag" : tag, @"prev" : prev}]];
}
[_scroller addConstraint:[NSLayoutConstraint constraintWithItem:tag
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:_scroller
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0]];
prev = tag;
}
[_scroller addConstraints:_constraint];
}
@end
//
//ViewController.m
//测试约束
//
//由stevenj于2014年3月24日创建。。
//版权所有(c)2014년 史蒂文·江。版权所有。
//
#导入“ViewController.h”
@界面标记视图:UILabel
-(void)setNumber:(NSInteger)num;
@结束
@实现TagView
-(无效)集合编号:(NSInteger)num
{
[self-setText:[NSString stringWithFormat:@“%d”,num]];
}
@结束
@界面视图控制器()
@属性(非原子,强)UIScrollView*滚动条;
@属性(非原子,强)NSMutableArray*标记;
@属性(非原子,强)NSMutableArray*约束;
@结束
@实现视图控制器
@合成卷轴=_卷轴;
-(无效)viewDidLoad
{
[超级视图下载];
_tags=[NSMutableArray new];
_约束=[NSMUTABLEARRY new];
//加载视图后,通常从nib执行任何其他设置。
//步骤1创建滚动视图
_scroller=[[UIScrollView alloc]initWithFrame:CGRectMake(0,20,320,60)];
[\u滚动条setBackgroundColor:[UIColor lightGrayColor]];
[\u滚动条移除约束:[\u滚动条约束]];
[_滚动条设置TranslatesAutoResizingMaskintoConstraints:是];
[self.view addSubview:_scroller];
//步骤2添加标记视图
对于(int i=0;i来说,听起来更好的方法可能是使用UICollectionView
。如果您想从右侧启动它,您可以执行以下操作:
NSIndexPath *lastIndex = [NSIndexPath indexPathForItem:data.count - 1
inSection:0];
[self.collectionView scrollToItemAtIndexPath:lastIndex
atScrollPosition:UICollectionViewScrollPositionRight
animated:NO];
通过这种方式,UICollectionViewFlowLayout
可以为您处理放置。UIScrollView
这种行为的原因是您忘记将最后一个元素(#4)的trailingAnchor
附加到滚动视图的trailingAnchor
滚动视图和元素#1的
前导角
彼此相连(见下面的绿色部分)。然而,滚动视图的内容矩形自然延伸到正坐标方向,从原点(0,0)
到右下(+x,+y)
。在您的情况下,滚动视图的内容大小为宽度0
,因为滚动视图的引导锚
和拖曳锚
之间没有任何内容
因此,在[\u滚动条添加约束:\u约束];
下面添加类似(伪代码)的内容:
你的问题是LTR视图应该是LTR,RTL视图应该是RTL…但对于RTL,你得到的是LTR视图这是问题吗?不,它是混合的,当我在RTL上时,添加的视图(标记)是镜像的,它们是RTL。视图的顺序也是RTL,第二个是从左到第一个,依此类推。但是contentView是LTR,它从滚动视图的左边缘开始,然后再进一步。[[tag3][tag2][tag1](滚动视图的左边缘)-----(滚动视图的右边缘)]在这种情况下是否尝试反转数组(假设原稿是1 2 3,使其显示为3 2 1)是的,我没有,它没有得到我想要的结果。它是从滚动视图的左边缘向右开始的,应该是第一个视图,在滚动视图的中间,它应该在右边,从那里开始向左。你能显示一个粗略的屏幕吗?你想要它对我来说是不起作用的,因为我需要和CON一起工作。在屏幕上同时显示多个视图,而不是分页。这是不必要的,请参阅下面我文章中的实际问题。
if tag == lastTag {
NSLAyoutconstraints.activate([
tag.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor)
])
}