简体   繁体   English

UILabel导致应用程序在添加到视图时崩溃(仅限Xcode 6和iOS 8)

[英]UILabel causes app to crash when added to view (Xcode 6 and iOS 8 only)

I have transitioned my project to Xcode 6 in order to build for iOS 8. However, a particular UILabel is causing the app to crash when it is added to the view hierarchy. 我已将我的项目转换为Xcode 6,以便为iOS 8构建。但是,特定的UILabel会导致应用程序在添加到视图层次结构时崩溃。 This is the only error log I receive: 这是我收到的唯一错误日志:

-[MyViewController _contentInsetsFromFonts]: unrecognized selector sent to instance     0x16d90da0

I have been unable to locate the contentInsetsFromFonts method anywhere in my project. 我无法在项目中的任何位置找到contentInsetsFromFonts方法。 Additionally, I have not even been able to find a reference for it anywhere online, including Apple's documentation. 此外,我甚至无法在网上任何地方找到它的参考,包括Apple的文档。 I am not using a NIB for this UIViewController so the UI is built in the loadView method: 我没有为这个UIViewController使用NIB,因此UI是在loadView方法中构建的:

- (void)loadView {
    UIImage *trackImage = [UIImage imageNamed:@"sliderTrack.png"];
    sliderBackground = [[UIImageView alloc] initWithImage:trackImage];
    UIView *view = [[UIView alloc] initWithFrame:sliderBackground.frame];
    [view addSubview:sliderBackground];

    slider = [[UISlider alloc] initWithFrame:sliderBackground.frame];
    CGRect sliderFrame = slider.frame;
    sliderFrame.size.width -= 46; 
    slider.frame = sliderFrame;
    slider.center = sliderBackground.center;
    slider.backgroundColor = [UIColor clearColor];
    [slider setMinimumTrackImage:[UIImage imageNamed:@"sliderMaxMinTrackImage.png"] forState:UIControlStateNormal];
    [slider setMaximumTrackImage:[UIImage imageNamed:@"sliderMaxMinTrackImage.png"] forState:UIControlStateNormal];
    UIImage *thumbImage = [UIImage imageNamed:@"cancel-slider.png"];
    [slider setThumbImage:thumbImage forState:UIControlStateNormal];
    slider.minimumValue = 0.0;
    slider.maximumValue = 1.0;
    slider.continuous = YES;
    slider.value = 0.0;

    // Set the slider action methods
    [slider addTarget:self 
           action:@selector(sliderUp:) 
     forControlEvents:UIControlEventTouchUpInside];
    [slider addTarget:self 
           action:@selector(sliderDown:) 
     forControlEvents:UIControlEventTouchDown];
    [slider addTarget:self 
           action:@selector(sliderChanged:) 
     forControlEvents:UIControlEventValueChanged];    

    NSString *labelText = @"label text";
    UIFont *labelFont;
    NSString *osVersion = [[UIDevice currentDevice] systemVersion];
    if ([osVersion floatValue] >= 7.0) {
        labelFont = [UIFont systemFontOfSize:22.0];

    } else {
        labelFont = [UIFont systemFontOfSize:24.0];
    }

    CGSize labelSize = [labelText sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:labelFont, NSFontAttributeName, nil]];

    label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, labelSize.width, labelSize.height)];

    CGFloat labelHorizontalCenter = slider.center.x + (thumbImage.size.width / 2);
    label.center = CGPointMake(labelHorizontalCenter, slider.center.y);

    // Set other label attributes and add it to the view
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.font = labelFont;
    label.text = labelText;

    [view addSubview:label];

    [view addSubview:slider];

    label.layer.delegate = self;

    self.view = view;
}

The app does not crash at [view addSubview:label] ; 该应用程序不会在[view addSubview:label]崩溃; it crashes after the loadView method returns. loadView方法返回后崩溃。

From the UIView documentation : UIView文档

Never change the delegate of this layer object. 永远不要更改此图层对象的委托。

Delete this line: 删除此行:

label.layer.delegate = self;

Whatever you're trying to accomplish here, you'll need to do in another way. 无论你想在这里完成什么,你都需要以另一种方式去做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我已将 Widgetkit 添加到我的应用程序中,但现在当应用程序在 Xcode 12 beta 上启动时在 iOS 13 上崩溃 - I have added Widgetkit to my app but now crash on iOS 13 when the app starts on Xcode 12 beta 设置 UILabel 背景颜色会导致应用崩溃 - Setting UILabel background color causes app to crash iOS添加菜单在非第一视图时导致崩溃 - iOS Adding Menu Causes Crash When Not First View iOS-didSelectRowAtIndexPath导致应用崩溃 - iOS - didSelectRowAtIndexPath causes crash in app iOS应用程序仅在未调试时崩溃 - iOS app crash only when not debugging 更改视图控制器时,Singleton导致应用程序崩溃 - Singleton causes app to crash when changing View Controllers iOS 15 仅视图修改器会导致 iOS 14 崩溃,即使 #available(iOS 15.0, *) - iOS 15 only view modifier causes crash on iOS 14 even with if #available(iOS 15.0, *) iOS仅在未通过XCode运行时崩溃。 Concidence? - iOS crash only when NOT running via XCode. Concidence? 添加空.xib时,XCode项目无法在IOS &lt;7上运行应用程序 - XCode project cannot run app on IOS <7 when empty .xib is added 在iOS应用中将视图添加到UIWindow时,视图控制器是什么? - What is view controller when a view is added to UIWindow in an iOS app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM