Ashish P
Ashish P

Reputation: 3056

App crash when set title in view controller

I'm using LGSideMenuController for side menu. Everything is working fine.

But when I set title of viewController my app crash. Without title it's working fine.

I have following code which are available in didFinishWithLauchingOption to configure navigation bar.

if #available(iOS 11.0, *) {
    UINavigationBar.appearance(whenContainedInInstancesOf: [UIDocumentBrowserViewController.self]).tintColor = .menuColor
} else {
    UINavigationBar.appearance(whenContainedInInstancesOf: [UIDocumentPickerViewController.self]).tintColor = .menuColor
}

// Configure Navigation Bar
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [
    NSAttributedString.Key.foregroundColor : UIColor.menuColor,
    NSAttributedString.Key.font : UIFont.systemFontSize
]

UIColor Extension

extension UIColor {
    // Menu Background Color
    class var menuColor: UIColor {
        get {
            return UIColor(red: 0, green: 105, blue: 255)
        }
    }

    convenience init(red: Int, green: Int, blue: Int) {
        assert(red >= 0 && red <= 255, "Invalid red component")
        assert(green >= 0 && green <= 255, "Invalid green component")
        assert(blue >= 0 && blue <= 255, "Invalid blue component")

        self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
    }
}

I have configure LGSideMenuController using storyboard as follow:

Storyboard

I'm trying following code in DashboardViewController (DashboardVC) which is crashing my app:

self.title = "Dashboard"

and produce following error:

