Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a working example : <a href="http://code.google.com/p/uiscrollview-grid-alignment/">Google Code</a></p> <p>I wrote out the code, and I have this. It will align the view to be 5 wide, and however tall it has to be, and the scrollview will change height.</p> <p>You will need to create a new <code>NSMutableArray</code> named <code>_buttons</code> that will contain a list of your buttons.</p> <pre><code>- (void)addImage:(UIImage *)imageToAdd { [_images addObject:imageToAdd]; [_thumbs addObject:[imageToAdd imageByScalingAndCroppingForSize:CGSizeMake(60, 60)]]; int row = floor(([views count] - 1) / 5); int column = (([views count] - 1) - (row * 5)); UIImage *thumb = [_thumbs objectAtIndex:[_thumbs count]-1]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(column*60+10, row*60+10, 60, 60); [button setImage:thumb forState:UIControlStateNormal]; [button addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside]; button.tag = [views count] - 1; // This is the title of where they were created, so we can see them move.s [button setTitle:[NSString stringWithFormat:@"%d, %d", row, column] forState:UIControlStateNormal]; [_buttons addObject:button]; [scrollView addSubview:button]; // This will add 10px padding on the bottom as well as the top and left. [scrollView setContentSize:CGSizeMake(300, row*60+20+60)]; } - (void)deleteItem:(id)sender { UIButton *button = (UIButton *)sender; [button removeFromSuperview]; [views removeObjectAtIndex:button.tag]; [_buttons removeObjectAtIndex:button.tag]; [self rearrangeButtons:button.tag]; } - (void)rearrangeButtons:(int)fromTag { for (UIButton *button in _buttons) { // Shift the tags down one if (button.tag &gt; fromTag) { button.tag -= 1; } // Recalculate Position int row = floor(button.tag / 5); int column = (button.tag - (row * 5)); // Move button.frame = CGRectMake(column*60+10, row*60+10, 60, 60); if (button.tag == [_buttons count] - 1) { [scrollView setContentSize:CGSizeMake(300, row*60+20+60)]; } } } </code></pre> <p>Note: In the <code>rearrangeButtons</code> method, it is possible to animate the changes.</p> <p>Here is the code to rearrange the files:</p> <pre><code>- (void)rearrangeButtons:(int)fromTag { for (UIButton *button in _buttons) { // Shift the tags down one if (button.tag &gt; fromTag) { // Create name string NSString *imageName = [NSString stringWithFormat:@"images%i.png", button.tag]; // Load image NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentFile = [paths objectAtIndex:0]; NSSting *oldFilePath = [documentFile stringByAppendingPathComponent:imageName]; NSData *data = [[NSData alloc] initWithContentsOfFile:oldFilePath]; button.tag -= 1; // Save the image with the new tag/name NSString *newImageName = [NSString stringWithFormat:@"images%i.png", button.tag]; NSString *newFilePath = [documentFile stringByAppendingPathComponent:newImageName]; [data writeToFile:newFilePath atomically:YES]; // Delete the old one NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *err = nil; if (![fileManager removeItemAtPath:file error:&amp;err]) { // Error deleting file } } // Recalculate Position int row = floor(button.tag / 5); int column = (button.tag - (row * 5)); // Move button.frame = CGRectMake(column*60+10, row*60+10, 60, 60); if (button.tag == [_buttons count] - 1) { [scrollView setContentSize:CGSizeMake(300, row*60+20+60)]; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload