Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
kennedy otis
3,570 PointsIOS Controls
Hi guys, Am trying to create UIScrollview and UIButton programmatically and I don't like the result so far. I would like to load some UIButton ( 10 buttons ) inside a UIScrollview and let them display horizontally inside the UIScrollview.Does anyone have sample code i can learn from.Am still newbie to ios development and i would like to learn how to implement these controls programmatically. I found some code online and tried to play around with it but still am not getting clear output. http://pastebin.com/bxf3eY9U I would really appreciate if I can get help or sample code which I can learn from.
2 Answers
Thomas Nilsen
14,957 Pointshere is an example:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
self.scrollView.contentSize = CGSizeMake(320, 580);
self.redButton = [self makeButtonsWithX:0.0f y:180.0f width:self.view.bounds.size.width height:80.0f color:[UIColor colorWithRed:0.796 green:0.282 blue:0.196 alpha:1] button:self.redButton];
self.yellowButton = [self makeButtonsWithX:0.0f y:260.0f width:self.view.bounds.size.width height:80.0f color:[UIColor colorWithRed:0.761 green:0.631 blue:0.184 alpha:1] button:self.yellowButton];
self.purpleButton = [self makeButtonsWithX:0.0f y:340.0f width:self.view.bounds.size.width height:80.0f color:[UIColor colorWithRed:0.624 green:0.208 blue:0.843 alpha:1] button:self.purpleButton];
self.greenButton = [self makeButtonsWithX:0.0f y:500.0f width:self.view.bounds.size.width height:80.0f color:[UIColor colorWithRed:0.275 green:0.714 blue:0.176 alpha:1] button:self.greenButton];
[self.view addSubview:self.scrollView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIButton *)makeButtonsWithX:(CGFloat)x y:(CGFloat)y width:(CGFloat)width height:(CGFloat)height color:(UIColor *)color button:(UIButton *)button
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(x, y, width, height);
button.backgroundColor = color;
[self.scrollView addSubview:button];
return button;
}
H-file:
@property (nonatomic, strong) UIButton *redButton;
@property (nonatomic, strong) UIButton *yellowButton;
@property (nonatomic, strong) UIButton *purpleButton;
@property (nonatomic, strong) UIButton *greenButton;
@property (nonatomic, strong) UIScrollView *scrollView;
- (UIButton *)makeButtonsWithX:(CGFloat)x
y:(CGFloat)y
width:(CGFloat)width
height:(CGFloat)height
color:(UIColor *)color
button:(UIButton *)button;
kennedy otis
3,570 PointsThanks.. That worked the way I wanted :)
Thomas Nilsen
14,957 PointsGreat!