1

I am getting CALayer NAN crash. I have a scroll view, two textfields (let's say A and B) inside and inside of the scroll view below the two textfields, I have a tableView which is going to add textfields dynamically (say C and D). Steps: When I click on A it opens a new controller and click on selecting an option, an API gets called and on the first screen (of the scrollView and A and B textfields), tableview adds two textfields C and D. When I click on C, then again on textfield A, it crashes.

Below are the crash logs -

*** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan 428.667]. Layer: <CALayer:0x600001fb0220; position = CGPoint (0 0); bounds = CGRect (0 0; 92.6667 47); delegate = <_UIEditMenuListView: 0x7fd1ed2ba900; frame = (-46.3333 -23.5; 92.6667 47); alpha = 0; layer = <CALayer: 0x600001fb0220>>; sublayers = (<CALayer: 0x600001fbd900>); opaque = YES; opacity = 0; shadowOpacity = 0.25; shadowOffset = CGSize (0 3); shadowRadius = 12>'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000011b1f68ab __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x000000011500dba3 objc_exception_throw + 48
    2   CoreFoundation                      0x000000011b1f6789 -[NSException initWithCoder:] + 0
    3   QuartzCore                          0x000000011409c05e -[CALayer setPosition:] + 388
    4   UIKitCore                           0x000000012eeb00f4 -[UIView setCenter:] + 253
    5   UIKitCore                           0x000000012dcdcc2a -[_UIEditMenuPresentation _displayMenu:reason:] + 1959
    6   UIKitCore                           0x000000012dcdc387 -[_UIEditMenuPresentation displayMenu:configuration:] + 81
    7   UIKitCore                           0x000000012ee6e72a __58-[UIEditMenuInteraction presentEditMenuWithConfiguration:]_block_invoke + 207
    8   UIKitCore                           0x000000012ee70222 __80-[UIEditMenuInteraction _prepareMenuAtLocation:configuration:completionHandler:]_block_invoke + 87
    9   UIKitCore                           0x000000012ebdf5cb __133-[_UIContextMenuInteractionBasedTextContextInteraction _editMenuInteraction:menuForConfiguration:suggestedActions:completionHandler:]_block_invoke + 208
    10  UIKitCore                           0x000000012ebdf732 -[_UIContextMenuInteractionBasedTextContextInteraction _querySelectionCommandsForConfiguration:suggestedActions:completionHandler:] + 325
    11  UIKitCore                           0x000000012ebdf4da -[_UIContextMenuInteractionBasedTextContextInteraction _editMenuInteraction:menuForConfiguration:suggestedActions:completionHandler:] + 126
    12  UIKitCore                           0x000000012ee7018f -[UIEditMenuInteraction _prepareMenuAtLocation:configuration:completionHandler:] + 296
    13  UIKitCore                           0x000000012ee6e485 -[UIEditMenuInteraction presentEditMenuWithConfiguration:] + 240
    14  UIKitCore                           0x000000012ebdfd6d -[_UIContextMenuInteractionBasedTextContextInteraction presentSelectionCommandsWithConfiguration:] + 94
    15  UIKitCore                           0x000000012ec33b6f -[UITextSelectionView _presentEditMenuWithPreferredDirection:replacements:] + 103
    16  UIKitCore                           0x000000012ec33792 -[UITextSelectionView _showCommandsWithReplacements:isForContextMenu:forDictation:arrowDirection:] + 1487
    17  UIKitCore                           0x000000012ec33157 __77-[UITextSelectionView _showCommandsWithReplacements:forDictation:afterDelay:]_block_invoke + 262
    18  libdispatch.dylib                   0x0000000117d4bf5b _dispatch_client_callout + 8
    19  libdispatch.dylib                   0x0000000117d4f164 _dispatch_continuation_pop + 836
    20  libdispatch.dylib                   0x0000000117d66d2e _dispatch_source_invoke + 2198
    21  libdispatch.dylib                   0x0000000117d5cbc7 _dispatch_main_queue_drain + 1065
    22  libdispatch.dylib                   0x0000000117d5c790 _dispatch_main_queue_callback_4CF + 31
    23  CoreFoundation                      0x000000011b155b1f __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    24  CoreFoundation                      0x000000011b150436 __CFRunLoopRun + 2482
    25  CoreFoundation                      0x000000011b14f6a7 CFRunLoopRunSpecific + 560
    26  GraphicsServices                    0x000000011f05728a GSEventRunModal + 139
    27  UIKitCore                           0x000000012e8a2ad3 -[UIApplication _run] + 994
    28  UIKitCore                           0x000000012e8a79ef UIApplicationMain + 123
    29  libswiftUIKit.dylib                 0x000000011c5a1c72 $s5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF + 98
    30  U App                       0x0000000102c08b78 $s12U_AppySpySpys4Int8VGGXEfU_ + 168
    31  U App                        0x0000000102c08ed0 _swift_se0333_UnsafeMutablePointer_withMemoryRebound + 800
    32  U App                        0x0000000102c08a79 main + 185
    33  dyld                                0x0000000111a2d2bf start_sim + 10
    34  ???                                 0x00000002096bd310 0x0 + 8747995920
)
libc++abi: terminating with uncaught exception of type NSException

