I am using the StoryBoard feature for my iPhone project. In the storyboard, I have a view controller with a table view using prototype cells. Inside the prototype cell, I have a UIButton (rounded rectangle) hooked up to a sub-classed UIButton class. The tableview gets its data from an array, and the data is printed out in cellForRowAtIndexPath.
Inside cellForRowAtIndexPath, I conditionally modify the cell's appearance. This is when I start getting problems. I am able to successfully change the text of the button. However, if I attempt to change the image of the button, using:
[cell.myButton setImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
the image is changed, but the text disappears.
If I attempt to change the text color of the button, using:
[cell.myButton setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.77] forState:UIControlStateNormal]
Nothing happens whatsoever. The color remains the same (unless I also try to change the image, in which case it completely disappears).
I am sensing I am doing something fundamentally wrong here -- do you have any suggestions?
use
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
to change image with text left
use
btn.titleLabel.textColor = yourcolor;
to change text color
Define myButton as below and check.This may be the wrong in your code.The code is:
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
This is newly editted part:
[myButton setBackgroundImage:yourimage forState:UIControlStateNormal];
[myButton setTitleColor:yourcolor forState:UIControlStateNormal];
This is the way to set different properties to a Custom Button...You also find that setBackgroudimage and setTitleColor properties 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];
Thank u..
I guess what you want is to change your button background:
[cell.myButton setBackgroundImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
Your text disappears because you have set the image at button front side as button main image.
[cell.myButton setImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
You need to set the button background image so that your text would be visible as the image will be drawn at the rear side of your UIButton context.
So just replace the above line of code to :
[cell.myButton setBackgroundImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
Related
I create a UIButton like this:
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,100,100);
The button works, but the hit area is only on the text, not the entire button. I've set the background of the button to be [UIColor clearColor]. What do I need to do to make it so that the entire frame of the button is the hit area?
I suppose I could change the frame of the label to equal the button, but that seems hokey. Is there a better way?
try like this,
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(0,0,100,100);
button.backgroundColor=[UIColor clearColor];
[button setTitle:#"Set" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubview:button];
The button in question was being built up with layers. What I ended up doing was ensuring the background layer had a solid background color. Simply setting the background of the button to clear did not work in this case.
//set a background color so it is clickable
[layer0 setBackgroundColor: CGCOLORA(0, 1) ];
Setting the opaque property of the UIButton to true worked for me (iOS10)
What is the best way to achive having test showing up as normal color but the Rect button lines being not shown effectively making it transparent.
i have tried alpha settings but no luck as it makes the whole button AND text transparent, which is not what i want i would prefer to achieve this by UIButton rather then Image.
with image i struggled to link actions / outlets among views and found it easier to connect using cntrl button and linking with views.
Any working idea would be good
Try creating a custom button:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(x, y, w, h);
[btn addTarget:self action:#selector(buttonPushed:) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"Push me!" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor clearColor];
[btn setTitleColor:[UIColor colorWithRed:0.3 green:0.1 blue:0.4 alpha:1.0] forState:UIControlStateNormal];
[self.view addSubview:btn];
I'm running through a few tutorials that describe ways of applying custom images to UIButtons in iPhone programming using Obj-C.
One method i like is to place a UIButton (Round Rect Button) on a XIB using Interface Builder then in the 'viewDidLoad' method of the XIB's view controller skin the button thus:
UIImage *ButtonImageNormal = [UIImage imageNamed:#"button_blue.png"];
UIImage *StretchableButtonImageNormal = [ButtonImageNormal stretchableImageWithLeftCapWidth:24 topCapHeight:24];
[self.Button setBackgroundImage:StretchableButtonImageNormal forState:UIControlStateNormal];
UIImage *ButtonImagePressed = [UIImage imageNamed:#"button_orange.png"];
UIImage *StretchableButtonImagePressed = [ButtonImagePressed stretchableImageWithLeftCapWidth:24 topCapHeight:24];
[self.Button setBackgroundImage:StretchableButtonImagePressed forState:UIControlStateHighlighted];
The trouble with this method is that the button doesn't lose its 'Round Rect' background and at the side of the buttons you can still see the white of the 'Round Rect' button peeping through past the custom images.
If however, i create a custom button from scratch using this code:
UIButton *CustomButton = [UIButton buttonWithType:UIButtonTypeCustom];
[CustomButton setFrame:CGRectMake(20, 100, 280, 45)];
[CustomButton.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
[CustomButton setTitle:#"Custom Button" forState:UIControlStateNormal];
[CustomButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[CustomButton setBackgroundImage:[UIImage imageNamed:#"button_blue.png"] forState:UIControlStateNormal];
[CustomButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[CustomButton setBackgroundImage:[UIImage imageNamed:#"button_orange.png"] forState:UIControlStateHighlighted];
[self.view addSubview:CustomButton];
There is no such white background peeping through.
I really would like to use the 'Round Rect' method of placing and skinning buttons because i'd like to see their positions in Interface builder. Is there a method i need to use or a property i need to amend to get rid of the white background of a 'Round Rect' button in code.
Here's an image showing the problem:
I've found the answer!
Typical you spend hours hunting through manuals to find it just after posting!
Just change the button type to 'Custom' in Interface Builder and no extra code needed!
Make the default colors for the button to be "clear color", the funny black and white diagonal thing.
I must have overlooked something completely obvious?? but my button displays its image and size correctly, but I simply can't get the Title to show up.
I did a really simple test, the Title does not even show up when I do this:
CGRect frameBtn = CGRectMake(160.0f, 150.0f, 144.0f, 42.0f);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"left_halfscreen_button.png"] forState:UIControlStateNormal];
[button setTitle:#"Hello" forState:UIControlStateNormal];
[button setFrame:frameBtn];
NSLog(#"Title:%#", [button currentTitle]);
//prints "Title:Hello
[self addSubview:button];
I have a factory class that generates custom buttons for me and I thought I messed some detail up there, so I moved the above code directly into my UIView, the title is still blank.
Is this a bug or am I simply missing something right in front of my eyes.
Thank you for the extra set of eyes:)
Image overrides title, you need to make the image a background image to show the title.
[button setBackgroundImage:[UIImage imageNamed:#"left_halfscreen_button.png"]
forState:UIControlStateNormal];
I just hab spome quite similar problem... Just set the title color, I guess the current one is white ;)
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
I found the problem!
It's not Image overriding the title. It is being push off frame by the image.
Try use this:
[btn setTitleEdgeInsets:UIEdgeInsetsMake(0.0, -img.size.width, 0.0, 0.0 )];
Hope it helps!
When you use UIButton, button uses some built-in size for its frame.
You should set the frame:
button.frame = CGRectMake(0, 0, width, height);
or even better and comfortable:
[button sizeToFit];
I had a similar problem that the title was not shown. I forgot to set the frame property:
//set the position of the button
button.frame = CGRectMake(100, 170, 100, 30);
I've tried creating a UIButton with UIButtonTypeCustom. Then afterwards used the following methods to adjust the look of it
[sendButton setImage:[UIImage imageNamed:#"sendButtonOff.png"] forState:UIControlStateNormal];
[sendButton setImage:[UIImage imageNamed:#"sendButtonOn.png"] forState:UIControlStateHighlighted];
[sendButton setImage:[UIImage imageNamed:#"sendButtonDisabled.png"] forState:UIControlStateDisabled];
However the problem is, once you start setting UIImages to the states, setTitle no longer works. I think setTitle only works with non-UIButtonTypeCustom? This means I either have to put the text into the images themselves (not very robust) or subclass this and add a UILabel to the view? Sounds like alot of work :( Any ideas?
If you want the title to display over the image, try using this:
[sendButton setBackgroundImage:[UIImage imageNamed:#"sendButtonOff.png"] forState:UIControlStateNormal];
[sendButton setBackgroundImage:[UIImage imageNamed:#"sendButtonOn.png"] forState:UIControlStateHighlighted];
[sendButton setBackgroundImage:[UIImage imageNamed:#"sendButtonDisabled.png"] forState:UIControlStateDisabled];