0
votes

I am getting this error in my application when i set the constraints of an UIActivityIndicatorView there is inside of an UITableViewController.The error ocurrs when i call the showFooter function:

Fatal Exception: NSGenericException 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.

func showFooter(show: Bool) {
    DispatchQueue.main.safeAsync {
        if show {
            let loadingFooter = UIActivityIndicatorView(activityIndicatorStyle: .gray)
            UIApplication.shared.keyWindow?.addSubview(loadingFooter)
            loadingFooter.startAnimating()
            loadingFooter.snp.makeConstraints { make in
                make.centerX.equalTo(self.view.snp.centerX)
                make.height.equalTo(20)
                make.bottom.equalTo(self.view.snp.bottom).inset(49)
            }
        } else {
            UIApplication.shared.keyWindow?.subviews.forEach { ($0 as? UIActivityIndicatorView)?.removeFromSuperview() }
        }
    }
}

public struct ConstraintViewDSL: ConstraintAttributesDSL { 
    public func makeConstraints(_ closure: (_ make: ConstraintMaker) -> Void) {
        ConstraintMaker.makeConstraints(item: self.view, closure: closure)
    }
}

public extension ConstraintView {
    public var snp: ConstraintViewDSL {
        return ConstraintViewDSL(view: self)
    }
}

Backtrace / Debug Log:

Fatal Exception: NSGenericException
0  CoreFoundation                 0x182609d04 __exceptionPreprocess
1  libobjc.A.dylib                0x181858528 objc_exception_throw
2  CoreFoundation                 0x182609c4c -[NSException initWithCoder:]
3  Foundation                     0x183118d18 -[NSLayoutConstraint _setActive:mutuallyExclusiveConstraints:]
4  Foundation                     0x183119004 __55+[NSLayoutConstraint _addOrRemoveConstraints:activate:]_block_invoke
5  Foundation                     0x182f3b59c -[NSISEngine withBehaviors:performModifications:]
6  Foundation                     0x183118ed4 +[NSLayoutConstraint _addOrRemoveConstraints:activate:]
7  SnapKit                        0x10281b54c specialized static ConstraintMaker.makeConstraints(item : LayoutConstraintItem, closure : (ConstraintMaker) -> ()) -> () (ConstraintMaker.swift:272)
8  SnapKit                        0x10281e578 ConstraintViewDSL.makeConstraints((ConstraintMaker) -> ()) -> () (ConstraintViewDSL.swift)
9  Zattini                        0x101341fc0 specialized UIViewController.(showFooter(show : Bool) -> ()).(closure #1) (UIViewController+Extensions.swift:140)
10 Zattini                        0x101340dd8 UIViewController.showFooter(show : Bool) -> () (UIViewController+Extensions.swift:14)
11 Zattini                        0x101340f10 @objc UIViewController.showFooter(show : Bool) -> () (UIViewController+Extensions.swift)
12 Zattini                        0x10132f5c4 ProductListControllerZattiniProductListDelegate (ProductListController.swift:197)
13 Zattini                        0x1013881cc ProductListViewModel.loadMoreProducts() -> () (ProductListViewModel.swift:159)
14 Zattini                        0x101331858 specialized ProductListController.scrollViewDidScroll(UIScrollView) -> () (ProductListController.swift:192)
15 Zattini                        0x10132de80 @objc ProductListController.scrollViewDidScroll(UIScrollView) -> () (ProductListController.swift)
16 UIKit                          0x18bd4270c -[UIScrollView(UIScrollViewInternal) _notifyDidScroll]
17 UIKit                          0x18ba5f090 -[UIScrollView setContentOffset:]
18 UIKit                          0x18bd46398 -[UIScrollView(UIScrollViewInternal) _adjustContentOffsetIfNecessary]
19 UIKit                          0x18ba99cc4 -[UIScrollView setBounds:]
20 UIKit                          0x18ba99580 -[UICollectionView setBounds:]
21 UIKit                          0x18bd1dcf4 -[UIView(Geometry) _applyISEngineLayoutValuesToBoundsOnly:]
22 UIKit                          0x18ba585c8 -[UIView(Geometry) _resizeWithOldSuperviewSize:]
23 UIKit                          0x18c5e4b48 -[UIScrollView(_UIOldConstraintBasedLayoutSupport) _resizeWithOldSuperviewSize:]
24 CoreFoundation                 0x1824e3a6c -[__NSArrayM enumerateObjectsWithOptions:usingBlock:]
25 UIKit                          0x18ba46af8 -[UIView(Geometry) resizeSubviewsWithOldSize:]
26 UIKit                          0x18bb2f018 -[UIView(AdditionalLayoutSupport) _is_layout]
27 UIKit                          0x18bd21228 -[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine]
28 UIKit                          0x18ba3e304 -[UIView(CALayerDelegate) layoutSublayersOfLayer:]
29 QuartzCore                     0x1865f7ec8 -[CALayer layoutSublayers]
30 QuartzCore                     0x1865fbfa8 CA::Layer::layout_if_needed(CA::Transaction*)
31 UIKit                          0x18ba530a8 -[UIView(Hierarchy) layoutBelowIfNeeded]
32 UIKit                          0x18bae8d7c -[UINavigationController _layoutViewController:]
33 UIKit                          0x18bae644c -[UINavigationController _layoutTopViewController]
34 UIKit                          0x18bdcf3c8 __105-[UINavigationController _repositionPaletteWithNavigationBarHidden:duration:shouldUpdateNavigationItems:]_block_invoke
35 UIKit                          0x18bdcf0a8 -[UINavigationController _repositionPaletteWithNavigationBarHidden:duration:shouldUpdateNavigationItems:]
36 UIKit                          0x18bae5a78 -[UINavigationController _updateBarsForCurrentInterfaceOrientation]
37 UIKit                          0x18bb67cac -[UITabBarController _updateLayoutForStatusBarAndInterfaceOrientation]
38 CoreFoundation                 0x18259c0bc __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__
39 CoreFoundation                 0x18259b65c _CFXRegistrationPost
40 CoreFoundation                 0x18259b3c0 ___CFXNotificationPost_block_invoke
41 CoreFoundation                 0x1826189c4 -[_CFXNotificationRegistrar find:object:observer:enumerator:]
42 CoreFoundation                 0x1824d20c0 _CFXNotificationPost
43 Foundation                     0x182ef3498 -[NSNotificationCenter postNotificationName:object:userInfo:]
44 UIKit                          0x18bf3f10c -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:]
45 UIKit                          0x18c6ba4b8 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke
46 UIKit                          0x18c6ba35c -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]
47 UIKit                          0x18c42c294 __125-    [_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke
48 UIKit                          0x18c5c30ac _performActionsWithDelayForTransitionContext
49 UIKit                          0x18c42c144 -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]
50 UIKit                          0x18c2122d8 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:]
51 UIKit                          0x18c0b4d68 -[UIApplicationSceneClientAgent scene:handleEvent:withCompletion:]
52 FrontBoardServices             0x184cb620c __80-[FBSSceneImpl updater:didUpdateSettings:withDiff:transitionContext:completion:]_block_invoke.362
53 libdispatch.dylib              0x181f8d048 _dispatch_client_callout
54 libdispatch.dylib              0x181f946c8 _dispatch_block_invoke_direct$VARIANT$mp
55 FrontBoardServices             0x184ce99f4 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__
56 FrontBoardServices             0x184ce9698 -[FBSSerialQueue _performNext]
57 FrontBoardServices             0x184ce9c34 -[FBSSerialQueue _performNextFromRunLoopSource]
58 CoreFoundation                 0x1825b22e8 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
59 CoreFoundation                 0x1825b2268 __CFRunLoopDoSource0
60 CoreFoundation                 0x1825b1af0 __CFRunLoopDoSources0
61 CoreFoundation                 0x1825af6c8 __CFRunLoopRun
62 CoreFoundation                 0x1824cffb8 CFRunLoopRunSpecific
63 GraphicsServices               0x184367f84 GSEventRunModal
64 UIKit                          0x18baa42f4 UIApplicationMain
65 Zattini                        0x101113a84 main (AppDelegate.swift:21)
66 libdyld.dylib                  0x181ff256c start
1

