自动布局调整基于文本按钮和文本框都填充可用空间 [英] Autolayout resize button based on text and have textfield fill available space

查看:150
本文介绍了自动布局调整基于文本按钮和文本框都填充可用空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的自动布局在iOS应用程序的工作,

I am working on an ios application using autolayout,

我有一个的UITextField和一个UIButton旁边水平。

I have a UITextField and a UIButton next to it horizontally.

该文本的UIButton是动态变化的。它可以是非常短(3个字母)或很长(15个字母)

The UIButton text is changed dynamically. It can be very short (3 letters) or very long (15 letters)

我需要添加约束,以实现以下目标:

I need to add constraint to achieve the following:

1),当我改变的UIButton的文字,我需要的按钮来调整大小以适应新的文本

1) when I change the UIbutton text, I need the button to resize to fit the new text

2)的UITextField应该调整采取的按钮上的文字变化后留下的可用空间

2) the UITextField should resize to take the available space left after the button text change

我试着做以下(如图片所示):

I tried to do the following (like shown in the picture):


  • 增加了对的UITextView领先制约了上海华20

  • Added a leading constraint for the UITextView to the superview 20

增加了对UIButton的尾部约束到上海华20

Added a trailing constraint for the UIButton to the superview 20

增加了一个固定的他们之间遥远的10

Added a fixed distant between them 10

但现在我卡在如何使UIButton的与文本和文本字段取可用大小调整。

but now I'm stuck on how to make the UIButton resize with the text and the textfield to take the available size.

任何帮助是pciated AP $ P $

Any help is appreciated

感谢您

推荐答案

尝试这些自动布局约束:

Try these autolayout constraints:


  1. 的UITextField 对象的宽度约束应该是小于或等于关于

  2. 的UIButton 对象的宽度约束应该是大于或等于关于

  3. 的UITextField 对象的水平内容拥抱优先应该是249(按我的约束设置上面显示

  1. UITextField object's width constraint should be a Less Than or Equal relation
  2. UIButton object's width constraint should be a Greater Than or Equal relation
  3. UITextField object's horizontal Content Hugging Priority should be 249 (as per my constraint settings shown above)

和相关code为:

[buttonObject setTitle:@"Button title is too... zzz" forState:UIControlStateNormal];
[buttonObject sizeToFit];


...或东西沿着这些路线

这篇关于自动布局调整基于文本按钮和文本框都填充可用空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