Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ios自动布局视觉格式-对齐_Ios_Cocoa_Autolayout - Fatal编程技术网

ios自动布局视觉格式-对齐

ios自动布局视觉格式-对齐,ios,cocoa,autolayout,Ios,Cocoa,Autolayout,在iOS6中,我使用的是自动布局 我有两个视图v1和v2编程创建 v2作为子视图添加到v1 v1的约束已通过编程方式创建(此处未显示) 我希望v1和v2的大小相同,我希望v2正好位于v1的顶部,水平和垂直中心匹配,这样v2正好位于v1的顶部 问题: 我如何才能做到这一点(最好使用视觉格式,如果不可能,您能建议其他方法吗) 代码-下面给出的是我的尝试,但它没有提供所需的输出。-没有抛出错误。V1位置正确,但V2位于V1的左角 注意-我在下面的评论中解释了我要做的事情。 [v1 addSub

在iOS6中,我使用的是自动布局

  • 我有两个视图v1和v2编程创建
  • v2作为子视图添加到v1
  • v1的约束已通过编程方式创建(此处未显示)
  • 我希望v1和v2的大小相同,我希望v2正好位于v1的顶部,水平和垂直中心匹配,这样v2正好位于v1的顶部
问题:

  • 我如何才能做到这一点(最好使用视觉格式,如果不可能,您能建议其他方法吗)
代码-下面给出的是我的尝试,但它没有提供所需的输出。-没有抛出错误。V1位置正确,但V2位于V1的左角

注意-我在下面的评论中解释了我要做的事情。

[v1 addSubView:v2];

v1.translatesAutoresizingMaskIntoConstraints        = NO;
v2.translatesAutoresizingMaskIntoConstraints        = NO;

NSDictionary *viewDictionary = NSDictionaryOfVariableBindings(v1,
                                                              v2);

//I wanted v1 and v2 to be of the same height and wanted the center Y to be aligned together
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[v2(==v1)]"
                                                                         options:NSLayoutFormatAlignAllCenterY
                                                                         metrics:nil
                                                                           views:viewDictionary];


//I wanted v1 and v2 to be of the same height and wanted the center X to be aligned together
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[v2(==v1)]"
                                                                       options:NSLayoutFormatAlignAllCenterX
                                                                       metrics:nil
                                                                         views:viewDictionary];

[v1 addConstraints:horizontalConstraints];
[v1 addConstraints:verticalConstraints];

这不应该奏效吗

NSMutableArray *constraints = [NSMutableArray new];

constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|[v2]|"
                                                                        options:0
                                                                        metrics:nil
                                                                          views:NSDictionaryOfVariableBindings(v2)];
constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[v2]|"
                                                                        options:0
                                                                        metrics:nil
                                                                          views:NSDictionaryOfVariableBindings(v2)];
[v1 addConstraints:constraints];
这样做的目的是使v2将其高度和宽度拉伸到其superview的大小,在本例中为v1,并将其放置在superview的右上方

在您的情况下,似乎不需要对齐视图centerX和centerY。否则,我无法正确理解你问题的背景