Skip to content

Instantly share code, notes, and snippets.

@chrisbrandow
Created October 22, 2014 20:56
Show Gist options
  • Save chrisbrandow/a12ada684c9ec75ce00c to your computer and use it in GitHub Desktop.
Save chrisbrandow/a12ada684c9ec75ce00c to your computer and use it in GitHub Desktop.
This creates an array of evenly distributed buttons that are sized and spaced such that the spacing + buttonWidth = 1.618*buttonWidth
+ (NSArray *)evenlySpacedGoldenRatioButtonsWith:(NSInteger)numberOfButtons width:(CGFloat)spaceWidth yPos:(CGFloat)spaceHeight {
//this gives position in purely frame Math way
//an autolayout method should be made -- trying to think how I want to implement that -- method that takes view and buttons -- block?
CGFloat buttonWidth = spaceWidth/(1.303*(CGFloat)numberOfButtons + 0.3083);
CGFloat buttonSpacing = 0.3083*buttonWidth;
NSMutableArray *buttons = [NSMutableArray new];
for (NSInteger i = 0; i < numberOfButtons; i++) {
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
CGFloat x = floor(buttonSpacing + (CGFloat)i*(buttonSpacing + buttonWidth));
aButton.frame = CGRectMake(x, spaceHeight, buttonWidth, buttonWidth);
// Basic Default Colors
aButton.backgroundColor = [UIColor darkGrayColor];
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[aButton setTitleColor:[[UIColor whiteColor] colorWithAlphaComponent:.65] forState:UIControlStateHighlighted];
[buttons addObject:aButton];
}
return [NSArray arrayWithArray:buttons];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment