Xcode 视图重叠但不堆叠
我对编程相当陌生 屏幕有两个垂直列和3个框。如果右栏中的框比左栏中的框少,则框将转到左栏。每个框都有几个子视图。 在我的代码中,“yearBOX”和“dayBOX”将位于左栏。问题是“dayBOX”只是重叠在年份框上,而不是堆叠在下面。我已经检查了文件,玩了大约10个小时,但找不到任何解决方案。欢迎使用Swift或Objective-C代码。我试图设置TopAnchord,但没有成功。非常感谢 下面是完整的代码: 已编辑:删除了一些未使用的行。并添加了一些关键代码来解决问题 [boxStack.topAnchor constraintEqualToAnchor:dayBox.topAnchor].active=YES; [dayBox.bottomAnchor constraintEqualToAnchor:boxStack.bottomAnchor].active=YES; [boxStack.topAnchor constraintEqualToAnchor:dayBox.topAnchor].active=YES; [dayBox.bottomAnchor constraintEqualToAnchor:boxStack.bottomAnchor].active=YESXcode 视图重叠但不堆叠,xcode,autolayout,uistackview,Xcode,Autolayout,Uistackview,我对编程相当陌生 屏幕有两个垂直列和3个框。如果右栏中的框比左栏中的框少,则框将转到左栏。每个框都有几个子视图。 在我的代码中,“yearBOX”和“dayBOX”将位于左栏。问题是“dayBOX”只是重叠在年份框上,而不是堆叠在下面。我已经检查了文件,玩了大约10个小时,但找不到任何解决方案。欢迎使用Swift或Objective-C代码。我试图设置TopAnchord,但没有成功。非常感谢 下面是完整的代码: 已编辑:删除了一些未使用的行。并添加了一些关键代码来解决问题 [boxStack.
- (void)viewDidLoad {
CGRect watchersRect = CGRectMake(0, 20, 320, 460);
UIView *colorLayer = [[UIView alloc]initWithFrame:watchersRect];
colorLayer.backgroundColor = [UIColor grayColor];
[self.view addSubview:colorLayer];
UIStackView *watchersUI = [[UIStackView alloc]init];
NSMutableArray *leftArr=[[NSMutableArray alloc]init];
NSMutableArray *rightArr=[[NSMutableArray alloc]init];
NSMutableArray *yearStackUIArr = [[NSMutableArray alloc]initWithObjects:@"year 1", @"year2",@"year3",@"year4",@"year5",nil];
NSMutableArray *monthStackUIArr = [[NSMutableArray alloc]initWithObjects:@"month 1", @"month 2",@"month 3",nil];
NSMutableArray *dayStackUIArr = [[NSMutableArray alloc]initWithObjects:@"day1", @"day2",@"day3",nil];
watchersUI = [[UIStackView alloc] initWithFrame:watchersRect];
watchersUI.axis= UILayoutConstraintAxisHorizontal;
watchersUI.distribution = UIStackViewDistributionFillEqually;
watchersUI.alignment = UIStackViewAlignmentFill;
watchersUI.spacing = 2;
watchersUI.translatesAutoresizingMaskIntoConstraints = YES;
[colorLayer addSubview:watchersUI];
UIView *leftColumn = [[UIView alloc] init];
[leftColumn setBackgroundColor:[UIColor yellowColor]];
UIView *rightColumn = [[UIView alloc]init];
[rightColumn setBackgroundColor:[UIColor redColor]];
[watchersUI addArrangedSubview:leftColumn];
[watchersUI addArrangedSubview:rightColumn];
[leftColumn.leftAnchor constraintEqualToAnchor:watchersUI.leftAnchor].active = YES;
[rightColumn.rightAnchor constraintEqualToAnchor:watchersUI.rightAnchor].active = YES;
[leftColumn.bottomAnchor constraintEqualToAnchor:watchersUI.bottomAnchor].active = YES;
[rightColumn.bottomAnchor constraintEqualToAnchor:watchersUI.bottomAnchor].active = YES;
UIStackView *leftStack =[[UIStackView alloc]init];
leftStack.axis = UILayoutConstraintAxisVertical;
leftStack.distribution = UIStackViewDistributionFillProportionally;
leftStack.alignment=UIStackViewAlignmentLeading;
leftStack.spacing =2;
leftStack.translatesAutoresizingMaskIntoConstraints = NO;
[leftColumn addSubview:leftStack];
[leftStack.leftAnchor constraintEqualToAnchor:leftColumn.leftAnchor].active =YES;
[leftStack.rightAnchor constraintEqualToAnchor:leftColumn.rightAnchor].active =YES;
UIStackView *rightStack =[[UIStackView alloc]init];
rightStack.axis = UILayoutConstraintAxisVertical;
rightStack.distribution = UIStackViewDistributionFillProportionally;
rightStack.alignment=UIStackViewAlignmentLeading;
rightStack.spacing =2;
rightStack.translatesAutoresizingMaskIntoConstraints = NO;
[rightColumn addSubview:rightStack];
[rightStack.leftAnchor constraintEqualToAnchor:rightColumn.leftAnchor].active =YES;
[rightStack.rightAnchor constraintEqualToAnchor:rightColumn.rightAnchor].active =YES;
UIView *monthBox=[[UIView alloc]init];
monthBox.backgroundColor =[UIColor blackColor];
UIView *yearBox=[[UIView alloc]init];
yearBox.backgroundColor = [UIColor purpleColor];
UIView *dayBox=[[UIView alloc]init];
dayBox.backgroundColor =[UIColor brownColor];
if ([yearStackUIArr count]>0) {
if ([rightArr count]<[leftArr count]) {
[rightStack addArrangedSubview:yearBox];
[rightArr addObject:yearBox];
[yearBox.topAnchor constraintEqualToAnchor:rightColumn.topAnchor].active = YES;
[yearBox.leftAnchor constraintEqualToAnchor:rightColumn.leftAnchor].active = YES;
[yearBox.rightAnchor constraintEqualToAnchor:rightColumn.rightAnchor].active = YES;
}
else
{
[leftStack addArrangedSubview:yearBox];
[leftArr addObject:yearBox];
for (UIView *subs in leftStack.subviews) {
NSLog(@"yearBox leftStack %@",subs);
}
[yearBox.topAnchor constraintEqualToAnchor:leftColumn.topAnchor].active = YES;
[yearBox.leftAnchor constraintEqualToAnchor:leftColumn.leftAnchor].active = YES;
[yearBox.rightAnchor constraintEqualToAnchor:leftColumn.rightAnchor].active = YES;
}
UIStackView *boxStack = [[UIStackView alloc]init];
boxStack.axis = UILayoutConstraintAxisVertical;
boxStack.distribution = UIStackViewDistributionFillEqually;
boxStack.alignment=UIStackViewAlignmentLeading;
boxStack.spacing=1;
boxStack.translatesAutoresizingMaskIntoConstraints = NO;
for (NSString *innerItem in yearStackUIArr) {
UILabel *innerView = [[UILabel alloc]init];
[boxStack addArrangedSubview:innerView];
[innerView.leftAnchor constraintEqualToAnchor:boxStack.leftAnchor].active =YES;
[innerView.rightAnchor constraintEqualToAnchor:boxStack.rightAnchor].active =YES;
[innerView.heightAnchor constraintGreaterThanOrEqualToConstant:20].active =YES;
innerView.backgroundColor =[UIColor cyanColor];
innerView.text = innerItem;
innerView.font = [UIFont systemFontOfSize:10];
}
[yearBox addSubview:boxStack];
[boxStack.leftAnchor constraintEqualToAnchor:yearBox.leftAnchor].active = YES;
[boxStack.rightAnchor constraintEqualToAnchor:yearBox.rightAnchor].active = YES;
[boxStack.topAnchor constraintEqualToAnchor:yearBox.topAnchor].active = YES;
[yearBox.bottomAnchor constraintEqualToAnchor:boxStack.bottomAnchor].active = YES;
}
if ([monthStackUIArr count]>0) {
if ([rightArr count]<[leftArr count]) {
[rightStack addArrangedSubview:monthBox];
[rightArr addObject:monthBox];
[monthBox.leftAnchor constraintEqualToAnchor:rightColumn.leftAnchor].active = YES;
[monthBox.rightAnchor constraintEqualToAnchor:rightColumn.rightAnchor].active = YES;
}
else
{
[leftStack addArrangedSubview:monthBox];
[leftArr addObject:monthBox];
[monthBox.leftAnchor constraintEqualToAnchor:leftColumn.leftAnchor].active = YES;
[monthBox.rightAnchor constraintEqualToAnchor:leftColumn.rightAnchor].active = YES;
}
UIStackView *boxStack = [[UIStackView alloc]init];
boxStack.axis = UILayoutConstraintAxisVertical;
boxStack.distribution = UIStackViewDistributionFillEqually;
boxStack.alignment=UIStackViewAlignmentLeading;
boxStack.spacing=2;
boxStack.translatesAutoresizingMaskIntoConstraints = NO;
for (NSString *innerItem in monthStackUIArr) {
UILabel *innerView = [[UILabel alloc]init];
[boxStack addArrangedSubview:innerView];
[innerView.leftAnchor constraintEqualToAnchor:boxStack.leftAnchor].active =YES;
[innerView.rightAnchor constraintEqualToAnchor:boxStack.rightAnchor].active =YES;
[innerView.heightAnchor constraintGreaterThanOrEqualToConstant:20].active =YES;
innerView.backgroundColor =[UIColor whiteColor];
innerView.text = innerItem;
innerView.font = [UIFont systemFontOfSize:10];
}
[monthBox addSubview:boxStack];
[boxStack.leftAnchor constraintEqualToAnchor:monthBox.leftAnchor].active = YES;
[boxStack.rightAnchor constraintEqualToAnchor:monthBox.rightAnchor].active = YES;
[boxStack.topAnchor constraintEqualToAnchor:monthBox.topAnchor].active=YES;
[monthBox.bottomAnchor constraintEqualToAnchor:boxStack.bottomAnchor].active = YES;
}
if ([dayStackUIArr count]>0) {
if ([rightArr count]<[leftArr count]) {
[rightStack addArrangedSubview:dayBox];
[rightArr addObject:dayBox];
[dayBox.leftAnchor constraintEqualToAnchor:rightColumn.leftAnchor].active = YES;
[dayBox.rightAnchor constraintEqualToAnchor:rightColumn.rightAnchor].active = YES;
}
else
{
[leftStack addArrangedSubview:dayBox];
[leftArr addObject:dayBox];
[dayBox.leftAnchor constraintEqualToAnchor:leftColumn.leftAnchor].active = YES;
[dayBox.rightAnchor constraintEqualToAnchor:leftColumn.rightAnchor].active = YES;
}
UIStackView *boxStack = [[UIStackView alloc]init];
boxStack.axis = UILayoutConstraintAxisVertical;
boxStack.distribution = UIStackViewDistributionFillEqually;
boxStack.alignment=UIStackViewAlignmentLeading;
boxStack.spacing=2;
boxStack.translatesAutoresizingMaskIntoConstraints = NO;
[dayBox addSubview:boxStack];
for (NSString *innerItem in dayStackUIArr) {
UILabel *innerView = [[UILabel alloc]init];
[boxStack addArrangedSubview:innerView];
[innerView.leftAnchor constraintEqualToAnchor:boxStack.leftAnchor].active =YES;
[innerView.rightAnchor constraintEqualToAnchor:boxStack.rightAnchor].active =YES;
[innerView.heightAnchor constraintGreaterThanOrEqualToConstant:20].active =YES;
innerView.backgroundColor =[UIColor blueColor];
innerView.text = innerItem;
innerView.font = [UIFont systemFontOfSize:10];
}
[boxStack.leftAnchor constraintEqualToAnchor:dayBox.leftAnchor].active = YES;
[boxStack.rightAnchor constraintEqualToAnchor:dayBox.rightAnchor].active = YES;
[boxStack.topAnchor constraintEqualToAnchor:dayBox.topAnchor].active=YES;
[dayBox.bottomAnchor constraintEqualToAnchor:boxStack.bottomAnchor].active = YES;
}
}
}
-(void)viewDidLoad{
CGRect watchersRect=CGRectMake(0,20,320,460);
UIView*colorLayer=[[UIView alloc]initWithFrame:WatcherRect];
colorLayer.backgroundColor=[UIColor grayColor];
[self.view addSubview:colorLayer];
UIStackView*watchersUI=[[UIStackView alloc]init];
NSMUTABLEARRY*leftArr=[[NSMUTABLEARRY alloc]init];
NSMutableArray*rightArr=[[NSMutableArray alloc]init];
NSMutableArray*yearStackUIArr=[[NSMutableArray alloc]initWithObjects:@“第1年”、“第2年”、“第3年”、“第4年”、“第5年”、“无];
NSMutableArray*monthStackUIArr=[[NSMutableArray alloc]initWithObjects:@“第1个月”、“第2个月”、“第3个月”、“无];
NSMUTABLEARRY*dayStackUIArr=[[NSMUTABLEARRY alloc]initWithObjects:@“day1”,“day2”,“day3”,nil];
watchersUI=[[UIStackView alloc]initWithFrame:watchersRect];
watchersUI.axis=UILayoutConstraintAshorizontal;
watchersUI.distribution=UIStackViewDistributionAllegly;
watchersUI.alignment=UIStackViewAlignmentFill;
watchersUI.spating=2;
watchersUI.translatesAutoresizingMaskIntoConstraints=是;
[colorLayer addSubview:WatcherUI];
UIView*leftColumn=[[UIView alloc]init];
[leftColumn setBackgroundColor:[UIColor yellowColor]];
UIView*rightColumn=[[UIView alloc]init];
[rightColumn setBackgroundColor:[UIColor redColor]];
[watchersUI addArrangedSubview:leftColumn];
[watchersUI addArrangedSubview:rightColumn];
[leftColumn.leftAnchor constraintEqualToAnchor:watchersUI.leftAnchor].active=YES;
[rightColumn.rightAnchor constraintEqualToAnchor:watchersUI.rightAnchor].active=YES;
[leftColumn.bottomAnchor constraintEqualToAnchor:watchersUI.bottomAnchor].active=YES;
[rightColumn.bottomAnchor constraintEqualToAnchor:watchersUI.bottomAnchor].active=YES;
UIStackView*leftStack=[[UIStackView alloc]init];
leftStack.axis=UILayoutConstraintaxVertical;
leftStack.distribution=UIStackViewDistributionFill按比例分配;
leftStack.alignment=UIStackViewAlignmentLeading;
leftStack.spacing=2;
leftStack.TranslatesAutoResizezingMaskintoConstraints=否;
[leftColumn addSubview:leftStack];
[leftStack.leftAnchor constraintEqualToAnchor:leftColumn.leftAnchor].active=YES;
[leftStack.rightAnchor constraintEqualToAnchor:leftColumn.rightAnchor].active=YES;
UIStackView*rightStack=[[UIStackView alloc]init];
rightStack.axis=UILayoutConstraintaxVertical;
rightStack.distribution=UIStackViewDistributionFill按比例分配;
rightStack.alignment=UIStackViewAlignmentLeading;
rightStack.spacing=2;
rightStack.TranslatesAutoResizezingMaskintoConstraints=否;
[rightColumn添加子视图:rightStack];
[rightStack.leftAnchor constraintEqualToAnchor:rightColumn.leftAnchor].active=YES;
[rightStack.rightAnchor constraintEqualToAnchor:rightColumn.rightAnchor].active=YES;
UIView*monthBox=[[UIView alloc]init];
monthBox.backgroundColor=[UIColor blackColor];
UIView*yearBox=[[UIView alloc]init];
yearBox.backgroundColor=[UIColor purpleColor];
UIView*dayBox=[[UIView alloc]init];
dayBox.backgroundColor=[UIColor brownColor];
如果([yearStackUIArr count]>0){
如果([rightArr计数]0){
如果([rightArr计数]0){
if([rightArr count]这是一个很好的问题,正在尝试调试。嘿,你的部分问题是你遇到的自动布局约束错误。我刚刚解决了这个问题。但问题还不是核心问题。因此,在你的if([dayStackUIArr count]>0)
中,然后在你的中,如果中。更改[dayBox.heightAnchor约束创建AnorEqualToAnchor:margin.heightAnchor].active=YES;
至[dayBox.heightAnchor约束创建AnorEqualToAnchor:margin.heightAnchor].active=YES;
这将消除自动布局错误Shi Akshansh。感谢您指出这一点。我认为我需要设置“innerView.topAnchor”以使其正确。我不知道为什么,但我只是发现这些innerView不会粘贴或附加到其父StackView。我想这就是原因。我会在收到通知后再次更新代码解决问题。再次感谢你。威廉,我试图操纵代码,但似乎没有任何帮助。我会继续尝试。但是,如果你找到解决方案,请发布解决方案。我非常感兴趣,并会关注它。祝你好运!嗨,阿肯什。神奇的事情发生在我在年鉴框中添加了一个底部锚之后,它als是boxstack子视图。也是dayBox的uistackview boxstack子视图的topAnchor。看来这些Anchor对于自动布局至关重要。非常感谢。