1 Answers

0
votes

You can only set constraints between UIViews that are in the same view hierachy. You are adding the UIActivityIndicatorView directly to the app's key window and then try to set constraints to the view controller's view. Those two UIViews are not in the same view hierachy, so you'll get an error.

This is the view hierachy of the UIActivityIndicatorView:

enter image description here

And this is the view hierachy of the view controller's view:

enter image description here

To make it work you have to set the constraints between the UIActivityIndicatorView and the keyWindow:

func showFooter(show: Bool) {
    guard let keyWindow = UIApplication.shared.keyWindow else { return }
    if show {
        let loadingFooter = UIActivityIndicatorView(activityIndicatorStyle: .gray)
        loadingFooter.backgroundColor = .red
        keyWindow.addSubview(loadingFooter)
        loadingFooter.startAnimating()
        loadingFooter.snp.makeConstraints { make in
            make.centerX.equalTo(keyWindow)
            make.height.equalTo(20)
            make.bottom.equalTo(keyWindow).inset(49)
        }
    } else {
        UIApplication.shared.keyWindow?.subviews.forEach { ($0 as? UIActivityIndicatorView)?.removeFromSuperview() }
    }
}

Another small thing. When you setting constraints between the same anchors of 2 views instead of

make.centerX.equalTo(self.view.snp.centerX)

you can write

make.centerX.equalTo(self.view)

A bit shorter ;-)