Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.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 “约束”;“宽度等于高度”;在interface builder中,对于相同的视图:如何创建这样的约束?_Ios_Macos_Interface Builder_Nslayoutconstraint - Fatal编程技术网

Ios “约束”;“宽度等于高度”;在interface builder中,对于相同的视图:如何创建这样的约束?

Ios “约束”;“宽度等于高度”;在interface builder中,对于相同的视图:如何创建这样的约束?,ios,macos,interface-builder,nslayoutconstraint,Ios,Macos,Interface Builder,Nslayoutconstraint,我想为同一视图创建一个约束“宽度等于高度”(因此,我的视图将为方形)。中给出的方法无效,因为它不是两个不同视图之间的约束 有可能吗?控制+从视图拖动到自身,然后将纵横比设置为1:1。在Interface Builder中设置一个窗口,以包含NSBox并将所有侧面的约束设置为标准值。然后将{IBOutlet NSBox*box;}添加到IB中的AppDelegate.h&将盒子插座连接到盒子。在AppDelegate.m中,将以下内容添加到applicationdFinishLaunching&运

我想为同一视图创建一个约束“宽度等于高度”(因此,我的视图将为方形)。中给出的方法无效,因为它不是两个不同视图之间的约束


有可能吗?

控制+从视图拖动到自身,然后将纵横比设置为1:1。

在Interface Builder中设置一个窗口,以包含NSBox并将所有侧面的约束设置为标准值。然后将
{IBOutlet NSBox*box;}
添加到IB中的
AppDelegate.h
&将盒子插座连接到盒子。在
AppDelegate.m
中,将以下内容添加到
applicationdFinishLaunching
&运行代码。我想这就是你想要的。如果要以编程方式添加约束,请确保添加足够的高度和宽度约束以指定所需内容。除了其他约束之外,只需添加此类约束

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    box.translatesAutoresizingMaskIntoConstraints = NO;
    [box addConstraint:
     [NSLayoutConstraint constraintWithItem:box
                                  attribute:NSLayoutAttributeWidth
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:box
                                  attribute:NSLayoutAttributeHeight
                                 multiplier:1
                                   constant:0]];
}