iphone - 改变 UIButton 状态时内部尺寸不更新

标签 iphone ios objective-c uikit interface-builder

我有一个包含 UIButton 的 UIView。 UIButton 为 UIControlStateNormal(“Follow”)和 UIControlStateSelected(“Following”)状态设置了 2 个标题。我在 UIButton 上使用自动布局,它有一个约束,即距父 View 顶部一定距离,另一个约束距父 View 左侧一定距离。我还在上面使用了“大小以适合内容”。

当我通过代码将按钮设置为选中状态时,标题会正确更改,但 UIButton 的固有宽度不会更改,因此当从“跟随”更改为“跟随”时,文本会变成椭圆形。

self.selected = self.following;

enter image description here enter image description here

当我以不同的方式处理问题并在有人点击按钮时简单地更改 UIControlStateNormal 的文本时,按钮会正确更改大小。

NSString *title = (self.following) ? @"Following" : @"Follow"
[self setTitle:title forState:UIControlStateNormal];

enter image description here enter image description here

这是 UIKit 中的错误吗?我希望按钮更改其固有大小以在其状态更改时正确反射(reflect)文本的新大小,特别是因为除了 2 个按钮状态的文本之外,我还想更改其他内容。

最佳答案

正如 David Caunt 在评论中指出的那样,调用 invalidateIntrinsicContentSize 将导致自动布局调整按钮的大小以适应它的新内容。

self.selected = self.following;
[self invalidateIntrinsicContentSize];

附言。大卫,如果你想发布你的命令作为答案,我会删除我的。

关于iphone - 改变 UIButton 状态时内部尺寸不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17981918/

相关文章:

iphone - 我无法理解iOS6中的旋转机制

ios - 'UITableView' 没有可见的@interface 声明选择器 'setShowsUserLocation:'

iphone - Objective-C 私有(private)方法困境

iphone - 有没有办法简化这个 IBOutletConnection 语句?

iphone - 将类传递给函数

iphone - 强制 iPhone Web 应用程序进入横向模式

c# - 如何在MonoDevelop中使用PickerView控件(iPhone)

ios - 将大数字四舍五入到最接近的百位, float 正在失去值(value)

ios - 快速清除 JSON 缓存?

iphone - 将文本绘制成椭圆形