Skip to content

Instantly share code, notes, and snippets.

@jverkoey
Last active May 16, 2020 02:51
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jverkoey/5288470 to your computer and use it in GitHub Desktop.
Save jverkoey/5288470 to your computer and use it in GitHub Desktop.
Layout + size calculations for iOS.
- (CGSize)sizeOfContentsWithSize:(CGSize)size
shouldLayout:(BOOL)shouldLayout {
// Calculate frames.
if (shouldLayout) {
// Update frames.
}
return // size
}
- (CGSize)sizeThatFits:(CGSize)size {
return [self sizeOfContentsWithSize:size shouldLayout:NO];
}
- (void)layoutSubviews {
[super layoutSubviews];
[self sizeOfContentsWithSize:self.bounds.size shouldLayout:YES];
}
@ianmendiola
Copy link

baddie

@Dimillian
Copy link

Nope: Chuck Testa

@taylanpince
Copy link

Why not call:

[view sizeToFit];
[view setNeedsLayout]; // If you need it

And do the calculation in sizeThatFits:

@jverkoey
Copy link
Author

@taylanpince because sizeThatFits is a question that may be asked multiple times before the eventual layout of the view occurs. Calling sizeThatFits shouldn't affect the frame of the view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment