代码之家  ›  专栏  ›  技术社区  ›  Stephan

如何使UItextView填充剩余空间

  •  0
  • Stephan  · 技术社区  · 6 年前

    我有一个 UIButton UITextField 并排,如下。

    enter image description here

    我希望“添加”按钮仅具有基于内容的宽度。文本视图将占据其余空间。我有以下自动布局约束。

    private func setupLayout() {
        newDeviceIdTextField.topAnchor.constraint(equalTo: deviceIdLabel.bottomAnchor, constant: 12).isActive = true
        newDeviceIdTextField.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 12).isActive = true
    
        newDeviceIdButton.topAnchor.constraint(equalTo: deviceIdLabel.bottomAnchor, constant: 12).isActive = true
        newDeviceIdButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -12).isActive = true
        newDeviceIdButton.leadingAnchor.constraint(equalTo: newDeviceIdTextField.trailingAnchor, constant: 0).isActive = true
    }
    

    this 文章中有一个部分我相信能解决我的问题。但我不明白怎么做。

    我这里缺什么?

    2 回复  |  直到 6 年前
        1
  •  3
  •   matt    6 年前

    按钮自动调整宽度以适应其内容,文本字段和按钮都有自动高度。所以你想做的是微不足道的:

    将文本字段的左侧固定到所需位置。

    把按钮的右边别在你想要的地方。

    把两个顶部都钉在你想要的地方。

    将文本字段的右侧固定到按钮的左侧,并将该常量设置为一个较小的数字,如8。

        2
  •  0
  •   Pratik Prajapati    6 年前

    你可以这样做而不需要编写代码,从故事板。我为这种用户界面所做的是,

    1. 将文本字段的左侧固定到所需位置。

    2. 把按钮的右边别在你想要的地方。

    3. 把两个顶部都钉在你想要的地方。

    4. 将文本字段的右侧固定到按钮的左侧

    如下图所示

    enter image description here

    现在,在“内容拥抱优先级”中选择“大小检查器”按钮

    将水平更改为750(高),然后完成:)

    enter image description here

    结果如下图所示

    enter image description here