SIGABRT 将 NSDictionary 用于文本和阴影属性

SIGABRT using NSDictionary for text and shadow attributes

我正在尝试在 Objective C 中构建一个简单的应用程序,利用我的 AppDelegate 模块中的属性字典来允许我自定义故事布局中各种导航项的外观。

代码构建良好,没有错误,但当它部署到我的测试设备上时,我得到了一个 SIGABRT。

我使用的是最新版本 Xcode(9.2);故事板都设置为"Builds for iOS 8.2 and Later";部署目标设置为 8.1。

我在我的代码中使用 UITextAttributeTextShadowColor, nil 没有问题,但自 iOS 7.0 以来已弃用,所以我将其更新为 NSShadowAttributeName, nil 现在它不起作用。

我做错了什么?

The specific SIGABRT error reads: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIDeviceRGBColor shadowColor]: unrecognized selector sent to instance 0x1d447cf00'.

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSDictionary *attribs = [NSDictionary dictionaryWithObjectsAndKeys:
    [UIColor colorWithRed:170.0/255.0 green:21.0/255.0 blue:29.0/255.0 alpha:1.0],
    NSForegroundColorAttributeName,
    [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0],
    NSShadowAttributeName, nil];
    [[UINavigationBar appearance] setTitleTextAttributes: attribs];
    [[UIBarButtonItem appearance] setTitleTextAttributes: attribs forState:UIControlStateNormal];
    return YES;
}

您需要像这样更新代码:

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowOffset = CGSizeMake(1, 0);
NSDictionary * attribs = @{NSForegroundColorAttributeName: [UIColor whiteColor],
                                  NSShadowAttributeName: shadow,
                                  NSFontAttributeName: [UIFont titleBolder]};
[[UINavigationBar appearance] setTitleTextAttributes: attribs];

这将解决您的崩溃问题。

文档:NSShadowAttributeName

The value of this attribute is an NSShadow object. The default value of this property is nil.

显然那不是你所做的。你给了一个 UIColor 对象。

这正是错误所在:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIDeviceRGBColor shadowColor]: unrecognized selector sent to instance 0x1d447cf00'

它说:我试图在 UIDeviceRGBColor(肯定是 UIColor 的集群)对象上调用方法 shadowColor。但是因为它不知道那个方法(~选择器,对于你的水平就是这样),我崩溃了。

显然,这就是您可能会怀疑的地方。 shadowColor 这是 NSShadow 对象的可用方法。也许我做错了什么。阅读文档你就会知道是这样的。

因此,为相应的值放置一个 NSShadow 对象而不是 UIColor 对象。