我试图在滚动视图中将这些按钮连续放置,但按钮的 CGRect 的 x 和 y 位置不会填充,并且宽度、高度和 x 和 y 位置的每个变量似乎总是打印 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];
}