Reputation: 3298
I am trying to position these buttons in a row in a scrollview but the x and y position of the CGRect for the buttons just will not populate and every variable for the width, height and x and y position always seems to print 0.
// create a mutable array to populate
imageNames = [[NSArray alloc] init];
NSMutableArray *mImageNames = [[NSMutableArray alloc] init];
// List of images
NSString *img1 = @"moustache";
NSString *img2 = @"whitem";
NSString *img3 = @"blackm";
// add images to mutable array
[mImageNames addObject:img1];
[mImageNames addObject:img2];
[mImageNames addObject:img3];
// set mutable array to array;
imageNames = mImageNames;
CGFloat contentSizeWidth = 0.0;
CGSize newSize = _scrollViewOutlet.frame.size;
for(int i = 0; i < [imageNames count]; i++){
NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:_firstImage];
UIButton *button = [NSKeyedUnarchiver unarchiveObjectWithData: archivedData];
[button addTarget:self action:@selector(badgePressed:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i+1;
CGRect newFrame = [button frame];
newFrame.size.width = _firstImage.frame.size.width;
newFrame.size.height = _firstImage.frame.size.height;
contentSizeWidth = (20+button.frame.size.width) * i;
newFrame.origin.x += (20+_firstImage.frame.size.width) * i;
newFrame.origin.y = _firstImage.frame.origin.y ;
[button setFrame:newFrame];
NSLog(@"button frame size: %f", _firstImage.frame.size.width);
NSString *imageName = [[imageNames objectAtIndex:i] stringByAppendingString:@".png"];;
NSString *documentsDirectory = [utils getDocumentsDirectoryPath];
//UIImage *image = [utils loadImage:imageName ofType:@"png" inDirectory:documentsDirectory];
UIImage *image = [UIImage imageNamed:imageName
]; [button setImage:image forState:UIControlStateNormal];
[button setImage:image forState:UIControlStateSelected];
[[button imageView] setContentMode: UIViewContentModeScaleAspectFit];
[button setAlpha:1.0f];
[[button imageView] setImage:image];
newSize.width += 10;
newSize.width = contentSizeWidth+100;
[_scrollViewOutlet setContentSize:newSize];
[_scrollViewOutlet addSubview:button];
}
Upvotes: 0
Views: 443
Reputation: 6731
I believe you need to create a new CGRect from scratch, and then assign it to the UIView.
You cannot grab the existing frame and then alter it's children.
CGRect newFrame = [button frame]; //this is not a new frame, it's the existing immutable frame
Upvotes: 1