简体   繁体   中英

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.

PS I am not using xib.

any help would be highly appreciated.

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:] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM