UIButton titleLabel frame size returning CGSize with zero width & height

前端 未结 6 460
执笔经年
执笔经年 2021-02-01 23:16

For iOS7, I used the UIButton titleLabel.frame.size.width property to determine the width of my button title under different localisations so I could p

6条回答
  •  庸人自扰
    2021-02-01 23:48

    I resolved it. App run on iOS8, build by Xcode 6, not update frame right after UIButton setTitle, setTitleEdgeInsets. It will wait for next UI update. So, if you get frame of titleLabel, you will get CGRectZero.

    Solution:

    1. Call below methods after set UI property, to update layout immediately:

      [self setNeedsLayout];
      [self layoutIfNeeded];

    Or:

    1. Delay a second, and you can get titleFrame, use self.titleLabel.frame.

    Or:

    1. dispatch_async in main queue, to move code to next queue

提交回复
热议问题