'Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy?
I adding tableView on viewController. When I open this screen, then I will get the below warning once.
[TableView] Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes. Make a symbolic breakpoint at UITableViewAlertForLayoutOutsideViewHierarchy to catch this in the debugger and see what caused this to occur, so you can avoid this action altogether if possible, or defer it until the table view has been added to a window. Table view: <UITableView: 0x7fe2ba8d5a00; frame = (-47 -80; 394 553); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x600003447480>; layer = <CALayer: 0x600003aa2be0>; contentOffset: {0, 0}; contentSize: {394, 176}; adjustedContentInset: {0, 0, 0, 0}; dataSource: <Project.MondayToSundayViewController: 0x7fe2b9da05c0>>
When added symbolic breakpoint for UITableViewAlertForLayoutOutsideViewHierarchy then below is shown in the console.
> UIKitCore`UITableViewAlertForLayoutOutsideViewHierarchy:
-> 0x7fff48260c7e <+0>: pushq %rbp
0x7fff48260c7f <+1>: movq %rsp, %rbp
0x7fff48260c82 <+4>: pushq %r14
0x7fff48260c84 <+6>: pushq %rbx
0x7fff48260c85 <+7>: subq $0x30, %rsp
0x7fff48260c89 <+11>: callq *0x3e51a6a1(%rip) ; (void *)0x00007fff51411350: objc_retain
0x7fff48260c8f <+17>: movq %rax, %rbx
0x7fff48260c92 <+20>: leaq 0x416a8dbf(%rip), %rdi ; _UIInternalPreference_UITableViewEnableAlertForLayoutOutsideViewHierarchy
0x7fff48260c99 <+27>: leaq 0x3e5a2140(%rip), %rsi ; @"UITableViewEnableAlertForLayoutOutsideViewHierarchy"
0x7fff48260ca0 <+34>: callq 0x7fff482a7529 ; _UIInternalPreferenceUsesDefault
0x7fff48260ca5 <+39>: testb %al, %al
0x7fff48260ca7 <+41>: jne 0x7fff48260cb3 ; <+53>
0x7fff48260ca9 <+43>: movb 0x416a8dad(%rip), %al ; _UIInternalPreference_UITableViewEnableAlertForLayoutOutsideViewHierarchy + 4
0x7fff48260caf <+49>: andb $0x1, %al
0x7fff48260cb1 <+51>: je 0x7fff48260d2d ; <+175>
0x7fff48260cb3 <+53>: leaq 0x416c3ffe(%rip), %rax ; _UIApplicationLinkedOnVersion
0x7fff48260cba <+60>: movl (%rax), %eax
0x7fff48260cbc <+62>: testl %eax, %eax
0x7fff48260cbe <+64>: je 0x7fff48260d3e ; <+192>
0x7fff48260cc0 <+66>: cmpl $0xd0000, %eax ; imm = 0xD0000
0x7fff48260cc5 <+71>: jb 0x7fff48260d2d ; <+175>
0x7fff48260cc7 <+73>: movq %rbx, %rdi
0x7fff48260cca <+76>: callq 0x7fff486252fa ; symbol stub for: objc_opt_class
0x7fff48260ccf <+81>: movq 0x41607402(%rip), %rsi ; "_isInternalTableView"
0x7fff48260cd6 <+88>: movq %rax, %rdi
0x7fff48260cd9 <+91>: callq *0x3e51a641(%rip) ; (void *)0x00007fff513f7780: objc_msgSend
0x7fff48260cdf <+97>: testb %al, %al
0x7fff48260ce1 <+99>: jne 0x7fff48260d2d ; <+175>
0x7fff48260ce3 <+101>: movq 0x3e519b46(%rip), %rax ; (void *)0x00007fff89ea06a0: _NSConcreteStackBlock
0x7fff48260cea <+108>: movq %rax, -0x38(%rbp)
0x7fff48260cee <+112>: movl $0xc2000000, %eax ; imm = 0xC2000000
0x7fff48260cf3 <+117>: movq %rax, -0x30(%rbp)
0x7fff48260cf7 <+121>: leaq 0x46728(%rip), %rax ; __UITableViewAlertForLayoutOutsideViewHierarchy_block_invoke
0x7fff48260cfe <+128>: movq %rax, -0x28(%rbp)
0x7fff48260d02 <+132>: leaq 0x3e51d237(%rip), %rax ; __block_descriptor_40_e8_32s_e5_v8?0l
0x7fff48260d09 <+139>: movq %rax, -0x20(%rbp)
0x7fff48260d0d <+143>: movq %rbx, %rdi
0x7fff48260d10 <+146>: callq *0x3e51a61a(%rip) ; (void *)0x00007fff51411350: objc_retain
0x7fff48260d16 <+152>: movq %rax, -0x18(%rbp)
0x7fff48260d1a <+156>: cmpq $-0x1, 0x416ca5c6(%rip) ; _UIInternalPreference_TableViewReorderingUsesDragAndDrop_block_invoke_10.__s_category + 7
0x7fff48260d22 <+164>: jne 0x7fff48260d52 ; <+212>
0x7fff48260d24 <+166>: movq %rax, %rdi
0x7fff48260d27 <+169>: callq *0x3e51a5fb(%rip) ; (void *)0x00007fff51411000: objc_release
0x7fff48260d2d <+175>: movq %rbx, %rdi
0x7fff48260d30 <+178>: addq $0x30, %rsp
0x7fff48260d34 <+182>: popq %rbx
0x7fff48260d35 <+183>: popq %r14
0x7fff48260d37 <+185>: popq %rbp
0x7fff48260d38 <+186>: jmpq *0x3e51a5ea(%rip) ; (void *)0x00007fff51411000: objc_release
0x7fff48260d3e <+192>: movl $0xd0000, %edi ; imm = 0xD0000
0x7fff48260d43 <+197>: callq 0x7fff48093724 ; _UIApplicationLinkedOnOrAfter
0x7fff48260d48 <+202>: testb %al, %al
0x7fff48260d4a <+204>: jne 0x7fff48260cc7 ; <+73>
0x7fff48260d50 <+210>: jmp 0x7fff48260d2d ; <+175>
0x7fff48260d52 <+212>: leaq 0x416ca58f(%rip), %rdi ; UITableViewAlertForLayoutOutsideViewHierarchy.once
0x7fff48260d59 <+219>: leaq -0x38(%rbp), %r14
0x7fff48260d5d <+223>: movq %r14, %rsi
0x7fff48260d60 <+226>: callq 0x7fff48624f8e ; symbol stub for: dispatch_once
0x7fff48260d65 <+231>: movq 0x20(%r14), %rax
0x7fff48260d69 <+235>: jmp 0x7fff48260d24 ; <+166>
Can you please help me to remove the warning?
Solution 1:[1]
You added constraint before you add your view to view hierarchy. Thas is why you get this error. Be sure that turn by turn adding, for example :
view.addSubview(tableView) // 1
// 2 and start adding constraints..
tableView.translatesAutoresizingMaskIntoConstraints = false
...
Solution 2:[2]
In my case the answers provided didn't help. Changing my DiffableDataSource snapshot implementation from this:
dataSource.apply(snapShot, animatingDifferences: true)
to this:
dataSource.apply(snapShot, animatingDifferences: false)
Fixed the error.
Solution 3:[3]
I've just solved this problem, and it's really tricky. It took me half a day.
The solution is:
1.Put your UITableView inside a UIView, and set the UITableView constraint to be the same size as the UIView,
2.Then change the UIView's Y and Height by monitoring the scrolling coordinates.
This will give you the effect you want. Don't change the size of the UITableView directly, just wrap it one layer.
Solution 4:[4]
- Check if you are trying to access tableView properties in
viewDidAppear
then change that to
viewWillAppear
So this way you now tableView is definitely present.
- Another option is remove all ? (optionals) from "tableView?." to "tableView." This way it will crash right where the problem is.
Solution 5:[5]
Word on the Street
Not too much online covering this. One Apple forum thread does go a little more into it, including a few workarounds, wherein one workaround appears to be pretty generic and functional.
In all cases, UIKit's warning seems to be obscure and tough for people to diagnose. Reports have very different ideas about causes and solutions. In some cases user-error seems to blame in other cases UIKit may be to blame. Not sure.
Workaround
Easiest workaround I found suppresses warning by subclassing UITableView and overriding layoutSubviews(). This workaround risks masking cases where your own code might behave badly, leading to the bad things the warning message warns of, and could be very confusing if the warning is suppressed, so proceed with caution.
To the workaround, I added a few lines of code to print a stack trace. Which I used as an alternative to UIKit's warning's suggestion to a breakpoint at the UIKit method responsible for detecting the windowless layout attempt.
class MyTableView : UITableView {
override func layoutSubviews() {
if (self.window == nil) {
#if DEBUG
for line in Thread.callStackSymbols {
print("\(line)")
}
#endif
return
}
super.layoutSubviews()
}
}
How the Problem Manifests For Me (Suggestions Welcome)
In my case, warning occurs in both table configuration approaches I tried, described below. In each case, UITableView is displayed as the result of a UITabBar item clicked, which triggers a segue to my UIViewController subclass. Unsure if relevant, but will mention, to avoid hassles of one massive Storyboard for whole project (of many view controllers w/multiple view containers), yet to enjoy benefits of visual layout storyboards offer, while avoiding hassles of fully programmatic layout, I broke project up into multiple storyboards, where tab bar is configured programmatically, with each view controller assigned to tab bar, instantiated from their own respective storyboard.
In first configuration attempt, my UIViewController subclass was a
UITableViewControllerdelegate withUITableViewa subview of view controller's main view.In second configuration attempt, view controller subclass' main view is a
Container Viewand uses an embed segue to loadUITableViewControllerinto it. (Note: In that case, all relevant view controllers and segues are configured entirely in Storyboard).
In both cases I get UIKit's warning as soon as I click relevant tab bar item. That's from simple initial 'hello world' bring-up testing, before adding any code to manipulate the table or deal with cells at all! That leads me to think that UIKit doesn't know how to handle launch table view handling in some configurations, and simply attempts to layout tableview prematurely.
Note: I welcome suggestions about how to configure my project and table so UIKit is happier and doesn't issue the warning. Perhaps I've missed an important step. Meanwhile, here's the top of the stack trace I got with the code snippet shown above
_s16MyProject14MyTableViewC14layoutSubviewsyyF + 244
_s16MyProject14MyTableViewC14layoutSubviewsyyFTo + 36
-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2588
_ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 472
-[UIView(Hierarchy) layoutBelowIfNeeded] + 556
-[UITabBarController _layoutViewController:] + 388
-[UITabBarController _wrapperViewForViewController:] + 208
-[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 1092
-[UITabBarController _setSelectedViewController:] + 424
-[UITabBarController setSelectedViewController:] + 112
-[UITabBarController _setSelectedViewControllerAndNotify:] + 600
-[UITabBarController _tabBarItemClicked:] + 60
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | zeytin |
| Solution 2 | Scott Bolin |
| Solution 3 | freercc |
| Solution 4 | Dharman |
| Solution 5 |