Clicking on UITextFields causes the above crash and it happens every time. I checked for code which can give me undefined values (such as divide by 0, etc.) but didn't find any solutions.

I tried everything and it's been 2 weeks already, any help is really appreciated!

CODE:

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {

    textField.layoutIfNeeded()
    textField.spellCheckingType = .no
    textField.autocorrectionType = .no
    
    if textField == txtfldLoc {
        self.locationButtonTapped(textField)
        return false
    }
    else if textField == txtfldOp {
        self.operatorButtonTapped(textField)
        return false
    }
    else if textField.superview?.isDescendant(of: self.authTableView) == true
    {
        if textField.accessibilityLabel != nil{
            
            let index = Int(textField.accessibilityLabel!)!
            
            let cell = self.authTableView.cellForRow(at: IndexPath(row: index, section: 0)) as! AuthTVC
            self.selectedRow = IndexPath(row: index, section: 0)
            if authenticatorObjectList.count > index  {
                let authenticator = self.authenticatorObjectList[index]

                cell.authenticatorTextField.defaultPlaceholderColor = UIColor(hex: Color.shared.blue)!
                cell.authenticatorTextField.placeholderColor = UIColor(hex: Color.shared.blue)!
                cell.authenticatorTextField.textColor = .black
                
                if authenticator.dataType == "List" {
                    self.authenticatorButtonTapped(sender:textField)
                    return false
                }
                else {
                    if authenticator.regex.first == "^"  {
                        
                        if let txtfld  = self.arrOfViews.first(where:{$0.accessibilityLabel == "\(index)"}) as? MFTextField
                            
                        {
                            
                            if let ind = self.arrOfViews.firstIndex(of: txtfld)
                            {
                                cell.authenticatorTextField.text = txtfld.text
                            }
                        }
                        DispatchQueue.main.async {
                            self.authTableView.layoutIfNeeded()
                        }
                        return true
                    }
                }
            }
        }
    }else if textField == amountTextField{
        

    }
    return true
}
  • Sounds similar to this: https://stackoverflow.com/questions/75280084/the-app-is-crashed-when-layout-2-text-fields-horizontally-on-ios-16-2 – DonMag Apr 03 '23 at 18:45
  • @DonMag tried that solution just now, it is still crashing – Shruti Suryawanshi Apr 03 '23 at 18:57
  • 1
    OK - with the information provided, tough to give you any other answer. Try to put together a [mre] so we can see exactly what's happening and help you look for what might be causing the problem. – DonMag Apr 03 '23 at 19:00
  • The link you provide the crash is similar. My textfields are vertically place, one in the main scrollview and one textfield inside the tableview which is inside the scrollview. So like I explained in the post after clicking on the "A" textfield it crashes. – Shruti Suryawanshi Apr 03 '23 at 19:05
  • 1
    I understand... your layout / view hierarchy is "similar but different." So, to try and help you figure this out, we need to *use your code*. That's why a [mre] is needed. – DonMag Apr 03 '23 at 19:49
  • So I have update the post and added code snippet on click of the UITextField (using library MFTextField but I tried just UITextfield as well it crashes). The Entire code structure is complicated and the code is too long so this was the main function for onclick. self. locationButtonTapped() method is pushing new VC. – Shruti Suryawanshi Apr 03 '23 at 19:59
  • 1
    If you remove all of that (comment it out), does it still crash? If not, add back in one piece at a time until you reproduce the crash. Then you know where to debug. – DonMag Apr 03 '23 at 20:37
  • @DonMag, I have removed everything and just added the dynamic textfields adding part, still it is crashing. – Shruti Suryawanshi Apr 05 '23 at 06:16
  • Can you set a breakpoint on the first line and step through the code? Or does the crash happen before you get to the breakpoint? I'm happy to try and help you debug this, but without a [mre] we can't do much. Try starting with this: https://pastebin.com/hHy0CfXZ ... add in the rest of your code/logic *one piece at a time* until you get the crash. – DonMag Apr 05 '23 at 13:03
  • Yes tried that and noticed that every time textFieldShouldBeginEditing delegate method gets called twice, the app is crashing. – Shruti Suryawanshi Apr 07 '23 at 05:47

0 Answers0