Coldsteel48
Coldsteel48

Reputation: 3522

UIButton with image and text doesn't work

I searched for solution I saw couple similar posts here but I couldn't fin an answer. I am trying to add an UIButton that sets with image (background colour only is good too) and i want to add text on this image

so far this is my code :

        labelSorry.text = @"לא נמצאו תוצאות.";//add text
        //add button.
        Request =[UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *requestimg = [UIImage imageNamed:@"requestcontact_resize2.png"];
        Request.frame = CGRectMake(0.0, 120, 320.0, 30.0);
        Request.titleLabel.textAlignment = NSTextAlignmentRight;
       // Request.titleLabel.textColor = UIColorFromRGB(0xffffff);
        [Request setBackgroundImage:requestimg forState:UIControlStateNormal];
        Request.imageView.image = [UIImage imageNamed:@"requestcontact_resize2.png"];//see no image :( this is kidding me

      //  [Request setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
        UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 120, 320.0, 30.0)];///You can replace it with your own dimensions.
        label.textColor = UIColorFromRGB(0xffffff);
        label.text = @"בקש פרטי איש קשר";
        [Request addSubview:label];

        //[Request setBackgroundColor:UIColorFromRGB(0x704c9f)];
        Request.titleLabel.text = @"בקש פרטי איש קשר";
        [Request addTarget:self action:@selector(requestDetails) forControlEvents:UIControlEventTouchUpInside];
        [noResultsView addSubview:Request];
        [self.view addSubview:noResultsView];

I can see the background of the button , but I can't see any text on it :( please maybe someone can enlighten me ?

as you can see I have tried various solutions(I put them in comment) but none worked.

P.S. I am not using xib.

any help would be highly appreciated.

Upvotes: 1

Views: 1533

Answers (1)

alex_izh
alex_izh

Reputation: 527

You should use [button setImage:forState:] and [button setTitle:forState:]. If you want set background image for button you should use [button setBackgroundImage:forState:]. Also, if you want, you can change position for image and title, for this use [button setImageEdgeInsets:] and [btn setTitleEdgeInstes:].

Upvotes: 3

Related Questions