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;
Related
I have 3 UIbuttons in my with background image in my app...
for all those 3 buttons if images are assigned only lower half portion of button is clickable and upper half doesn't work at all..
any idea why ?
I tried to put color for buttons to check if they have right CGRect but it looks proper...
Here is some code
UIButton* segment=[[UIButton alloc] initWithFrame:CGRectMake(0+(i* segmentWidth) , 0,segmentWidth, 34)];
segment.backgroundColor = [UIColor redColor];
[segment addTarget:self action:#selector(segmentActionButton:) forControlEvents:UIControlEventTouchUpInside];
[segment setTag:i];
segment.titleLabel.font=[UIFont fontWithName:#"Arial" size:12.0];
segment.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
[segment setTitle:[mItems objectAtIndex:i] forState:UIControlStateNormal];
[segment setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[segment setBackgroundImage:[UIImage imageNamed:SEGMENT_IMAGE_INACTIVE_MAP] forState:UIControlStateNormal];
[segment setBackgroundImage:[UIImage imageNamed:SEGMENT_IMAGE_ACTIVE_MAP] forState:UIControlStateHighlighted];
[self addSubview:segment];
[mButtonArray addObject:segment];
[segment release];
Hope it helps..
May be the error occurs because you are creating button in alloc+initWithFrame way. I'm not sure but try to create button in such way:
UIButton* segment = [UIButton buttonWithType:UIButtonTypeCustom];
segment.frame = CGRectMake(0+(i* segmentWidth) , 0,segmentWidth, 34);
And remove line [segment release]; as you will create autoreleased object.
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
how to customize the UIButton so that it will display other than the default button style.
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(10.0, 20.0, 50.0, 50.0); // You can change the x, y, width, height.
[btn setImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateNormal];
[self.view addSubView:btn];
You can do something like this:
UIImage *btnImage = [UIImage imageNamed:#"images-01.png"];
UIButton *btnGo = [UIButton buttonWithType:UIButtonTypeCustom];
btnGo.frame = CGRectMake(85,123, 100, 26);
btnGo.backgroundcolor = [UIColor clearColor];
btnGo. setImage:btnImage forState: UIcontrolStateNormal];
[btnGo.titleLabel setFont: [UIFont systemFontOfSize:14]];
btnGo addTarget: self action:#selector(btnGoClicked) forControlEvents: UIControlEventTouchUpInside];
You can use this
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame = CGRectMake(899, 5, 82, 55);
customButton.tag = 123;
[customButton addTarget:self action:#selector(buttonToggleForHide) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:customButton];
and make event for that using
-(void)buttonToggleForHide{...}
In interface build simply change the type from rounded rect to custom and then specify a new background image.
In code:
UIButton* b = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
Set the background image on it, set Type=Custom in interface builder.
Do you mean display button with custom image? You can try this:
UIButton *button = [[UIButton alloc]initWithImage:[UIImage imageNamed:#"button.png"]];
[button release];
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame = urRequiredFrame;
[button setImage:yourCustomImage forState:UIControlStateNormal];
button addTarget:self action:#selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubView:button];
#shabbir 1st select the button then press cmd+1 change the button type to custom
if it right select my answer
other way is that
UIButton* b = [UIButton buttonWithType:UIButtonTypeCustom];
And if you want to call image on button then use this
UIButton *button = [[UIButton alloc]initWithImage:[UIImage imageNamed:#"button.png"]];
[button release];
I am having a button using IB. Now i want to add an image to the button programmatically. how can I set the button's size of image's size like we do in IB Layout -> Size to Fit . I want to do it programmatically
Thanks..
You can add an image to the button using UIButton's setImage:forState: method and then you set the content sizing using UIView's contentMode property.
An example would look like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img = [UIImage imageNamed:#"myImage.png"];
button.frame = CGRectMake(20, 100, img.size.width, img.size.height);
[button setImage:img forState:UIControlStateNormal];
[button setImage:img forState:UIControlStateHighlighted];
[button setImage:img forState:UIControlStateSelected];
button.contentMode = UIViewContentModeScaleToFill; //Look up UIViewContentMode in the documentation for other options
[self.view addSubview:button];
[yourButton setImage:yourImage forState:UIControlStateNormal];
yourButton.contentMode = UIViewContentModeScaleToFill;
where the the values for contentMode can be any one of the below
typedef enum {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit, // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill, // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw, // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter, // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
} UIViewContentMode;
I think this could help you
Sample Code,
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img1 = [UIImage imageNamed:#"image1.png"];
btn.frame = CGRectMake(20.0 , 270.0, img1.size.width, img1.size.height);
[btn setImage:img1 forState:UIControlStateNormal];
UIImage *img2 = [UIImage imageNamed:#"image2.png"];
[btn setImage:img2 forState:UIControlStateHighlighted];
[btn setImage:img2 forState:UIControlStateSelected];
[btn addTarget:self action:#selector(Action:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
Use the sizeToFit method of UIButton (UIView actually).
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];