-1

Thanks a lot! Please help!

I hava a scrollView in a UIView and a imageView in the ScrollView. they have same size. and here are the LOGs of the scrollView and imageview

and I have not used autoLayout constrains between uiview and scroll View as well as scrollview and UIimageView

the code are:

- (void)setupScrollView
{
    CGRect frame = CGRectMake(0, 0, self.scrollViewSizeView.frame.size.width, self.scrollViewSizeView.frame.size.height);
    _colorScrollView = [[ColorScrollView alloc]initWithFrame:frame andUIImage:self.image];
    [self.scrollViewSizeView addSubview:self.colorScrollView];
}


- (instancetype)initWithFrame:(CGRect)frame andUIImage:(UIImage *)image
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.contentSize = frame.size;
        _imageView = [[ColorImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.imageView.image = image;
        [self addSubview:self.imageView];
    }
    return self;
}

scrollView:

<ColorScrollView: 0x7fee13746dc0; baseClass = UIScrollView; frame = (0 0; 600 436); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7fee13748270>; layer = <CALayer: 0x7fee13741330>; contentOffset: {0, 0}; contentSize: {600, 436}>

imageView:

 <ColorImageView: 0x7fee13747e00; baseClass = UIImageView; frame = (0 0; 600 436); opaque = NO; layer = <CALayer: 0x7fee1370fc50>>

the imageView content mode is UIViewContentModeScaleAspectFit. I want the image in imageView is center. but it is not. it look like it prefer to stay at the right side. sorry that I don't have enough reputation to post a image:

it looks this (* means transparent)


*****this is the image area

*****this is the image area

*****this is the image area

*****this is the image area

*****this is the image area

2 Answers2

0

The only thing that come to my mind is add a uiview in the uiscrollview and then add your image in to that uiview

if you could give us some of your constraint you use that could help us

Ali Riahipour
  • 524
  • 6
  • 20
0

From your commends for @Ali RP's answer, "if the aspect ratio of the image is close to the aspect ratio of the imageView. then no problem." I think your problem is because of UIViewContentModeScaleAspectFit. You have some white space of both size of the image.

UIViewContentModeScaleAspectFit

Scales the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.

And the reason why it is not centred is because the image you are using is too big I guess and right part of the subview is cropped. Try with a smaller one and one with different aspect ratio for debugging. For example, you should have some images with width > height and some with height > width.

Also see this one: Difference between UIViewContentModeScaleAspectFit and UIViewContentModeScaleToFill?

Community
  • 1
  • 1
Yuchen
  • 30,852
  • 26
  • 164
  • 234