Skip to content

Instantly share code, notes, and snippets.

@jspahrsummers
Created December 12, 2012 04:23
Show Gist options
  • Save jspahrsummers/4264845 to your computer and use it in GitHub Desktop.
Save jspahrsummers/4264845 to your computer and use it in GitHub Desktop.
An example of clean CGRectDivide-based layout.
- (void)resizeSubviewsWithOldSize:(CGSize)oldSize {
[super resizeSubviewsWithOldSize:oldSize];
CGRect availableRect = CGRectInset(self.bounds, GHGitSetupViewHorizontalMargin, GHGitSetupViewVerticalMargin);
CGFloat labelWidth = fmax(CGRectGetWidth(self.nameLabel.bounds), CGRectGetWidth(self.emailLabel.bounds));
CGRect nameRect = CGRectZero;
CGRectDivideWithPadding(availableRect, &nameRect, &availableRect, CGRectGetHeight(self.nameTextField.bounds), GHGitSetupViewControlVerticalPadding, CGRectMaxYEdge);
CGRectDivideWithPadding(nameRect, self.nameLabel.frame, self.nameTextField.frame, labelWidth, GHGitSetupViewControlHorizontalPadding, CGRectMinXEdge);
CGRect emailRect = CGRectZero;
CGRectDivideWithPadding(availableRect, &emailRect, &availableRect, CGRectGetHeight(self.emailTextField.bounds), GHGitSetupViewControlVerticalPadding, CGRectMaxYEdge);
CGRectDivideWithPadding(emailRect, self.emailLabel.frame, self.emailTextField.frame, labelWidth, GHGitSetupViewControlHorizontalPadding, CGRectMinXEdge);
CGRectDivideWithPadding(availableRect, NULL, self.explanatoryLabel.frame, labelWidth, GHGitSetupViewControlHorizontalPadding, CGRectMinXEdge);
}
@jspahrsummers
Copy link
Author

Half the length of the equivalent Auto Layout code, easier to understand (once you grok the one main function being used), and easier to debug if it's wrong.

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