Why does UIButton resize when I access titleLabel property?

后端 未结 2 348
心在旅途
心在旅途 2021-01-28 21:46

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 withi

相关标签:
2条回答
  • 2021-01-28 22:24

    This isn't a bug, it's a consequence of auto layout. When using auto layout, you shouldn't set any frames. Instead, you should change the size or position by modifying the constraints. What's happening, is that whenever the view needs to be redrawn, the frame reverts to the frame that's defined by the constraints.

    0 讨论(0)
  • 2021-01-28 22:39

    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.

    0 讨论(0)
提交回复
热议问题