【问题标题】:Constraint "width equals height" in interface builder, for the same view: how to create such a constraint?界面生成器中的约束“宽度等于高度”,对于同一视图:如何创建这样的约束?
【发布时间】:2014-10-25 09:41:47
【问题描述】:

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

有可能吗?

【问题讨论】:

    标签: ios macos interface-builder nslayoutconstraint


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
      
          box.translatesAutoresizingMaskIntoConstraints = NO;
          [box addConstraint:
           [NSLayoutConstraint constraintWithItem:box
                                        attribute:NSLayoutAttributeWidth
                                        relatedBy:NSLayoutRelationEqual
                                           toItem:box
                                        attribute:NSLayoutAttributeHeight
                                       multiplier:1
                                         constant:0]];
      }
      

      【讨论】:

        猜你喜欢
        • 2013-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多