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

前端之家收集整理的这篇文章主要介绍了UILabel导致应用程序在添加到视图时崩溃(仅限Xcode 6和iOS 8)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已将我的项目转换为 Xcode 6,以便为iOS 8构建.但是,特定的UILabel会导致应用程序在添加到视图层次结构时崩溃.这是我收到的唯一错误日志:
-[MyViewController _contentInsetsFromFonts]: unrecognized selector sent to instance     0x16d90da0

我无法在项目中的任何位置找到contentInsetsFromFonts方法.此外,我甚至无法在网上任何地方找到它的参考,包括Apple的文档.我没有为这个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;
}

该应用程序不会在[view addSubview:label]中崩溃;它在loadView方法返回后崩溃.

解决方法

the UIView documentation开始:

Never change the delegate of this layer object.

删除此行:

label.layer.delegate = self;

无论你想在这里完成什么,你都需要以另一种方式去做.

猜你在找的iOS相关文章