为什么在访问titleLabel属性时UIButton会调整大小? [英] Why does UIButton resize when I access titleLabel property?

查看:106
本文介绍了为什么在访问titleLabel属性时UIButton会调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将按钮的大小调整为(它的内在大小+更多一点)以绘制自定义背景。但是,每次我在按钮内访问self.titleLabel时,大小和位置都会重新转换为故事板的大小和位置。我没有对标签做任何事情来重现这一点,只需从按钮中检索它。

I'm trying to adjust the size of a button to (it's intrinsic size + a little bit more) in order to draw a custom background. However, every time I access self.titleLabel within the button, the size and position resnaps to that of the storyboard. I don't have to do anything with the label to reproduce this, just retrieve it from the button.

我把记录代码全部放在我的按钮上并查看控制器为了找到这种情况发生的地方。它不是来自子视图的中继或我在视图控制器中看到的任何其他通知。访问titleLabel之前的行,位置和大小都是正确的。之后,它重新回到故事板位置。注释掉访问可以防止大小/位置捕捉。有人能告诉我这种情况发生的原因或原因吗?

I've put logging code all over my button and view controller in order to find where this is happening. It's not coming from a relaying-out of subviews or any other notification I see to get within the view controller. The line before accessing titleLabel, the position and size are correct. The line after, it has snapped back to the storyboard position. Commenting out the access prevents the size/position snapping. Can someone tell me where or why this is happening?

我没有设置约束(我可以告诉),但我是否在这里反对自动布局?我是否应该采用不同的方式来组合控件或其他方式?

I have no constraints set (that I can tell), but am I fighting against auto-layout here? Should I be doing this a different way like composing controls or something?

UIButton修改标题标签似乎改变了它的框架 UIButton titleLabel在新闻时调整大小?,但两者都没有得到答复或解释为可能是一个错误。

Something similar (or the same?) has been asked before at UIButton modifying titlelabel seems to change its frame and UIButton titleLabel resizes on press?, but both were left unanswered or explained away with just "maybe a bug."

推荐答案

如果项目启用了自动布局,那么YES就是在进行自动布局。您有两个选择,子类UIButton,以便您可以覆盖内在大小计算,或修改约束,以便在任何约束中不使用内在大小。如果您执行后者,那么您可能希望为宽度约束创建一个IBOutlet,以便您可以根据需要调整常量属性。

If the project has auto-layout enabled, then YES, you're fighting auto-layout. You have two choices, either subclass UIButton so that you can override the intrinsic size calculation, or modify the constraints so that the intrinsic size is not used in any constraint. If you do the latter, then you probably want to create an IBOutlet to the constraint for the width, so that you can adjust the constant property as needed.

这篇关于为什么在访问titleLabel属性时UIButton会调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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