UIButton
subview when the button is not in the window hierarchy will need to send layoutIfNeeded
to the button before retrieving layout information (such as button.titleLabel.frame
) to ensure that the layout values are up to date.For example, if you had something like this:
舉例如下,如果iOS8.3以前,你這樣寫代碼:
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
// code that sets up the button, but doesn’t yet add it to a window
CGRect titleFrame = button.titleLabel.frame;
// code that relies on the correct value for titleFrameYou now need: 那麼,現在你需要這樣寫代碼:
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
// code that sets up the button, but doesn’t yet add it to a window
; // This is also safe pre-iOS 8.3
CGRect titleFrame = button.titleLabel.frame;
// code that relies on the correct value for titleFrame