0
votes

I'm new to iOS. Here is my code causing the error.

FSImageView *imageView = [_imageViews objectAtIndex:(NSUInteger) page];
if ((NSNull *) imageView == [NSNull null]) {
        imageView = [self dequeueImageView];
        if (imageView != nil) {
            [_imageViews exchangeObjectAtIndex:(NSUInteger) imageView.tag withObjectAtIndex:(NSUInteger) page];
            imageView = [_imageViews objectAtIndex:(NSUInteger) page];
        }
    }

    if (imageView == nil || (NSNull *) imageView == [NSNull null]) {
        imageView = [[FSImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, _scrollView.bounds.size.width, _scrollView.bounds.size.height)];
        UIColor *backgroundColor = barsHidden ? _backgroundColorHidden : _backgroundColorVisible;
        [imageView changeBackgroundColor:backgroundColor];
        [_imageViews replaceObjectAtIndex:(NSUInteger) page withObject:imageView];
    }

    imageView.useScaleFactor = self.useScaleFactor;
    imageView.image = _imageSource[page];
if (imageView.superview == nil) {
        [_scrollView addSubview:imageView];

        if (@available(iOS 11.0, *)) {
            [imageView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor
                                                constant:0.0].active = YES;
            [imageView.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor
                                                   constant:0.0].active = YES;
        } else {
            // Fallback on earlier versions
        }
    }

The exception occurs at Anchor stuffs. It says..

Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors and because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'

1

1 Answers

0
votes

Looking at your code at this point

[imageView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor
                                            constant:0.0].active = YES;
[imageView.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor
                                               constant:0.0].active = YES;

You are trying to activate the constraint with respect to the superView(i.e your self.view) of superView(i.e your scrollView) That's illegal

Your structure is like UIView -> ScrollView -> imageView. and your are setting anchor from your UIView, you should try setting it relative to ScrollView.

EDIT

Your should try this:

[imageView.topAnchor constraintEqualToAnchor:_scrollView.topAnchor
                                                constant:0.0].active = YES;
[imageView.bottomAnchor constraintEqualToAnchor:_scrollView.bottomAnchor
                                                   constant:0.0].active = YES;

also, set for .leftAnchor and .rightAnchor.

EDIT

[_scrolLView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor
                                                constant:0.0].active = YES;
[_scrolLView.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor
                                                   constant:0.0].active = YES;

also, set for .leftAnchor and .rightAnchor.

Try and share the results.

Hope it helps.