UIImageView does not correctly resize in cells for iOS 8 self calibration

enter image description here

I am trying to achieve something similar to the 9gag application, where the cells automatically change depending on the size of the image. The width embraces the sides, but the height changes as necessary.

I have a shortcut consisting of a title above it, as well as a UIImageView, and below the image I have section icons, etc. I set limits for the image as a UIImageView, as well as lower icons for both leading and ending spaces. It seems that I tried every combination of the "Mode" switch of the view (for example, the fill aspect, the aspect fits), when I do the aspect, it changes the image correctly, but it adds a bunch of indentation to both the bottom and the top of the image (the screenshot is included below )enter image description here

All labels and badges have corresponding restrictions (as far as I can tell)

I searched for several hours trying to find someone with a similar problem, but to no avail, I cannot find anything regarding self-calibration of cells that use images that may vary in size.

Can someone point me in the right direction or find out about a solution to my problem? All help is much appreciated, as always!

+4
source share
3 answers

As I have already seen, you cannot really resize images from Interface Builder. You will need to manually write the code in the ViewController.m file. There is something like this:

- (void) adjustImageSize{
    CGSize imageSize = self.imageView.image.size;
    CGSize imageViewSize = self.imageView.frame.size;
    float imageAspectRatio = imageSize.width/imageSize.height;
    CGRect frame = self.statusImage.frame;
    frame.size.height =imageViewSize.width/imageAspectRatio;
    self.imageView.frame = frame;
    // set your Cell frame here, based on your other constraints (labels/buttons etc), after you have resized your image
}

You can call this from your tableView data source function. Then add this delegate function to inform the table about the dynamic height of each cell.

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

So I did a dynamic image resizing for a NewsFeed based application. Hope this helps.

+1
source

- UIImageView ( ). , , .

, UIImageView

@interface PostImageViewCell()

@property (nonatomic, strong) NSLayoutConstraint *aspectConstraint;

@end

@implementation PostImageViewCell

- (void) setAspectConstraint:(NSLayoutConstraint *)aspectConstraint {

    if (_aspectConstraint != nil) {
        [self.postImage removeConstraint:_aspectConstraint];
        _aspectConstraint = nil;
    }
    if (aspectContraint != nil) {
        [self.postImage addConstraint:aspectConstraint];
        _aspectConstraint = aspectConstraint
    }
}

- (void)setPicture:(UIImage *)image {

    CGFloat aspect = image.size.width / image.size.height;
    self.aspectConstraint = [NSLayoutConstraint constraintWithItem:self.postImage
                                                         attribute:NSLayoutAttributeWidth
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self.postImage
                                                         attribute:NSLayoutAttributeHeight
                                                        multiplier:aspect
                                                          constant:0.0];
    [self.postImage setImage:image];
}

@end
+1

.

, View , UIImageView , .

: 1000x1000. " Fit", UIImageView . , iPhone - . 750px 750px ( ). , imageView ; 1000x1000. .

, , , .

?

0

All Articles