Search code examples
iosobjective-cuiscrollviewuiimageviewpure-layout

image view not scrollable in scrollview and not fit properly using pure layout


I'm simply trying to place an image into scroll view which is in a main view using purelayout.

And what i'm taking right now is;

sample image

Note that view with red background is scroll view. And also the image is not scrollable.

    - (void)loadView {
        self.view = [[UIView alloc] init];

        [self.scrollView addSubview:self.photoView];        
        [self.view addSubview:self.scrollView];
        [self.view setNeedsUpdateConstraints];
    }

    - (void)updateViewConstraints {

        if (!_didSetupConstraints) {
            ...
            ...
            ...
            [self.photoView autoPinEdgeToSuperviewEdge:ALEdgeTop];
            [self.photoView autoPinEdgeToSuperviewEdge:ALEdgeBottom];
            [self.photoView autoPinEdgeToSuperviewEdge:ALEdgeLeft];
            [self.photoView autoPinEdgeToSuperviewEdge:ALEdgeRight];

            self.didSetupConstraints = YES;
        }

        [super updateViewConstraints];
    }

    - (UIImageView *)photoView {
        if (!_photoView) {
            _photoView = [UIImageView newAutoLayoutView];
            _photoView.backgroundColor = [UIColor whiteColor];
        }
        return _photoView;
    }

    - (UIScrollView *)scrollView {
        if (!_scrollView) {
            _scrollView = [UIScrollView newAutoLayoutView];
            _scrollView.backgroundColor = [UIColor redColor];
            _scrollView.maximumZoomScale = 2.0;
            _scrollView.minimumZoomScale = 0.5;
        }
        return _scrollView;
    }

Solution

  • Ok, it seems working well with two changes i did.

    Firstly i set self.photoView size manually.

    [self.photoView autoSetDimensionsToSize:CGSizeMake([HelperModel screenWidth], [HelperModel screenHeight] -[HelperModel viewHeight:self.navigationController.navigationBar] -20.0)];
    

    HelperModel.m

    @implementation HelperModel
    
    + (CGFloat)screenWidth {
        return [[UIScreen mainScreen] bounds].size.width;
    }
    
    + (CGFloat)screenHeight {
        return [[UIScreen mainScreen] bounds].size.height;
    }
    
    + (CGFloat)viewHeight:(UIView *)view {
        return CGRectGetHeight(view.frame);
    }
    
    + (CGFloat)photoDetailHeight {
        return [HelperModel screenHeight] -20;
    }
    
    @end   
    

    Secondly,

    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
    

    for zooming feature and if the zoomed image exceeds the bounds of scroll view, then the image can be scrolled properly.