-[__NSCFNumber renderingMode]: unrecognized selector sent to instance 0xd9c845dd03d829d0
2019-03-08 12:50:37.552233+0530 AppName[5924:95152] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber renderingMode]: unrecognized selector sent to instance 0xd9c845dd03d829d0'
*** First throw call stack:
(
0   CoreFoundation                      0x0000000108f711bb __exceptionPreprocess + 331
1   libobjc.A.dylib                     0x0000000108093735 objc_exception_throw + 48
2   CoreFoundation                      0x0000000108f8ff44 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3   CoreFoundation                      0x0000000108f75ed6 ___forwarding___ + 1446
4   CoreFoundation                      0x0000000108f77da8 _CF_forwarding_prep_0 + 120
5   UIFoundation                        0x000000010e7154b6 __NSValidateCoreTextAttributes + 1098
6   UIFoundation                        0x000000010e711836 __NSStringDrawingEngine + 2830
7   UIFoundation                        0x000000010e714304 -[NSAttributedString(NSExtendedStringDrawing) boundingRectWithSize:options:context:] + 795
8   UIKitCore                           0x0000000111698a2f -[UILabel _textRectForBounds:limitedToNumberOfLines:includingShadow:] + 583
9   UIKitCore                           0x00000001116985a4 -[UILabel textRectForBounds:limitedToNumberOfLines:] + 50
10  UIKitCore                           0x000000011169d3f3 -[UILabel _intrinsicSizeWithinSize:] + 389
11  UIKitCore                           0x000000011169cb3c -[UILabel _ensureBaselineMetricsReturningBounds] + 197
12  UIKitCore                           0x000000011169ce9d -[UILabel _firstBaselineOffsetFromTop] + 169
13  UIKitCore                           0x00000001116788e5 -[UIView(AdditionalLayoutSupport) nsli_lowerAttribute:intoExpression:withCoefficient:forConstraint:onBehalfOfLayoutGuide:] + 1361
14  UIKitCore                           0x0000000111678385 -[UIView(AdditionalLayoutSupport) nsli_lowerAttribute:intoExpression:withCoefficient:forConstraint:] + 289
15  Foundation                          0x0000000107cb8ed3 -[NSLayoutConstraint _lowerIntoExpression:reportingConstantIsRounded:] + 96
16  Foundation                          0x0000000107cb8a0f -[NSLayoutConstraint _addToEngine:mutuallyExclusiveConstraints:] + 114
17  UIKitCore                           0x0000000111662bc7 -[UIView(UIConstraintBasedLayout) _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 447
18  UIKitCore                           0x0000000111663021 -[UIView(UIConstraintBasedLayout) _tryToAddConstraintWithoutUpdatingConstraintsArray:roundingAdjustment:mutuallyExclusiveConstraints:] + 30
19  UIKitCore                           0x00000001116636c4 -[UIView(UIConstraintBasedLayout) nsli_addConstraint:] + 38
20  Foundation                          0x0000000107cbaa92 __55+[NSLayoutConstraint _addOrRemoveConstraints:activate:]_block_invoke + 65
21  Foundation                          0x0000000107ca035a -[NSISEngine withBehaviors:performModifications:] + 110
22  Foundation                          0x0000000107cba96e +[NSLayoutConstraint _addOrRemoveConstraints:activate:] + 403
23  UIKitCore                           0x00000001109ee5e7 -[_UINavigationBarContentViewLayout _keepConstraintsActiveIfNecessary:updateLayout:] + 145
24  UIKitCore                           0x00000001109eec6c -[_UINavigationBarContentViewLayout setTitleView:] + 143
25  UIKitCore                           0x00000001109e7136 -[_UINavigationBarContentView _setupTitleViewAnimated:] + 1197
26  UIKit                               0x0000000129d030e9 -[_UINavigationBarContentViewAccessibility _setupTitleViewAnimated:] + 42
27  UIKitCore                           0x00000001109e8da1 -[_UINavigationBarContentView updateContentAnimated:] + 86
28  UIKitCore                           0x0000000110a1afeb -[_UINavigationBarVisualProviderModernIOS _updateContentForTopItem:backItem:animated:] + 1926
29  UIKitCore                           0x0000000110a1b638 -[_UINavigationBarVisualProviderModernIOS _updateContentForTopItem:backItem:] + 53
30  UIKitCore                           0x0000000110a1e255 -[_UINavigationBarVisualProviderModernIOS traitCollectionDidChange:] + 414
31  UIKitCore                           0x00000001109c7156 -[UINavigationBar traitCollectionDidChange:] + 529
32  UIKitCore                           0x0000000111713a47 -[UIView _wrappedProcessTraitCollectionDidChange:forceNotification:] + 137
33  UIKitCore                           0x0000000111713b54 -[UIView _wrappedProcessTraitCollectionDidChange:forceNotification:] + 406
34  UIKitCore                           0x0000000111713d5a -[UIView _processDidChangeRecursivelyFromOldTraits:toCurrentTraits:forceNotification:] + 129
35  UIKitCore                           0x000000011173e72d -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1337
36  QuartzCore                          0x00000001072f7b19 -[CALayer layoutSublayers] + 175
37  QuartzCore                          0x00000001072fc9d3 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 395
38  QuartzCore                          0x00000001072757ca _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 342
39  QuartzCore                          0x00000001072ac97e _ZN2CA11Transaction6commitEv + 576
40  UIKitCore                           0x000000011126f2d0 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 139
41  CoreFoundation                      0x0000000108ed662c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
42  CoreFoundation                      0x0000000108ed5de0 __CFRunLoopDoBlocks + 336
43  CoreFoundation                      0x0000000108ed0654 __CFRunLoopRun + 1284
44  CoreFoundation                      0x0000000108ecfe11 CFRunLoopRunSpecific + 625
45  GraphicsServices                    0x000000010e3b11dd GSEventRunModal + 62
46  UIKitCore                           0x000000011125481d UIApplicationMain + 140
47  AppName                               0x00000001038ea857 main + 71
48  libdyld.dylib                       0x000000010a527575 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Even I have also tried to set title from storyboard, it's also crash the app.

I have already followed solution available on SO, but it's didn't work for me.

Upvotes: 1

Views: 1083

Answers (3)

Matic Oblak
Matic Oblak

Reputation: 16774

Unfortunately, it looks like the issue is elsewhere and it might be hard to track this one down.

From the error you posted, it seems that there is some incorrect type entered somewhere. The -[__NSCFNumber renderingMode]: unrecognized selector sent to instance says basically means that there is an NSNumber where some other object type should be and a system is trying to call a method renderingMode which does not exist for this type.

These things happen mostly on the Objective-C part and some frameworks still do use Objective-C internally. It is easiest to input incorrect types through arrays or dictionaries. And from the rest of your stack, a good start might be searching for the attributed string. You can see near the end a label is being called and an attributed string NSAttributedString follows.

An attributed string will not check for types when it receives attributes so it is very easy to make it crash this way. Please check your code for anything that might work with NSAttributedString and comment out attributes to see if it fixes your crash. If this is the case then simply fix the attributes, there must be some mistake in what you have entered.

If this is not the case it still might be a bug on another system that uses NSAttributedString. Maybe some library or open source that you should be discouraged to use?

Or maybe even appearance may cause the same problem. Do you set appearance attributes somewhere in your code for your navigation bar?

Upvotes: 2

Niilesh R Patel
Niilesh R Patel

Reputation: 707

LGSideMenuController as One root controller and Two left and right view controller.

So please verify that the root view controller of SideMenuController added with NavigationController.

Ex:- 

UIViewController *rootViewController = [UIViewController new];
UITableViewController *leftViewController = [UITableViewController new];
UITableViewController *rightViewController = [UITableViewController new];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

LGSideMenuController *sideMenuController = [LGSideMenuController sideMenuControllerWithRootViewController:navigationController
                                                                                       leftViewController:leftViewController
                                                                                      rightViewController:rightViewController];

in the example controllers added with the navigation controller.

Upvotes: 0

Rawand Ahmed Shaswar
Rawand Ahmed Shaswar

Reputation: 2571

if you’re directly presenting the ViewController not with the NavigationController, the title will be nil. Try this:

self.navigationItem.title = "Dashboard"

Upvotes: 0

Related Questions