Objective-C将图像缩略图附加到视图的末尾

时间:2016-05-05 14:58:57

标签: objective-c addsubview uistackview

我正在尝试将图像附加到视图上,让它们在一个漂亮整齐的行中显示缩略图。我遇到的问题是,看起来所有图像只是在角落中相互叠加在一起,因为只有阵列中的最后一个图像被显示出来。

- (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];

}

1 个答案:

答案 0 :(得分:0)

使用addArrangedSubview UIStackView代替addSubview

您应该将所有按钮添加到同一UIStackView。因此,将UIStackView的属性添加到您的类中,在viewDidLoad(或在故事板中)初始化它,并将按钮添加到addImageToScreen中的堆栈视图。