我正试图将图像附加到视图中,并让它们以一个漂亮整洁的行显示缩略图。我遇到的问题是,似乎所有图像都只是在角落里叠加在一起,因为阵列中只有最后一个图像被显示。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.notesField.delegate = self;
NSString *userName = ((ontracAppDelegate*)[UIApplication sharedApplication].delegate).userName;
self.textField.text = [NSString stringWithFormat:@"Date Created: %@ nCreated By: %@",[NSDate date], userName];
if (self.noteText != nil) {
self.notesField.text = self.noteText;
self.notesField.editable = NO;
self.textLabel.hidden = TRUE;
}
[self.view resignFirstResponder];
if ([self.imageArray count] != 0) {
for (UIImage *image in imageArray) {
NSLog(@"Image %@", image);
[self addImageToScreen:image];
}
}
NSLog(@"imageArray %@", imageArray);
NSLog(@"self.textField.text %@", self.textField.text );
NSLog(@"self.notesField.text %@", self.notesField.text);
}
-(void) addImageToScreen:(UIImage*) image;
{
int adjustHeight = 0;
int adjustWidth = 10;
int imagesInARow = 7;
int imageHeight = 75;
int imageWidth = 75;
int count = [self.imageArray count];
self.numberOfPhotosLabel.text = [NSString stringWithFormat:@"%i",count];
if (count > 1 && count < imagesInARow) {
adjustHeight = 0;
adjustWidth = (20 * [self.imageArray indexOfObject:image]) + ([self.imageArray indexOfObject:image] * imageWidth);
}
UIButton* container = [[UIButton alloc] init ];
CGRect frame = CGRectMake(adjustWidth, 5, imageWidth, imageHeight);
[container setTag:[self.imageArray indexOfObject:image]];
[container setImage:image forState:UIControlStateNormal];
[container setFrame:frame];
[container addTarget:self action:@selector(displayFullScreenImage:) forControlEvents:UIControlEventAllTouchEvents];
UIStackView *photosView = [[UIStackView alloc] initWithFrame:frame];
container.frame = photosView.bounds;
[photosView addSubview:container];
[self.view addSubview:photosView];
}
使用UIStackView
的addArrangedSubview
而不是addSubview
。
您应该将所有按钮添加到同一个UIStackView
中。因此,将UIStackView
的属性添加到类中,在viewDidLoad
(或故事板)中初始化它,并在addImageToScreen
中将按钮添加到堆栈视图中。