Iphone UIButton background color - iphone

I'm creating some UIButtons programmatically in a loop but I'm having some problem with setting the background color of the button.
The color of the button always shows up as white.
But works fine with I'm only using 2 colors in the backgroundcolor.
Eg : red : 255 green:0 blue :200
Here is the code I'm using to add the button.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(80, 20 + (i*75), 200, 75);
button.layer.cornerRadius = 10;
button.layer.borderWidth = 1;
[button setTitle:#"saf" forState:UIControlStateNormal];
[button addTarget:self action:#selector(moveLocation:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundColor:[UIColor colorWithRed:255 green:180 blue:200 alpha:1]];
button.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[scrollView addSubview:button];

I believe you are building your UIColor wrong.
Try this:
[button setBackgroundColor:[UIColor colorWithRed:(255/255.0) green:(180/255.0) blue:(200/255.0) alpha:1]];

UIColor colorWithRed: green: blue
accepts CGFloats between 0.0 and 1.0
Here is the api reference.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIColor_Class/Reference/Reference.html

Related

UIButton with rounded corners using square setBackgroundImage

So I create a button. I want it to have rounded corners with out making rounded .png files
I can set the background image ok. I can round the corners ok. However, when I set the background Image the rounded corners go away.
I did this once before a long time ago but can not seem to get it working again. How can this be done?
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self action:#selector(goSpecials)
forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:#"Specials" forState:UIControlStateNormal];
button1.titleLabel.font = [UIFont fontWithName:#"Arial-BoldMT" size:(15.0)];
[button1 setTitleColor:[self colorWithHexString:buttonColor] forState:UIControlStateNormal];
button1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: #"button.png"]];
//[button1 setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
button1.frame = CGRectMake(20.0, 232.0, 135.0, 32.0);
button1.layer.borderColor = [UIColor blackColor].CGColor;
button1.layer.borderWidth = 0.5f;
button1.layer.cornerRadius = 8.0f;
[self.view addSubview:button1];
button.png:
Add the maskToBounds property to your button.
button1.layer.masksToBounds = YES;

transparent UIButton in iOS 5 sdk

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

UIButton setTitleColor with RGB values not working

I Have to set Title color for ffe1b1 to a UIButton.RGB value of ffe1b1 is 255,225,177.
I am trying this code, but its not properly reflect. I search on net Color components are floats between 0.0 and 1.0 !
So what is proper way for giving That RGB Values.Thanks
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom] ;
btn.frame = CGRectMake(134.0, 193.0, 80.0, 30.0);
[btn setBackgroundImage:img forState:UIControlStateNormal];
[btn setTitle:#"Contact" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor colorWithRed:255.0 green:225.0 blue:177.0 alpha:0.6 ]forState: UIControlStateNormal];
Change the line like this
[btn setTitleColor:[UIColor colorWithRed:255.0 green:225.0 blue:177.0 alpha:0.6 ]forState: UIControlStateNormal];
to
[btn setTitleColor:[UIColor colorWithRed:(255.0/255) green:(225.0/255) blue:(177.0/255) alpha:0.6 ]forState: UIControlStateNormal];
in objective c you cannot directly use RGB values. follow this link:
http://www.touch-code-magazine.com/web-color-to-uicolor-convertor/

iPad - set text to a button programmatically

I'm new to this iPad business, and I'm creating some buttons inside a scrollview programmatically based on the contents of an XML file. I have this code on a for:
float x = (SLIDER_ELEMENT_HEIGHT * i) + 20;
CGRect frame = CGRectMake(x, 0, SLIDER_ELEMENT_WIDTH, SLIDER_ELEMENT_HEIGHT);
UIButton *button = [[UIButton alloc] initWithFrame:frame];
UIColor *bgColor = [[UIColor alloc] initWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
button.backgroundColor = bgColor;
NSString *titleForButton = [NSString stringWithFormat: #"This is my title"];
[button setTitle:titleForButton forState:(UIControlStateNormal | UIControlStateApplication | UIControlStateHighlighted)];
UIColor *fgColor = [[UIColor alloc] initWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
[button setTitleColor:fgColor forState:(UIControlStateNormal | UIControlStateApplication | UIControlStateHighlighted)];
[button addTarget:self action:#selector(buttonMethod:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
[scrl_lastIssues addSubview:button];
Now, the method listener I'm appending is working OK, but the text of the button never shows up... what am I doing wrong?
Try change the line:
[button setTitle:titleForButton forState:(UIControlStateNormal | UIControlStateApplication | UIControlStateHighlighted)];
to:
[button setTitle:titleForButton forState:UIControlStateNormal];
the forState parameter does not work the way you currently expected, it works like:
[button setTitle:#"Normal" forState:UIControlStateNormal];
[button setTitle:#"Highlighted" forState:UIControlStateHighlighted];
Also, if you only set the UIControlStateNormal text..it will be the text for all the control states that you have separated by bit wise ORs, which is, I believe, the functionality you are searching for.
[button setTitle:#"Return" forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor orangeColor]];
And if you want to set the same title for all the states then do this:
[button setTitle:titleForButton forState:UIControlStateNormal];
[button setTitle:titleForButton forState:UIControlStateHighlighted];

Why can't I set my button's shadow colour?

I'm trying to set the shadow colour on a UIButton, but all I seem to be able to get is a mid grey.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(20, 20, 200, 100);
[button setTitle:#"a" forState:UIControlStateNormal];
[button addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:96]];
// set up the button colours
button.titleLabel.shadowColor = [UIColor blueColor];
[button.titleLabel setShadowOffset:CGSizeMake(5.0f, 5.0f)];
[self.view addSubview:button];
alt text http://img.skitch.com/20090825-xur3112ni5q2wrwwiix4jbcwc5.png
Am I setting the wrong property, or is it the way that I'm setting shadowColor that's wrong?
Thanks.
Have you tried:
[button setTitleShadowColor:[UIColor blueColor] forState:UIControlStateNormal];