我试图在uiImageVIew顶部添加uiLabel,这是背景图像。在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];


}

最佳答案

检查这个

- (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];


}

10-08 06:06