如何正确创建一个在Xcode 4中调整文本长度的UIButton? [英] How to properly create a UIButton that resizes to the text length in Xcode 4?

查看:114
本文介绍了如何正确创建一个在Xcode 4中调整文本长度的UIButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xcode 4中,您应该能够设计根据内容调整大小的UI界面。仍然看起来有一些关于如何获得这个的缺失部分。

In Xcode 4 you should be able to design UI interfaces that do resize based on their content. Still it looks that the are some missing pieces regarding how to obtain this.

我有一个在XIB文件中配置的按钮调整到右边,它有很多这样做的空间。

I have a button that is configured in the XIB file to resize to the right and it has plenty of space to do that.

问题是当标签更新时它没有缝合来调整按钮的大小。

The problem is that it doesn't seam to resize the button when the label is updated.

这可以通过在标签更改后添加 [button sizeToFit]; 来修复。

This can be fixed by adding a [button sizeToFit]; after the label is changed.

我仍在寻找一种解决方案,无需在代码中添加此手动步骤。

Still I am looking for a solution that works without adding this manual step into the code.

我提到有足够的空间来扩展进一步按钮,仍然 sizeToFit 接缝放大按钮但仍不足以适合整个文本。

I mention that there is enough space to expand the button further, still sizeToFit seams to to enlarge the button but still not enough to fit the entire text.

什么我错过了吗?

推荐答案

在XCode 4.5及更高版本中,现在可以使用'自动布局/限制条件'。

In XCode 4.5 and above, this can now be done by using 'Auto-layouting / Constraints'.

主要优点是:


  1. 你不需要以编程方式设置帧!

  2. 如果操作正确,则无需为了更改方向而重置帧。

  3. 此外,设备更改无需打扰您(阅读,无需针对不同的屏幕尺寸单独编码)。

一些缺点:


  1. 不向后兼容 - 仅适用于iOS 6及更高版本。

  2. 需要熟悉(但以后会节省时间)。

最酷的事情是我们专注于声明一个意图如下:

Coolest thing is we get to focus on declaring an intent such as:


  • 我希望这两个按钮具有相同的宽度;或

  • 我需要将此视图垂直居中,并从superview的边缘延伸到最大10 pts;甚至,

  • 我希望这个按钮/标签根据它显示的标签调整大小!

这是一个介绍自动布局的简单教程:
http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2

Here is a simple tutorial to get introduced to auto-layouting: http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2

有关详细信息,请访问:
http://www.techotopia .com / index.php / An_Introduction_to_Auto_Layout_in_iOS_6

For a more detailed look at things, go to: http://www.techotopia.com/index.php/An_Introduction_to_Auto_Layout_in_iOS_6

一开始需要一些时间,但看起来确实值得付出努力。

It takes some time at first, but it sure looks like it will be well worth the effort.

干杯!

这篇关于如何正确创建一个在Xcode 4中调整文本长度的UIButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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