【问题标题】:How to stack + center two text views using programatic constraints?如何使用编程约束堆叠+居中两个文本视图?
【发布时间】:2016-10-08 05:29:33
【问题描述】:

我正在尝试实现一个硬编码布局,其中两个文本视图应堆叠在彼此之上并以父 UICollectionViewCell 为中心:

----------------------
|                    |
|    This is text    |
|      Also text     |
|                    |
----------------------

由于各种遗留/业务原因,我应该使用在 UICollectionViewCell 的子类中硬编码的约束来执行此操作。两个文本视图的长度可以不同,但​​应在父视图中垂直居中,同时彼此重叠。

有没有一种简单的方法可以在约束中表达这一点?我对这种类型的布局系统有点陌生,所以任何帮助都非常感谢!

我正在使用的应用程序也使用 Masonry (https://github.com/SnapKit/Masonry) 库,如果这样可以让事情变得更容易的话。

【问题讨论】:

    标签: ios constraints masonry-ios-osx


    【解决方案1】:

    假设标签被命名为textView1textView2

    您需要设置一个约束,将textView1 水平居中设置为superviewUICollectionViewCell),然后将textView2textView1 居中(您也可以将superview 居中)和您将同时居中。

    为了让它相互叠加,您必须设置一个约束,将textView2 顶部设置为textView1 底部。

    从未使用过 Masonry,但看起来您需要有这些限制:

    [textView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        //Center first textView in the superview
        make.centerX.equalTo(superview); 
    }];
    [textView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        //Center second textView with the first one 
        make.centerX.equalTo(textView1);
        //Set second textView to be below the first one
        make.top.equalTo(textView1.mas_bottom);
    }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-21
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 2016-10-01
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      相关资源
      最近更新 更多