ios:如何在背景图像上添加uiLabel?

时间:2014-11-10 05:13:01

标签: ios objective-c uiimageview addsubview

我正在尝试将uiLabel添加到作为背景图像的uiImageVIew之上。当在uiImageView上添加uiLabel时,uilabel在测试和运行时无法使用。如果这样做,你能告诉我替代方案或预防措施吗?

以下是我的代码:

@implementation CoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];

   _container = [[UIView alloc] initWithFrame:[self.view bounds]];

    [self setTapDemo :[UIImage imageNamed:@"cover_1_d.png"] ] ;
}



- (void) setTapDemo: (UIImage *) cover {

    _image = [UIImage imageNamed:@"shaded_region.png"];
    _imageShaded = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [_imageShaded setImage:_image];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [imageView setImage: cover ];
    _imageShaded.userInteractionEnabled = YES;
    UITapGestureRecognizer *myGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchesBegan:)];
    myGesture.numberOfTapsRequired = 1;
    myGesture.delegate=self;
    [_imageShaded addGestureRecognizer:myGesture];
    [_imageShaded setContentMode:UIViewContentModeScaleAspectFill];

    NSString * text = @"Quad name:";

    CGSize labelSize = CGSizeMake(60, CGFLOAT_MAX);
    UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(91, 15, labelSize.width, labelSize.height)];
    fromLabel.text = text;
    fromLabel.numberOfLines = 1;
    fromLabel.lineBreakMode = NSLineBreakByWordWrapping;
    fromLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
    fromLabel.adjustsFontSizeToFitWidth = YES;
    fromLabel.minimumScaleFactor = 10.0f/12.0f;
    fromLabel.clipsToBounds = YES;
    fromLabel.backgroundColor = [UIColor clearColor];
    fromLabel.textAlignment = NSTextAlignmentLeft;
    [_imageShaded addSubview:fromLabel];
    [imageView addSubview:fromLabel];


    [_container addSubview:_imageShaded];
    [_container addSubview:imageView];
    [_container addSubview:fromLabel];

    [self.view addSubview:_container];
    [self.view sendSubviewToBack:_container];


}

2 个答案:

答案 0 :(得分:1)

检查此

- (void) setTapDemo: (UIImage *) cover {

    _image = [UIImage imageNamed:@"images.jpg"];
    _imageShaded = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height)];
    [_imageShaded setImage:_image];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height)];
    [imageView setImage: cover ];
    _imageShaded.userInteractionEnabled = YES;
    UITapGestureRecognizer *myGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchesBegan:)];
    myGesture.numberOfTapsRequired = 1;
    myGesture.delegate=self;
    [_imageShaded addGestureRecognizer:myGesture];
    [_imageShaded setContentMode:UIViewContentModeScaleAspectFill];

    NSString * text = @"Quad name:";
   // CGSize labelSize = CGSizeMake(60, CGFLOAT_MAX);
    UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(91, 15, 300, 40)];
    fromLabel.text = text;
    fromLabel.textColor = [UIColor greenColor];
    fromLabel.numberOfLines = 1;
    fromLabel.lineBreakMode = NSLineBreakByWordWrapping;
    fromLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
    fromLabel.adjustsFontSizeToFitWidth = YES;
    fromLabel.minimumScaleFactor = 10.0f/12.0f;
    fromLabel.clipsToBounds = YES;
    fromLabel.backgroundColor = [UIColor clearColor];
    fromLabel.textAlignment = NSTextAlignmentLeft;



    [_container addSubview:_imageShaded];
    [_container addSubview:imageView];


    [self.view addSubview:_container];
    [self.view sendSubviewToBack:_container];
    [imageView addSubview:fromLabel];


}

答案 1 :(得分:0)

您在不同的视图中多次添加UILabel。你应该一次在一个视图上添加它;

//[_imageShaded addSubview:fromLabel];   // 1
//[imageView addSubview:fromLabel];      // 2


[_container addSubview:_imageShaded];
[_container addSubview:imageView];
//[_container addSubview:fromLabel];     //3