I need to space UIButtons out on a UIScrollView. The code I have works, however depending on the amount of dataItems
I have, the spacing is not even.
Snippet in Question
CGRectMake(10, ((120 / (count + 1)) * (i + 1) * 3) ,300,50)
Specifically
((120 / 开发者_StackOverflow社区(count + 1)) * (i + 1) * 3)
Working code
int count = [dataItems count]; /* not a specific value, can grow */
for (int i = 0; i < count; i++) {
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTag:i];
[aButton setFrame: CGRectMake(10,((120 / (count + 1)) * (i + 1) * 3) ,300,50) ];
[aButton setTitle:[[dataItems objectAtIndex:i] objectForKey:@"Feed"] forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(viewCategories:) forControlEvents:UIControlEventTouchUpInside];
[scroller addSubview:aButton];
}
Screenshot
The example on the right should look like the example on the left as far as spacing goes. The UIButtons
are sitting on a UIScrollView
, so the UIScrollView
's contentSize
should also grow if there are more dataItems
so the buttons can scroll offscreen if there are say, 30+ dataItems
.
sounds like you need a set size and padding...
int count = [dataItems count];
CGFloat staticX = 10; // Static X for all buttons.
CGFloat staticWidth = 300; // Static Width for all Buttons.
CGFloat staticHeight = 50; // Static Height for all buttons.
CGFloat staticPadding = 10; // Padding to add between each button.
for (int i = 0; i < count; i++) {
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTag:i];
//Calculate with the Static values.
//I added one set of padding for the top. then multiplied padding plus height for the count.
// 10 + 0 for the first
// 10 + 60 for the second.
// 10 + 120 for the third. and so on.
[aButton setFrame: CGRectMake(10,(staticPadding + (i * (staticHeight + staticPadding)) ,staticWidth,staticHeight) ];
[aButton setTitle:[[dataItems objectAtIndex:i] objectForKey:@"Feed"] forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(viewCategories:) forControlEvents:UIControlEventTouchUpInside];
[scroller addSubview:aButton];
}
however if you are trying to get buttons to do this sort of behavior. I am inclined to agree with the rest.. You can make a custom cell with a button in it.
Then you can load that button cell when the table asks for a button.
And you have the table tie to your [dataItems count] for the total of the items.
then your only calculation is to set the cell height when you initially set the dataItems count. making sure not to make them too short. and the table will handle the rest.
I agree with Gurpartap, but if your dead set on this, I'd do it a bit differently. Even if you understand the math in that setFrame statement now, will you or someone else understand it later. I sure don't looking at it quick. Why not do something like:
CGRect frm = CGRectMake(10,10,300,50);
for (int i = 0; i < count; i++) {
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setFrame: frm];
frm.origin.y = frm.origin.y + aButton.frame.size.height + 10;
etc, etc
You should really use a UITableView instead. It does the scroll view, cell laying out, etc. all by itself. You just specify the cell contents.
精彩评论