1

I am having a strange problem here. I have a UIScrollView that is setup in the interface builder. I add content to it via a method called setupScrollView which is as follows :

-(void)setupScrollView {

    UIView *content = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width*[self.stickers count], self.scrollView.frame.size.height)];
    for(int i=0; i<[self.stickers count]; i++){

        UIImageView *imageView= [[UIImageView alloc] initWithFrame:CGRectMake(i*self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
        MySticker *sticker = self.stickers[i];
        imageView.image=sticker.image;
        [content addSubview:imageView];
        NSLog(@"scroll view width is %f",self.scrollView.frame.size.width);
        NSLog(@"ImageView width is %f", imageView.frame.size.width);
    }



    self.scrollView.contentSize=CGSizeMake(content.frame.size.width ,content.frame.size.height);
    [self.scrollView addSubview:content];




}

Now I call this method in viewDidLayoutSubviews because I need the views to be repositioned first after using the constraints set in the interface builder. This all works fine but my images in the scroll view are slightly distorted with jagged edges, there are sticker/chat images with black outlines so it's quite noticeable. If however I place the [self setupScrollView] in the viewDidLoad method the images won't be distorted and look perfectly clear. Example image:

enter image description here

Have no idea why it is doing this. Could anyone give me some pointers to what I might be doing wrong?

1 Answer 1

1

If it works correctly in viewDidLoad try to call setupScrollView method in viewWillLayoutSubviews method. Also check situation when setupScrollView will be call more than once to avoid adding duplicates.

3
  • Still distorted if I place it in viewWillLayoutSubviews. I could keep the method in viewDidLoad if I could get the correct content size, but the problem is the content size isn't yet defined. Is there any other way I could calculate the content size whilst leaving the setupScrollView in viewDidLoad?
    – KexAri
    Apr 12, 2015 at 17:16
  • wait up though, seems that the setupScrollView is being called more than once....
    – KexAri
    Apr 12, 2015 at 17:18
  • THAT WAS IT! You are a genius. It was adding the scrollview more than once. No wonder it was distorted. I put a BOOL flag in there so it would only do it once and now it's fine. Was banging me head for over 6 hours on this. Thanks so much!
    – KexAri
    Apr 12, 2015 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.