iphone customizing UIButtons - iphone

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

Related

UIButton - Programmatic creation issue

I am creating a UIButton programmatically as below.
- (void)viewDidLoad
{
[paymentsHomeView setFrame:CGRectMake(0, 0, 320, 520)]; // paymentsHomeView is added and referenced in IB
UIButton *paymentButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 45, 300, 41)];
paymentButton.titleLabel.text = #"Button";
paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[paymentButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.paymentsHomeView addSubview:paymentButton];
[super viewDidLoad];
}
-(void) buttonClicked {
NSLog(#"buttonClicked");
}
There is no output for the above code. The button is not created.
Whereas the same works when I create UITextField.
Help me out. Thanks in advance..!
you need to set frame for button after init statement and you need to set title for button in different way
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Button" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
Try this one my friend..
UIButton *paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[paymentButton addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[paymentButton setTitle:#"Show View" forState:UIControlStateNormal];
paymentButton.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.paymentsHomeView addSubview:paymentButton];
let me know is it working or not...!!!
Happy Coding!!!!
You are creating button here:
UIButton *paymentButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 45, 300, 41)];
and again on this line you create another button so this overwrite the previous one, so you need to set the frame again:
paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
What I suggest do this:
paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[paymentButton setFrame:CGRectMake(10, 45, 300, 41)];
paymentButton.titleLabel.text = #"Button";
[paymentButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.paymentsHomeView addSubview:paymentButton];
I have notated some problems here:
[paymentsHomeView setFrame:CGRectMake(0, 0, 320, 520)]; // paymentsHomeView is added and referenced in IB
UIButton *paymentButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 45, 300, 41)];
paymentButton.titleLabel.text = #"Button";
// You just allocated a paymentButton above. Without ARC it is leaked here:
paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// Now you have assigned an auto released button to paymentButton. It does not have a frame or text.
[paymentButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.paymentsHomeView addSubview:paymentButton];
Try this:
UIButton *paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
paymentButton.frame = CGRectMake(10, 45, 300, 41);
paymentButton.titleLabel.text = #"Button";
[paymentButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.paymentsHomeView addSubview:paymentButton];
For Custom Button use bellow code...
Remove this line
paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
and add this line which shows button with black background
paymentButton.titleLabel.textColor = [UIColor whiteColor];// set text color which you want
[paymentButton setBackgroundColor:[UIColor blackColor]];// set back color which you want
And for RoundRect Button use bellow code
UIButton *paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
paymentButton.frame = CGRectMake(10, 45, 300, 41);
paymentButton.titleLabel.text = #"Button";
[paymentButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.paymentsHomeView addSubview:paymentButton];
[self.paymentsHomeView bringSubviewToFront:paymentButton];

Add UIButton programmatically from a IBAction

I am trying to make an action where every time you press a button, it adds a button to the view. Nothing I have tried works.
It would be much appreciated if someone could clear up what to do.
This is the most recent attempt at my button-adding button:
-(IBAction) addButton{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10,10,50,50);
button.tag = 1;
[button addTarget:self
action:#selector(buttonTouched:)
forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10,10,50,50);
button.tag = 1;
[button addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Try the above codes.
you need to add this
#import <QuartzCore/CoreAnimation.h>
then Do the follow this
-(IBAction)buttonTouched:(id)sender{
NSLog(#"New Button Clicked");
}
-(IBAction)addButton:(id)sender
{
UIButton *btnallstates;
UIImage *statesbtnimg=[UIImage imageNamed:#"1.png"];
btnallstates=[[UIButton buttonWithType:UIButtonTypeCustom]retain];
btnallstates.tag=1;
btnallstates.frame=CGRectMake(103, 127, 193, 31);
[btnallstates.layer setBorderWidth:0];
[btnallstates addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
[btnallstates setBackgroundImage:statesbtnimg forState:UIControlStateNormal];
[self.view addSubview:btnallstates];
}
Your code is correct. Only change this line
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
to
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
to show the button on the view.
UIButtonTypeCustom does not show up although it is added to the view.
Your can understand this by changing the color of the button.
e.g change the code to this.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor=[UIColor redColor];
Hope it works. Enjoy.
Try this
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];

How to add the button in uitableviewcell in ipad

I am new in ipad development i want to add the button in uitableviewcell but it is not displaying. how to add this button in tableview cell. I am writing this code in cellforRowAtIndexPath My code is:
UIButton *cellImgButton = [[UIButton alloc]initWithFrame:CGRectMake(300, 350, 40, 40)];
cellImgButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"remove.png"];
[cellImgButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[cellImgButton addTarget:self action:#selector(cellImgButton:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cellImgButton];
Thanks in Advance:
The problem is in your button's frame. Try this :
UIButton *cellImgButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cellImgButton setFrame:CGRectMake(0, 5, 40 , 40) ];
UIImage *buttonImage = [UIImage imageNamed:#"remove.png"];
[cellImgButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[cellImgButton addTarget:self action:#selector(cellImgButton:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cellImgButton];
Try this code. You mentioned the 'Y axis' is 350. Please check you row height. And give the 'Y' axis related to your row height.
UIButton *cellImgButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cellImgButton setFrame:CGRectMake(10, 5, 40 , 40) ];
[cellImgButton setBackgroundImage:[UIImage imageNamed:#"remove.png"] forState:UIControlStateNormal];
[cellImgButton addTarget:self action:#selector(cellImgButton:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cellImgButton];
You can just create a prototype cell in interface builder and drag a UIButton in the cell. after that create a new UITableViewCell class and link the button.
don't forget to add a identifier to the cell
1)check Table cell height
2)Check frame size of button (means x and y axies)

How do I create a simple button without using the Interface builder?

How do I create a simple button without using the Interface builder?
You would need to declare a UIButton like this:
UIButton *newButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
[newButton setTitle:#"Some Button" forState:UIControlStateNormal];
You would then add the button to a view -
[self.view addSubview: newButton]; // (could be another view other then self.view)
And finally, if you want some action to occur when pressing that button, you would add:
[newButton addTarget:self action:#selector(doSomething)
forControlEvents:UIControlEventTouchUpInside];
and of course declare that function:
- (void) doSomething
{
NSLog(#"Button pressed");
}
And of course don't forget to release the button.
Try with below
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
playButton.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
[playButton setTitle:#"Play" forState:UIControlStateNormal];
playButton.backgroundColor = [UIColor clearColor];
[playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:#"blueButton.png"];
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:#"whiteButton.png"];
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[playButton addTarget:self action:#selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playButton];
like this-
custom button -
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 30.0f)];
system buttom -
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

Problem in UIButton in iPhone programming

Can you give me some links or example programs to create UIButtons on a view and on action on it, we need to create one more button on other view..
plz help
-(void)action:(id)sender
{
}
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
button.frame = CGRectMake(x, y, width, height);
button.titleLabel.font = [UIFont systemFontOfSize: 30];
[button setTitle:#"TITLE"
forState:UIControlStateNormal];
[button addTarget:self
action:#selector(action:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];