UIButton titleLabel frame size returning CGSize with zero width & height

前端 未结 6 448
执笔经年
执笔经年 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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-01 23:42

    If you are then setting an image next to a button after getting the frame, you can use

    [theButton setTitle: @"theTitle" forState:UIControlStateNormal];
    [theButton setImage: theImage forState:UIControlStateNormal];
    [theButton setNeedsLayout];
    [theButton layoutIfNeeded];
    [theButton setImageEdgeInsets: UIEdgeInsetsMake(6, transactionsButton.titleLabel.frame.size.width + 40.0, 0, 0)];
    

    And in Swift 3, this will do.

    self.view.layoutIfNeeded()
    

提交回复
热议问题