UIButton setTitleColor and setImage not working

I am using the StoryBoard feature for my iPhone project. In the storyboard, I have a view controller with a table view using prototypes. Inside the prototype cell, I have a UIButton (rounded rectangle) connected to the UIButton class of the subclass. Tableview gets its data from the array, and the data is printed in cellForRowAtIndexPath.

Inside cellForRowAtIndexPath, I conditionally change the appearance of the cell. This is when I start getting problems. I can successfully change the button text. However, if I try to change the button image using:

[cell.myButton setImage:[UIImage imageNamed:@"mynewimage.png"] forState:UIControlStateNormal]; 

the image changes, but the text disappears.

If I try to change the button text color using:

 [cell.myButton setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.77] forState:UIControlStateNormal] 

Nothing happens. The color remains the same (unless I try to change the image, in which case it will completely disappear).

I feel like I'm doing something fundamentally wrong here - do you have any suggestions?

+6
source share
5 answers

using

 - (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state; 

to change the image with text on the left

using

 btn.titleLabel.textColor = yourcolor; 

to change text color

+4
source

Define myButton as shown below and check. This may not be true in your code. The code:

 myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

This is the new part:

 [myButton setBackgroundImage:yourimage forState:UIControlStateNormal]; [myButton setTitleColor:yourcolor forState:UIControlStateNormal]; 
+3
source

This is a way to set various properties in a custom button ... You will also find that the setBackgroudimage and setTitleColor properties are set here ...

  UIButtin *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; [myButton setFrame:CGRectMake(165, 205, 65, 40)]; [myButton setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.77] forState:UIControlStateNormal]; [myButton setTitle:@"Hi....." forState:UIControlStateNormal]; [myButton addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside]; [myButton setBackgroundImage:[UIImage imageNamed:@"mynewimage.png"] forState:UIControlStateNormal]; [cell addSubview:myButton]; 

Thanks and..

+2
source

I assume that you want to change the background of the button:

 [cell.myButton setBackgroundImage:[UIImage imageNamed:@"mynewimage.png"] forState:UIControlStateNormal]; 
0
source

The text disappears because you set the image on the front of the button as the main image of the button.

 [cell.myButton setImage:[UIImage imageNamed:@"mynewimage.png"] forState:UIControlStateNormal]; 

You need to set the background image of the button so that the text is visible, since the image will be drawn on the back of your UIButton context.

So just replace the line of code above with:

 [cell.myButton setBackgroundImage:[UIImage imageNamed:@"mynewimage.png"] forState:UIControlStateNormal]; 
0
source

Source: https://habr.com/ru/post/915071/


All Articles