How do I create a basic UIButton programmatically?
How can I create a basic UIButton
programmatically? For example in my view controller, when executing the viewDidLoad
method, three UIButton
s will be created dynamically and its layout or properties are set.
objective-c uibutton programmatically-created
add a comment |
How can I create a basic UIButton
programmatically? For example in my view controller, when executing the viewDidLoad
method, three UIButton
s will be created dynamically and its layout or properties are set.
objective-c uibutton programmatically-created
webindream.com/how-to-add-uibutton-programmatically-in-swift has a good tutorial to add uibutton programmatically
– farhad rubel
Sep 26 '16 at 11:10
Create UIButton in Swift, stackoverflow.com/questions/24102191/…
– iOS
Jan 11 at 12:10
add a comment |
How can I create a basic UIButton
programmatically? For example in my view controller, when executing the viewDidLoad
method, three UIButton
s will be created dynamically and its layout or properties are set.
objective-c uibutton programmatically-created
How can I create a basic UIButton
programmatically? For example in my view controller, when executing the viewDidLoad
method, three UIButton
s will be created dynamically and its layout or properties are set.
objective-c uibutton programmatically-created
objective-c uibutton programmatically-created
edited Nov 6 '17 at 5:00
Leo Dabus
133k31271346
133k31271346
asked Sep 4 '09 at 11:44
domlaodomlao
6,3152478119
6,3152478119
webindream.com/how-to-add-uibutton-programmatically-in-swift has a good tutorial to add uibutton programmatically
– farhad rubel
Sep 26 '16 at 11:10
Create UIButton in Swift, stackoverflow.com/questions/24102191/…
– iOS
Jan 11 at 12:10
add a comment |
webindream.com/how-to-add-uibutton-programmatically-in-swift has a good tutorial to add uibutton programmatically
– farhad rubel
Sep 26 '16 at 11:10
Create UIButton in Swift, stackoverflow.com/questions/24102191/…
– iOS
Jan 11 at 12:10
webindream.com/how-to-add-uibutton-programmatically-in-swift has a good tutorial to add uibutton programmatically
– farhad rubel
Sep 26 '16 at 11:10
webindream.com/how-to-add-uibutton-programmatically-in-swift has a good tutorial to add uibutton programmatically
– farhad rubel
Sep 26 '16 at 11:10
Create UIButton in Swift, stackoverflow.com/questions/24102191/…
– iOS
Jan 11 at 12:10
Create UIButton in Swift, stackoverflow.com/questions/24102191/…
– iOS
Jan 11 at 12:10
add a comment |
34 Answers
34
active
oldest
votes
1 2
next
Here's one:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[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);
[view addSubview:button];
Thanks! can you explain to me what is aMethod?
– domlao
Sep 4 '09 at 12:43
5
buttonWithType: returns an autoreleased instance of UIButton
– Allyn
Apr 14 '11 at 21:05
259
Handy Tip: If you don't want your button to activate as soon as you touch it, use UIControlEventTouchUpInside instead of UIControlEventTouchDown - this will wait for the user to lift their finger, giving them the option of cancelling by dragging away (recommended in Apple Human Interface Guidelines)
– ayreguitar
Jul 29 '11 at 15:33
9
I do recommend using UIControlEventTouchUpInside for most cases. I have used UIControlEventTouchDown in some games and music playing apps (ClefTunes and ClefNotes) so that the touch immediately generates music notes.
– mahboudz
Sep 30 '11 at 17:47
45
Note for the people like me that struggled with this--@selector(aMethod:) refers to a method with an argument. @selector(aMethod) refers to a method with no argument. If your method signature looks like -(void) aMethod; and you use @selector(aMethod:), your app will crash.
– Chad Schultz
Oct 16 '12 at 17:31
|
show 7 more comments
- (void)viewDidLoad {
[super viewDidLoad];
[self addMyButton]; // Call add button method on view load
}
- (void)addMyButton{ // Method for creating button, with background image and other properties
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
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];
}
great thanks, can you arrange the code? thanks =)
– domlao
Mar 2 '10 at 23:31
@ sasayins: I have arranged the code, you can call addMyButton method on viewDidLoad.
– Xorsat
Mar 4 '10 at 17:15
1
thanks... but why use viewController:viewDidLoad? it seems like this stuff (setting up of the view/adding buttons) should be done in the view instead of the view controller? thanks
– Rakka Rage
Jun 19 '11 at 4:44
@rakkarage The addMyButton() method is generic method, you can use it from event.
– Xorsat
Jul 1 '11 at 11:56
1
A much better answer than the one accepted, because it shows context for the code.
– Morgan Wilde
May 9 '13 at 9:44
|
show 1 more comment
Objective-C
UIButton *but= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[but addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[but setFrame:CGRectMake(52, 252, 215, 40)];
[but setTitle:@"Login" forState:UIControlStateNormal];
[but setExclusiveTouch:YES];
// if you like to add backgroundImage else no need
[but setbackgroundImage:[UIImage imageNamed:@"XXX.png"] forState:UIControlStateNormal];
[self.view addSubview:but];
-(void) buttonClicked:(UIButton*)sender
{
NSLog(@"you clicked on button %@", sender.tag);
}
Swift
let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
myButton.setTitle("Hai Touch Me", forState: .Normal)
myButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
myButton.frame = CGRectMake(15, 50, 300, 500)
myButton.addTarget(self, action: "pressedAction:", forControlEvents: .TouchUpInside)
self.view.addSubview( myButton)
func pressedAction(sender: UIButton!) {
// do your stuff here
NSLog("you clicked on button %@", sender.tag)
}
Swift3 and above
let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
myButton.setTitle("Hi, Click me", for: .normal)
myButton.setTitleColor(UIColor.blue, for: .normal)
myButton.frame = CGRect(x: 15, y: 50, width: 300, height: 500)
myButton.addTarget(self, action: #selector(pressedAction(_:)), for: .touchUpInside)
self.view.addSubview( myButton)
func pressedAction(_ sender: UIButton) {
// do your stuff here
print("you clicked on button (sender.tag)")
}
swift 3: gives errorexped declaration
at myButton.setTitle("Hi, Click")
– Xcodian Solangi
Dec 20 '17 at 9:14
@XcodianSolangi -- is notmyButton.setTitle("Hi, Click")
the syntax ismyButton.setTitle("Hi, Click me", for: .normal)
i tried my code i am not faced any issues
– Anbu.Karthik
Dec 20 '17 at 9:24
i write as it as you defined, now i have defined constant in VC and other parts inside the function
– Xcodian Solangi
Dec 20 '17 at 10:01
add a comment |
To add a button programatically to your controller's view, use the following:
-(void)viewDidLoad
{
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 50);
[btn setTitle:@"Hello, world!" forState:UIControlStateNormal];
[self.view addSubview:btn];
}
To add three of these, rinse and repeat.
It seems it is not a good idea (??) to do initialization of coordinates of view in viewDidLoad: stackoverflow.com/questions/17882199/… ???
– user2054339
Jul 30 '13 at 13:46
3
Correct, there are better approaches now than there were in 2009.
– Jason
Jul 31 '13 at 19:47
add a comment |
Here you can create dynamically a UIButton:
//For button image
UIImage *closebtnimg = [UIImage imageNamed:@"close_btn.png"];
//Custom type button
btnclose = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
//Set frame of button means position
btnclose.frame = CGRectMake(103, 257, 94, 32);
//Button with 0 border so it's shape like image shape
[btnclose.layer setBorderWidth:0];
//Set title of button
[btnclose setTitle:@"CLOSE" forState:UIControlStateNormal];
[btnclose addTarget:self action:@selector(methodname:) forControlEvents:UIControlEventTouchUpInside];
//Font size of title
btnclose.titleLabel.font = [UIFont boldSystemFontOfSize:14];
//Set image of button
[btnclose setBackgroundImage:closebtnimg forState:UIControlStateNormal];
add a comment |
Come on, it's 2014! Why isn't code block evaluation assignment being used yet, as trends show it's the future!
UIButton* button = ({
//initialize button with frame
UIButton* button = [[UIButton alloc] initWithFrame:({
CGRect frame = CGRectMake(10.0, 10.0, 200.0, 75.0);
frame;
})];
//set button background color
[button setBackgroundColor:({
UIColor* color = [UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0];
color;
})];
//set button title for state
[button setTitle:({
NSString* string = [NSString stringWithFormat:@"title words"];
string;
}) forState:({
UIControlState state = UIControlStateNormal;
state;
})];
//set selector
[button addTarget:self action:({
SEL select = @selector(method:);
select;
}) forControlEvents:({
UIControlEvents event = UIControlEventTouchUpInside;
event;
})];
//return button
button;
});
[self.view addSubview:button];
whoa!
Or the exact results can be accomplished as such:
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(10.0, 10.0, 200.0, 75.0)];
[button setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0]];
[button setTitle:@"title words" forState:UIControlStateNormal];
[button addTarget:self action:@selector(method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
6
A little over the top, but it's an interesting new feature.
– Paul Solt
Jan 23 '14 at 5:52
That's the point, to show off code block evaluation and ANSWER the question. It's annoying this is getting -1's...
– John Riselvato
Jan 23 '14 at 14:20
Levelled it out...
– StuartM
Jan 23 '14 at 17:26
1
you will have more upvotes if you give explanation for why code block evaluation assignment is important...
– Fahim Parkar
Mar 1 '16 at 5:53
3
I comment with context to Come on, it's 2014!
– Fahim Parkar
Mar 2 '16 at 5:01
|
show 1 more comment
'action:@selector(aMethod:)'
write method like this :
- (void)aMethod:(UIButton*)button
{
NSLog(@"Button clicked.");
}
It works for me. Thanks. KS.
I was looking for this , thanks a lot
– smoothumut
Jan 20 '15 at 14:11
add a comment |
Objective-C
// Create the Button with RoundedRect type
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// instend of "Click Me" you can write your own message/Label
[mybutton setTitle:@"Click Me" forState:UIControlStateNormal];
// create the Rectangle Frame with specified size
mybutton.frame = CGRectMake(10, 10, 300, 140); // x,y,width,height [self.view addSubview:mybutton];// add button to your view.
Swift
let button = UIButton(type: UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.greenColor()
button.setTitle("Test Button", forState: UIControlState.Normal)
self.view.addSubview(button)
add a comment |
try this code to create a button and repeat it for 2 more times with different coordinates and the method(myButtonClick) is called when the button is pressed
UIButton *editButton = [UIButton buttonWithType: UIButtonTypeCustom];
editButton.frame = CGRectMake(0, 0, width, height);
[editButton setBackgroundImage: editButtonImage forState: UIControlStateNormal];
[myButton addTarget:self action:@selector(myButtonClick:) forControlEvents:UIControlEventTouchUpInside];
editButton.adjustsImageWhenHighlighted = YES;
editButton.titleLabel.text = @"Edit";
editButton.titleLabel.textColor = [UIColor whiteColor];
editButton.titleLabel.textAlignment = UITextAlignmentCenter;
editButton.titleLabel.font = [UIFont fontWithName: @"Helvetica" size: 14];
[self.view addSubview: editButton];
-(void) myButtonClick:(NSString *)myString{
NSLog(@"you clicked on button %@", myString);
}
What is myString set to?
– Doug Null
Aug 15 '13 at 14:58
that is the string to pass as a parameter just for additional info, you can also use that method like this -(void) myButtonClick{ NSLog(@"you clicked on button"); } but be sure that while calling the method remove the : just use like this [myButton addTarget:self action:@selector(myButtonClick) forControlEvents:UIControlEventTouchUpInside];
– ashokdy
Aug 15 '13 at 15:16
+1 for showing how to set multiple settings instead of just creating a button.
– Matt Wagner
Mar 21 '14 at 1:09
thnQ @MattWagner
– ashokdy
Mar 25 '14 at 7:06
add a comment |
You can just put the creator instance within a loop and dynamically add names from an array if you so wish.
yup i tried mahboudz, and at the same time, i tried the looping. thanks
– domlao
Mar 2 '10 at 23:31
add a comment |
Check out this code:
CGRect frameimg = CGRectMake(15, 46, 55,70);
UIButton *SelectionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
SelectionButton.frame=frameimg;
SelectionButton.tag=i;
[SelectionButton setTitle:[SelectionArray objectAtIndex:0] forState:UIControlStateNormal];
[SelectionButton addTarget:self action:@selector(BtnSelected:)
forControlEvents:UIControlEventTouchUpInside];
[SelectionButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12.0]];
SelectionButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
SelectionButton.titleLabel.numberOfLines = 2;
SelectionButton.titleLabel.textAlignment = NSTextAlignmentCenter;
[SelectionButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
[SelectionButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[SelectionButton setShowsTouchWhenHighlighted:YES];
[self.view addSubview:SelectionButton];
I hope you find this code useful.
add a comment |
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(10.0, 100.0, 300.0, 20.0);
[self.view addSubview:button];
add a comment |
-(UIButton *)addButton:(NSString *)title :(CGRect)frame : (SEL)selector :(UIImage *)image :(int)tag{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = frame;
[btn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:title forState:UIControlStateNormal];
[btn setImage:image forState:UIControlStateNormal];
btn.backgroundColor = [UIColor clearColor];
btn.tag = tag;
return btn;
}
and you can add it to the view:
[self.view addSubview:[self addButton:nil :self.view.frame :@selector(btnAction:) :[UIImage imageNamed:@"img.png"] :1]];
add a comment |
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
add a comment |
This is an example as well to create three buttons. Just move their location.
UIImage *buttonOff = [UIImage imageNamed:@"crysBallNorm.png"];
UIImage *buttonOn = [UIImage imageNamed:@"crysBallHigh.png"];
UIButton *predictButton = [UIButton alloc];
predictButton = [UIButton buttonWithType:UIButtonTypeCustom];
predictButton.frame = CGRectMake(180.0, 510.0, 120.0, 30.0);
[predictButton setBackgroundImage:buttonOff forState:UIControlStateNormal];
[predictButton setBackgroundImage:buttonOn forState:UIControlStateHighlighted];
[predictButton setTitle:@"Predict" forState:UIControlStateNormal];
[predictButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
[predictButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:predictButton];
add a comment |
You can create button by this code.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchDragInside];
[btn setTitle:@"click button" forState:UIControlStateNormal];
btn.frame = CGRectMake(50, 100, 80, 40);
[self.view addSubview:btn];
Here is the button action method
-(void)btnAction
{
NSLog(@"button clicked");
}
add a comment |
For Swift 2.0:
let btnObject : UIButton = UIButton()
btnObject.frame = CGRect(x: 8, y: 89, width: 70, height: 22)
btnObject.titleLabel?.font = UIFont(name: "Helvetica Neue", size: 13)
btnObject.titleLabel?.textColor = UIColor.whiteColor()
btnObject.backgroundColor = UIColor(red: 189/255, green: 176/255, blue: 0/255, alpha: 1)
btnObject.titleLabel?.textAlignment = NSTextAlignment.Center
btnObject.addTarget(self, action: "btnbtnObjectClick:", forControlEvents: UIControlEvents.TouchUpInside)
subView.addSubview(btnObject)
add a comment |
For creating UIButton programmatically we can create in both objective c and swift
SWIFT 3
let buttonSwift = UIButton(type: UIButtonType.system) as UIButton
//OR
let buttonSwift = UIButton(type: UIButtonType.Custom) as UIButton
//Set Frame for Button
buttonSwift.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
//Set title for button
buttonSwift.setTitle("ClickMe", for: .normal)
//If you want to set color for button title
buttonSwift.setTitleColor(UIColor.white, for: .normal)
//If you want to set Background color for button
buttonSwift.backgroundColor = UIColor.black
//If you want to set tag for button
buttonSwift.tag = 0
//If you want to add or set image for button
let image = UIImage(named: "YourImageName") as UIImage?
buttonSwift.setImage(image, for: .normal)
//If you want to add or set Background image for button
buttonSwift.setBackgroundImage(image, for: .normal)
//Add action for button
buttonSwift.addTarget(self, action: #selector(actionPressMe), for:.touchUpInside)
//Add button as SubView to Super View
self.view.addSubview(buttonSwift)
UIButton Action Method
func actionPressMe(sender: UIButton!)
{
NSLog("Clicked button tag is %@", sender.tag)
OR
print("Clicked button tag is (sender.tag)")
//Then do whatever you want to do here
........
}
OBJECTIVE C
UIButton *buttonObjectiveC = [UIButton buttonWithType:UIButtonTypeCustom];
OR
UIButton *buttonObjectiveC = [UIButton buttonWithType:UIButtonTypeSystem];
buttonObjectiveC.frame = CGRectMake(200, 100, 200, 100);
//Set title for button
[buttonObjectiveC setTitle:@"ClickMe" forState:UIControlStateNormal];
//If you want to set color for button title
[buttonObjectiveC setTitleColor:[UIColor whiteColor] forState: UIControlStateNormal];
//If you want to set Background color for button
[buttonObjectiveC setBackgroundColor:[UIColor blackColor]];
//If you want to set tag for button
buttonSwift.tag = 0;
//If you want to add or set image for button
UIImage *image = [UIImage imageNamed:@"YourImageName"];
[buttonObjectiveC setImage:image forState:UIControlStateNormal];
//If you want to add or set Background image for button
[buttonObjectiveC setBackgroundImage:image forState:UIControlStateNormal];
//Add action for button
[buttonObjectiveC addTarget:self action:@selector(actionPressMe:)forControlEvents:UIControlEventTouchUpInside];
//Add button as SubView to Super View
[self.view addSubview:buttonObjectiveC];
UIButton Action Method
- (void)actionPressMe:(UIButton *)sender
{
NSLog(@"Clicked button tag is %@",sender.tag);
//Then do whatever you want to do here
..........
}
Output Screenshot is
add a comment |
-(void)addStuffToView
{
UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 20, 20)]; //(x, y, width, height of button on screen
[aButton setTitle:@"Button" forState:UIControlStateNormal];//puts the text on the button
aButton.titleLabel.font = somefont;//sets the font if one is already stated
aButton.titleLabel.font = [UIFont fontWithName:@"Arial-MT" size:12];//sets the font type and size
[aButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];//see back method below
[aButton setBackgroundImage:[UIImage imageNamed:@"someImage.png"] forState:UIControlStateNormal];//sets the image of the button
[self.view addSubview:back];
}
-(void)back
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle.....]
}
-(void)viewDidLoad
{
[super viewDidLoad];
[self addStuffToView];//adds all items built in this method to the view
}
add a comment |
For Swift 2.2 (with the with the new "selector" declaration).
let btn = UIButton(type: UIButtonType.System) as UIButton
btn.frame = CGRectMake(0, 0, 100, 20) // set any frame you want
btn.setTitle("MyAction", forState: UIControlState.Normal)
btn.addTarget(self, action: #selector(MyClass.myAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btn)
func myAction(sender:UIButton!){
// Some action
}
add a comment |
You can implement it in your ViewDidLoad
Method:
continuebtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 100, view1.frame.size.width-20, 40)];
[continuebtn setBackgroundColor:[UIColor grayColor]];
[continuebtn setTitle:@"Continue" forState:UIControlStateNormal];
continuebtn.layer.cornerRadius = 10;
continuebtn.layer.borderWidth =1.0;
continuebtn.layer.borderColor = [UIColor blackColor].CGColor;
[continuebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[continuebtn addTarget:self action:@selector(continuetonext) forControlEvents:UIControlEventTouchUpInside];
[view1 addSubview:continuebtn];
Where continuetonext
is:
-(void)continuetonext
{
GeneratePasswordVC *u = [[GeneratePasswordVC alloc]init];
[self.navigationController pushViewController:u animated:YES];
}
add a comment |
As of Swift 3, several changes have been made to the syntax.
Here is how you would go about creating a basic button as of Swift 3:
let button = UIButton(type: UIButtonType.system) as UIButton
button.frame = CGRect(x: 100, y: 100, width: 100, height: 50)
button.backgroundColor = UIColor.green
button.setTitle("Example Button", for: UIControlState.normal)
self.view.addSubview(button)
Here are the changes that have been made since previous versions of Swift:
let button = UIButton(type: UIButtonType.System) as UIButton
// system no longer capitalised
button.frame = CGRectMake(100, 100, 100, 50)
// CGRectMake has been removed as of Swift 3
button.backgroundColor = UIColor.greenColor()
// greenColor replaced with green
button.setTitle("Example Button", forState: UIControlState.Normal)
// normal is no longer capitalised
self.view.addSubview(button)
add a comment |
Try it....
UIButton *finalPriceBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
finalPriceBtn.frame=CGRectMake(260, 25, 45, 15);
[finalPriceBtn addTarget:self action:@selector(goBtnClk:) forControlEvents:UIControlEventTouchUpInside];
finalPriceBtn.titleLabel.font=[UIFont systemFontOfSize:12];
[finalPriceBtn setTitle:[NSString stringWithFormat:@"$%.2f",tempVal] forState:UIControlStateNormal];
finalPriceBtn.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1];
finalPriceBtn.titleLabel.textAlignment=UITextAlignmentLeft;
[imageView addSubview:finalPriceBtn];
Hope i helped.
add a comment |
UIButton *custombutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[custombutton addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[custombutton setTitle:@"Click" forState:UIControlStateNormal];
custombutton.frame = CGRectMake(80.0, 110.0, 160.0, 40.0);
custombutton.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1];
[custombutton setImage:[UIImage imageNamed:@"hh.png"] forState:UIControlStateNormal];
[view addSubview:custombutton];
add a comment |
For Swift 3 (even shorter code)
let button = UIButton(type: UIButtonType.custom)
button.frame = CGRect(x: 0, y: 0, width: 200.0, height: 40.0)
button.addTarget(nil, action: #selector(tapButton(_:)), for: UIControlEvents.touchUpInside)
button.tintColor = UIColor.white
button.backgroundColor = UIColor.red
button.setBackgroundImage(UIImage(named: "ImageName"), for: UIControlState.normal)
button.setTitle("MyTitle", for: UIControlState.normal)
button.isEnabled = true
func tapButton(sender: UIButton) {
}
add a comment |
Swift3 version should be
let myButton:UIButton = {
let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
myButton.setTitle("Hai Touch Me", for: .normal)
myButton.setTitleColor(UIColor.blue, for: .normal)
myButton.frame = CGRect(x: 20, y: 20, width: 100, height: 40)
myButton.addTarget(self, action: #selector(ViewController.pressedAction(_:)), for: .touchUpInside)
self.view.addSubview(myButton)
return myButton
}()
add a comment |
UIButton *buttonName = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
[buttonName addTarget:self
action:@selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[buttonName setTitle:@"Show View" forState:UIControlStateNormal];
.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); [view
addSubview:buttonName];
add a comment |
In Swift 4.1 and Xcode 10
Basically we have two types of buttons.
1) System type button
2) Custom type button (In custom type button we can set background image for button)
And these two types of buttons has few control states https://developer.apple.com/documentation/uikit/uicontrol/state
Important states are
1) Normal state
2) Selected state
3) Highlighted state
4) Disabled state etc...
//For system type button
let button = UIButton(type: .system)
button.frame = CGRect(x: 100, y: 250, width: 100, height: 50)
// button.backgroundColor = .blue
button.setTitle("Button", for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 13.0)
button.titleLabel?.textAlignment = .center//Text alighment center
button.titleLabel?.numberOfLines = 0//To display multiple lines in UIButton
button.titleLabel?.lineBreakMode = .byWordWrapping//By word wrapping
button.tag = 1//To assign tag value
button.btnProperties()//Call UIButton properties from extension function
button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button)
//For custom type button (add image to your button)
let button2 = UIButton(type: .custom)
button2.frame = CGRect(x: 100, y: 400, width: 100, height: 50)
// button2.backgroundColor = .blue
button2.setImage(UIImage.init(named: "img.png"), for: .normal)
button2.tag = 2
button2.btnProperties()//Call UIButton properties from extension function
button2.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button2)
@objc func buttonClicked(sender:UIButton) {
print("Button (sender.tag) clicked")
}
//You can add UIButton properties like this also
extension UIButton {
func btnProperties() {
layer.cornerRadius = 10//Set button corner radious
clipsToBounds = true
backgroundColor = .blue//Set background colour
//titleLabel?.textAlignment = .center//add properties like this
}
}
add a comment |
UIButton *btnname = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnname setTitle:@"Click Me" forState:UIControlStateNormal];
btnname.frame = CGRectMake(10, 10, 100, 140);
[self.view addSubview:btnname];
add a comment |
UIButton * tmpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[tmpBtn addTarget:self action:@selector(clearCart:) forControlEvents:UIControlEventTouchUpInside];
tmpBtn.tag = k;
tmpBtn.frame = CGRectMake(45, 0, 15, 15);
[tmpBtn setBackgroundImage:[UIImage imageNamed:@"CloseButton.png"] forState:UIControlStateNormal];
[self.view addSubview:tmpBtn];
add a comment |
1 2
next
protected by Community♦ Sep 25 '15 at 20:15
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
34 Answers
34
active
oldest
votes
34 Answers
34
active
oldest
votes
active
oldest
votes
active
oldest
votes
1 2
next
Here's one:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[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);
[view addSubview:button];
Thanks! can you explain to me what is aMethod?
– domlao
Sep 4 '09 at 12:43
5
buttonWithType: returns an autoreleased instance of UIButton
– Allyn
Apr 14 '11 at 21:05
259
Handy Tip: If you don't want your button to activate as soon as you touch it, use UIControlEventTouchUpInside instead of UIControlEventTouchDown - this will wait for the user to lift their finger, giving them the option of cancelling by dragging away (recommended in Apple Human Interface Guidelines)
– ayreguitar
Jul 29 '11 at 15:33
9
I do recommend using UIControlEventTouchUpInside for most cases. I have used UIControlEventTouchDown in some games and music playing apps (ClefTunes and ClefNotes) so that the touch immediately generates music notes.
– mahboudz
Sep 30 '11 at 17:47
45
Note for the people like me that struggled with this--@selector(aMethod:) refers to a method with an argument. @selector(aMethod) refers to a method with no argument. If your method signature looks like -(void) aMethod; and you use @selector(aMethod:), your app will crash.
– Chad Schultz
Oct 16 '12 at 17:31
|
show 7 more comments
Here's one:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[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);
[view addSubview:button];
Thanks! can you explain to me what is aMethod?
– domlao
Sep 4 '09 at 12:43
5
buttonWithType: returns an autoreleased instance of UIButton
– Allyn
Apr 14 '11 at 21:05
259
Handy Tip: If you don't want your button to activate as soon as you touch it, use UIControlEventTouchUpInside instead of UIControlEventTouchDown - this will wait for the user to lift their finger, giving them the option of cancelling by dragging away (recommended in Apple Human Interface Guidelines)
– ayreguitar
Jul 29 '11 at 15:33
9
I do recommend using UIControlEventTouchUpInside for most cases. I have used UIControlEventTouchDown in some games and music playing apps (ClefTunes and ClefNotes) so that the touch immediately generates music notes.
– mahboudz
Sep 30 '11 at 17:47
45
Note for the people like me that struggled with this--@selector(aMethod:) refers to a method with an argument. @selector(aMethod) refers to a method with no argument. If your method signature looks like -(void) aMethod; and you use @selector(aMethod:), your app will crash.
– Chad Schultz
Oct 16 '12 at 17:31
|
show 7 more comments
Here's one:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[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);
[view addSubview:button];
Here's one:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[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);
[view addSubview:button];
edited Oct 12 '15 at 6:43
Fahim Parkar
20.5k34130239
20.5k34130239
answered Sep 4 '09 at 11:51
mahboudzmahboudz
34.2k1688121
34.2k1688121
Thanks! can you explain to me what is aMethod?
– domlao
Sep 4 '09 at 12:43
5
buttonWithType: returns an autoreleased instance of UIButton
– Allyn
Apr 14 '11 at 21:05
259
Handy Tip: If you don't want your button to activate as soon as you touch it, use UIControlEventTouchUpInside instead of UIControlEventTouchDown - this will wait for the user to lift their finger, giving them the option of cancelling by dragging away (recommended in Apple Human Interface Guidelines)
– ayreguitar
Jul 29 '11 at 15:33
9
I do recommend using UIControlEventTouchUpInside for most cases. I have used UIControlEventTouchDown in some games and music playing apps (ClefTunes and ClefNotes) so that the touch immediately generates music notes.
– mahboudz
Sep 30 '11 at 17:47
45
Note for the people like me that struggled with this--@selector(aMethod:) refers to a method with an argument. @selector(aMethod) refers to a method with no argument. If your method signature looks like -(void) aMethod; and you use @selector(aMethod:), your app will crash.
– Chad Schultz
Oct 16 '12 at 17:31
|
show 7 more comments
Thanks! can you explain to me what is aMethod?
– domlao
Sep 4 '09 at 12:43
5
buttonWithType: returns an autoreleased instance of UIButton
– Allyn
Apr 14 '11 at 21:05
259
Handy Tip: If you don't want your button to activate as soon as you touch it, use UIControlEventTouchUpInside instead of UIControlEventTouchDown - this will wait for the user to lift their finger, giving them the option of cancelling by dragging away (recommended in Apple Human Interface Guidelines)
– ayreguitar
Jul 29 '11 at 15:33
9
I do recommend using UIControlEventTouchUpInside for most cases. I have used UIControlEventTouchDown in some games and music playing apps (ClefTunes and ClefNotes) so that the touch immediately generates music notes.
– mahboudz
Sep 30 '11 at 17:47
45
Note for the people like me that struggled with this--@selector(aMethod:) refers to a method with an argument. @selector(aMethod) refers to a method with no argument. If your method signature looks like -(void) aMethod; and you use @selector(aMethod:), your app will crash.
– Chad Schultz
Oct 16 '12 at 17:31
Thanks! can you explain to me what is aMethod?
– domlao
Sep 4 '09 at 12:43
Thanks! can you explain to me what is aMethod?
– domlao
Sep 4 '09 at 12:43
5
5
buttonWithType: returns an autoreleased instance of UIButton
– Allyn
Apr 14 '11 at 21:05
buttonWithType: returns an autoreleased instance of UIButton
– Allyn
Apr 14 '11 at 21:05
259
259
Handy Tip: If you don't want your button to activate as soon as you touch it, use UIControlEventTouchUpInside instead of UIControlEventTouchDown - this will wait for the user to lift their finger, giving them the option of cancelling by dragging away (recommended in Apple Human Interface Guidelines)
– ayreguitar
Jul 29 '11 at 15:33
Handy Tip: If you don't want your button to activate as soon as you touch it, use UIControlEventTouchUpInside instead of UIControlEventTouchDown - this will wait for the user to lift their finger, giving them the option of cancelling by dragging away (recommended in Apple Human Interface Guidelines)
– ayreguitar
Jul 29 '11 at 15:33
9
9
I do recommend using UIControlEventTouchUpInside for most cases. I have used UIControlEventTouchDown in some games and music playing apps (ClefTunes and ClefNotes) so that the touch immediately generates music notes.
– mahboudz
Sep 30 '11 at 17:47
I do recommend using UIControlEventTouchUpInside for most cases. I have used UIControlEventTouchDown in some games and music playing apps (ClefTunes and ClefNotes) so that the touch immediately generates music notes.
– mahboudz
Sep 30 '11 at 17:47
45
45
Note for the people like me that struggled with this--@selector(aMethod:) refers to a method with an argument. @selector(aMethod) refers to a method with no argument. If your method signature looks like -(void) aMethod; and you use @selector(aMethod:), your app will crash.
– Chad Schultz
Oct 16 '12 at 17:31
Note for the people like me that struggled with this--@selector(aMethod:) refers to a method with an argument. @selector(aMethod) refers to a method with no argument. If your method signature looks like -(void) aMethod; and you use @selector(aMethod:), your app will crash.
– Chad Schultz
Oct 16 '12 at 17:31
|
show 7 more comments
- (void)viewDidLoad {
[super viewDidLoad];
[self addMyButton]; // Call add button method on view load
}
- (void)addMyButton{ // Method for creating button, with background image and other properties
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
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];
}
great thanks, can you arrange the code? thanks =)
– domlao
Mar 2 '10 at 23:31
@ sasayins: I have arranged the code, you can call addMyButton method on viewDidLoad.
– Xorsat
Mar 4 '10 at 17:15
1
thanks... but why use viewController:viewDidLoad? it seems like this stuff (setting up of the view/adding buttons) should be done in the view instead of the view controller? thanks
– Rakka Rage
Jun 19 '11 at 4:44
@rakkarage The addMyButton() method is generic method, you can use it from event.
– Xorsat
Jul 1 '11 at 11:56
1
A much better answer than the one accepted, because it shows context for the code.
– Morgan Wilde
May 9 '13 at 9:44
|
show 1 more comment
- (void)viewDidLoad {
[super viewDidLoad];
[self addMyButton]; // Call add button method on view load
}
- (void)addMyButton{ // Method for creating button, with background image and other properties
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
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];
}
great thanks, can you arrange the code? thanks =)
– domlao
Mar 2 '10 at 23:31
@ sasayins: I have arranged the code, you can call addMyButton method on viewDidLoad.
– Xorsat
Mar 4 '10 at 17:15
1
thanks... but why use viewController:viewDidLoad? it seems like this stuff (setting up of the view/adding buttons) should be done in the view instead of the view controller? thanks
– Rakka Rage
Jun 19 '11 at 4:44
@rakkarage The addMyButton() method is generic method, you can use it from event.
– Xorsat
Jul 1 '11 at 11:56
1
A much better answer than the one accepted, because it shows context for the code.
– Morgan Wilde
May 9 '13 at 9:44
|
show 1 more comment
- (void)viewDidLoad {
[super viewDidLoad];
[self addMyButton]; // Call add button method on view load
}
- (void)addMyButton{ // Method for creating button, with background image and other properties
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
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];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self addMyButton]; // Call add button method on view load
}
- (void)addMyButton{ // Method for creating button, with background image and other properties
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
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];
}
edited Mar 4 '10 at 17:13
answered Mar 2 '10 at 18:27
XorsatXorsat
2,1931821
2,1931821
great thanks, can you arrange the code? thanks =)
– domlao
Mar 2 '10 at 23:31
@ sasayins: I have arranged the code, you can call addMyButton method on viewDidLoad.
– Xorsat
Mar 4 '10 at 17:15
1
thanks... but why use viewController:viewDidLoad? it seems like this stuff (setting up of the view/adding buttons) should be done in the view instead of the view controller? thanks
– Rakka Rage
Jun 19 '11 at 4:44
@rakkarage The addMyButton() method is generic method, you can use it from event.
– Xorsat
Jul 1 '11 at 11:56
1
A much better answer than the one accepted, because it shows context for the code.
– Morgan Wilde
May 9 '13 at 9:44
|
show 1 more comment
great thanks, can you arrange the code? thanks =)
– domlao
Mar 2 '10 at 23:31
@ sasayins: I have arranged the code, you can call addMyButton method on viewDidLoad.
– Xorsat
Mar 4 '10 at 17:15
1
thanks... but why use viewController:viewDidLoad? it seems like this stuff (setting up of the view/adding buttons) should be done in the view instead of the view controller? thanks
– Rakka Rage
Jun 19 '11 at 4:44
@rakkarage The addMyButton() method is generic method, you can use it from event.
– Xorsat
Jul 1 '11 at 11:56
1
A much better answer than the one accepted, because it shows context for the code.
– Morgan Wilde
May 9 '13 at 9:44
great thanks, can you arrange the code? thanks =)
– domlao
Mar 2 '10 at 23:31
great thanks, can you arrange the code? thanks =)
– domlao
Mar 2 '10 at 23:31
@ sasayins: I have arranged the code, you can call addMyButton method on viewDidLoad.
– Xorsat
Mar 4 '10 at 17:15
@ sasayins: I have arranged the code, you can call addMyButton method on viewDidLoad.
– Xorsat
Mar 4 '10 at 17:15
1
1
thanks... but why use viewController:viewDidLoad? it seems like this stuff (setting up of the view/adding buttons) should be done in the view instead of the view controller? thanks
– Rakka Rage
Jun 19 '11 at 4:44
thanks... but why use viewController:viewDidLoad? it seems like this stuff (setting up of the view/adding buttons) should be done in the view instead of the view controller? thanks
– Rakka Rage
Jun 19 '11 at 4:44
@rakkarage The addMyButton() method is generic method, you can use it from event.
– Xorsat
Jul 1 '11 at 11:56
@rakkarage The addMyButton() method is generic method, you can use it from event.
– Xorsat
Jul 1 '11 at 11:56
1
1
A much better answer than the one accepted, because it shows context for the code.
– Morgan Wilde
May 9 '13 at 9:44
A much better answer than the one accepted, because it shows context for the code.
– Morgan Wilde
May 9 '13 at 9:44
|
show 1 more comment
Objective-C
UIButton *but= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[but addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[but setFrame:CGRectMake(52, 252, 215, 40)];
[but setTitle:@"Login" forState:UIControlStateNormal];
[but setExclusiveTouch:YES];
// if you like to add backgroundImage else no need
[but setbackgroundImage:[UIImage imageNamed:@"XXX.png"] forState:UIControlStateNormal];
[self.view addSubview:but];
-(void) buttonClicked:(UIButton*)sender
{
NSLog(@"you clicked on button %@", sender.tag);
}
Swift
let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
myButton.setTitle("Hai Touch Me", forState: .Normal)
myButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
myButton.frame = CGRectMake(15, 50, 300, 500)
myButton.addTarget(self, action: "pressedAction:", forControlEvents: .TouchUpInside)
self.view.addSubview( myButton)
func pressedAction(sender: UIButton!) {
// do your stuff here
NSLog("you clicked on button %@", sender.tag)
}
Swift3 and above
let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
myButton.setTitle("Hi, Click me", for: .normal)
myButton.setTitleColor(UIColor.blue, for: .normal)
myButton.frame = CGRect(x: 15, y: 50, width: 300, height: 500)
myButton.addTarget(self, action: #selector(pressedAction(_:)), for: .touchUpInside)
self.view.addSubview( myButton)
func pressedAction(_ sender: UIButton) {
// do your stuff here
print("you clicked on button (sender.tag)")
}
swift 3: gives errorexped declaration
at myButton.setTitle("Hi, Click")
– Xcodian Solangi
Dec 20 '17 at 9:14
@XcodianSolangi -- is notmyButton.setTitle("Hi, Click")
the syntax ismyButton.setTitle("Hi, Click me", for: .normal)
i tried my code i am not faced any issues
– Anbu.Karthik
Dec 20 '17 at 9:24
i write as it as you defined, now i have defined constant in VC and other parts inside the function
– Xcodian Solangi
Dec 20 '17 at 10:01
add a comment |
Objective-C
UIButton *but= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[but addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[but setFrame:CGRectMake(52, 252, 215, 40)];
[but setTitle:@"Login" forState:UIControlStateNormal];
[but setExclusiveTouch:YES];
// if you like to add backgroundImage else no need
[but setbackgroundImage:[UIImage imageNamed:@"XXX.png"] forState:UIControlStateNormal];
[self.view addSubview:but];
-(void) buttonClicked:(UIButton*)sender
{
NSLog(@"you clicked on button %@", sender.tag);
}
Swift
let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
myButton.setTitle("Hai Touch Me", forState: .Normal)
myButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
myButton.frame = CGRectMake(15, 50, 300, 500)
myButton.addTarget(self, action: "pressedAction:", forControlEvents: .TouchUpInside)
self.view.addSubview( myButton)
func pressedAction(sender: UIButton!) {
// do your stuff here
NSLog("you clicked on button %@", sender.tag)
}
Swift3 and above
let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
myButton.setTitle("Hi, Click me", for: .normal)
myButton.setTitleColor(UIColor.blue, for: .normal)
myButton.frame = CGRect(x: 15, y: 50, width: 300, height: 500)
myButton.addTarget(self, action: #selector(pressedAction(_:)), for: .touchUpInside)
self.view.addSubview( myButton)
func pressedAction(_ sender: UIButton) {
// do your stuff here
print("you clicked on button (sender.tag)")
}
swift 3: gives errorexped declaration
at myButton.setTitle("Hi, Click")
– Xcodian Solangi
Dec 20 '17 at 9:14
@XcodianSolangi -- is notmyButton.setTitle("Hi, Click")
the syntax ismyButton.setTitle("Hi, Click me", for: .normal)
i tried my code i am not faced any issues
– Anbu.Karthik
Dec 20 '17 at 9:24
i write as it as you defined, now i have defined constant in VC and other parts inside the function
– Xcodian Solangi
Dec 20 '17 at 10:01
add a comment |
Objective-C
UIButton *but= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[but addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[but setFrame:CGRectMake(52, 252, 215, 40)];
[but setTitle:@"Login" forState:UIControlStateNormal];
[but setExclusiveTouch:YES];
// if you like to add backgroundImage else no need
[but setbackgroundImage:[UIImage imageNamed:@"XXX.png"] forState:UIControlStateNormal];
[self.view addSubview:but];
-(void) buttonClicked:(UIButton*)sender
{
NSLog(@"you clicked on button %@", sender.tag);
}
Swift
let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
myButton.setTitle("Hai Touch Me", forState: .Normal)
myButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
myButton.frame = CGRectMake(15, 50, 300, 500)
myButton.addTarget(self, action: "pressedAction:", forControlEvents: .TouchUpInside)
self.view.addSubview( myButton)
func pressedAction(sender: UIButton!) {
// do your stuff here
NSLog("you clicked on button %@", sender.tag)
}
Swift3 and above
let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
myButton.setTitle("Hi, Click me", for: .normal)
myButton.setTitleColor(UIColor.blue, for: .normal)
myButton.frame = CGRect(x: 15, y: 50, width: 300, height: 500)
myButton.addTarget(self, action: #selector(pressedAction(_:)), for: .touchUpInside)
self.view.addSubview( myButton)
func pressedAction(_ sender: UIButton) {
// do your stuff here
print("you clicked on button (sender.tag)")
}
Objective-C
UIButton *but= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[but addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[but setFrame:CGRectMake(52, 252, 215, 40)];
[but setTitle:@"Login" forState:UIControlStateNormal];
[but setExclusiveTouch:YES];
// if you like to add backgroundImage else no need
[but setbackgroundImage:[UIImage imageNamed:@"XXX.png"] forState:UIControlStateNormal];
[self.view addSubview:but];
-(void) buttonClicked:(UIButton*)sender
{
NSLog(@"you clicked on button %@", sender.tag);
}
Swift
let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
myButton.setTitle("Hai Touch Me", forState: .Normal)
myButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
myButton.frame = CGRectMake(15, 50, 300, 500)
myButton.addTarget(self, action: "pressedAction:", forControlEvents: .TouchUpInside)
self.view.addSubview( myButton)
func pressedAction(sender: UIButton!) {
// do your stuff here
NSLog("you clicked on button %@", sender.tag)
}
Swift3 and above
let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
myButton.setTitle("Hi, Click me", for: .normal)
myButton.setTitleColor(UIColor.blue, for: .normal)
myButton.frame = CGRect(x: 15, y: 50, width: 300, height: 500)
myButton.addTarget(self, action: #selector(pressedAction(_:)), for: .touchUpInside)
self.view.addSubview( myButton)
func pressedAction(_ sender: UIButton) {
// do your stuff here
print("you clicked on button (sender.tag)")
}
edited Aug 21 '17 at 7:50
answered Oct 18 '13 at 13:13
Anbu.KarthikAnbu.Karthik
61.6k16121111
61.6k16121111
swift 3: gives errorexped declaration
at myButton.setTitle("Hi, Click")
– Xcodian Solangi
Dec 20 '17 at 9:14
@XcodianSolangi -- is notmyButton.setTitle("Hi, Click")
the syntax ismyButton.setTitle("Hi, Click me", for: .normal)
i tried my code i am not faced any issues
– Anbu.Karthik
Dec 20 '17 at 9:24
i write as it as you defined, now i have defined constant in VC and other parts inside the function
– Xcodian Solangi
Dec 20 '17 at 10:01
add a comment |
swift 3: gives errorexped declaration
at myButton.setTitle("Hi, Click")
– Xcodian Solangi
Dec 20 '17 at 9:14
@XcodianSolangi -- is notmyButton.setTitle("Hi, Click")
the syntax ismyButton.setTitle("Hi, Click me", for: .normal)
i tried my code i am not faced any issues
– Anbu.Karthik
Dec 20 '17 at 9:24
i write as it as you defined, now i have defined constant in VC and other parts inside the function
– Xcodian Solangi
Dec 20 '17 at 10:01
swift 3: gives error
exped declaration
at myButton.setTitle("Hi, Click")– Xcodian Solangi
Dec 20 '17 at 9:14
swift 3: gives error
exped declaration
at myButton.setTitle("Hi, Click")– Xcodian Solangi
Dec 20 '17 at 9:14
@XcodianSolangi -- is not
myButton.setTitle("Hi, Click")
the syntax is myButton.setTitle("Hi, Click me", for: .normal)
i tried my code i am not faced any issues– Anbu.Karthik
Dec 20 '17 at 9:24
@XcodianSolangi -- is not
myButton.setTitle("Hi, Click")
the syntax is myButton.setTitle("Hi, Click me", for: .normal)
i tried my code i am not faced any issues– Anbu.Karthik
Dec 20 '17 at 9:24
i write as it as you defined, now i have defined constant in VC and other parts inside the function
– Xcodian Solangi
Dec 20 '17 at 10:01
i write as it as you defined, now i have defined constant in VC and other parts inside the function
– Xcodian Solangi
Dec 20 '17 at 10:01
add a comment |
To add a button programatically to your controller's view, use the following:
-(void)viewDidLoad
{
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 50);
[btn setTitle:@"Hello, world!" forState:UIControlStateNormal];
[self.view addSubview:btn];
}
To add three of these, rinse and repeat.
It seems it is not a good idea (??) to do initialization of coordinates of view in viewDidLoad: stackoverflow.com/questions/17882199/… ???
– user2054339
Jul 30 '13 at 13:46
3
Correct, there are better approaches now than there were in 2009.
– Jason
Jul 31 '13 at 19:47
add a comment |
To add a button programatically to your controller's view, use the following:
-(void)viewDidLoad
{
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 50);
[btn setTitle:@"Hello, world!" forState:UIControlStateNormal];
[self.view addSubview:btn];
}
To add three of these, rinse and repeat.
It seems it is not a good idea (??) to do initialization of coordinates of view in viewDidLoad: stackoverflow.com/questions/17882199/… ???
– user2054339
Jul 30 '13 at 13:46
3
Correct, there are better approaches now than there were in 2009.
– Jason
Jul 31 '13 at 19:47
add a comment |
To add a button programatically to your controller's view, use the following:
-(void)viewDidLoad
{
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 50);
[btn setTitle:@"Hello, world!" forState:UIControlStateNormal];
[self.view addSubview:btn];
}
To add three of these, rinse and repeat.
To add a button programatically to your controller's view, use the following:
-(void)viewDidLoad
{
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 100, 50);
[btn setTitle:@"Hello, world!" forState:UIControlStateNormal];
[self.view addSubview:btn];
}
To add three of these, rinse and repeat.
edited Sep 12 '09 at 16:23
answered Sep 4 '09 at 11:48
JasonJason
21.4k105762
21.4k105762
It seems it is not a good idea (??) to do initialization of coordinates of view in viewDidLoad: stackoverflow.com/questions/17882199/… ???
– user2054339
Jul 30 '13 at 13:46
3
Correct, there are better approaches now than there were in 2009.
– Jason
Jul 31 '13 at 19:47
add a comment |
It seems it is not a good idea (??) to do initialization of coordinates of view in viewDidLoad: stackoverflow.com/questions/17882199/… ???
– user2054339
Jul 30 '13 at 13:46
3
Correct, there are better approaches now than there were in 2009.
– Jason
Jul 31 '13 at 19:47
It seems it is not a good idea (??) to do initialization of coordinates of view in viewDidLoad: stackoverflow.com/questions/17882199/… ???
– user2054339
Jul 30 '13 at 13:46
It seems it is not a good idea (??) to do initialization of coordinates of view in viewDidLoad: stackoverflow.com/questions/17882199/… ???
– user2054339
Jul 30 '13 at 13:46
3
3
Correct, there are better approaches now than there were in 2009.
– Jason
Jul 31 '13 at 19:47
Correct, there are better approaches now than there were in 2009.
– Jason
Jul 31 '13 at 19:47
add a comment |
Here you can create dynamically a UIButton:
//For button image
UIImage *closebtnimg = [UIImage imageNamed:@"close_btn.png"];
//Custom type button
btnclose = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
//Set frame of button means position
btnclose.frame = CGRectMake(103, 257, 94, 32);
//Button with 0 border so it's shape like image shape
[btnclose.layer setBorderWidth:0];
//Set title of button
[btnclose setTitle:@"CLOSE" forState:UIControlStateNormal];
[btnclose addTarget:self action:@selector(methodname:) forControlEvents:UIControlEventTouchUpInside];
//Font size of title
btnclose.titleLabel.font = [UIFont boldSystemFontOfSize:14];
//Set image of button
[btnclose setBackgroundImage:closebtnimg forState:UIControlStateNormal];
add a comment |
Here you can create dynamically a UIButton:
//For button image
UIImage *closebtnimg = [UIImage imageNamed:@"close_btn.png"];
//Custom type button
btnclose = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
//Set frame of button means position
btnclose.frame = CGRectMake(103, 257, 94, 32);
//Button with 0 border so it's shape like image shape
[btnclose.layer setBorderWidth:0];
//Set title of button
[btnclose setTitle:@"CLOSE" forState:UIControlStateNormal];
[btnclose addTarget:self action:@selector(methodname:) forControlEvents:UIControlEventTouchUpInside];
//Font size of title
btnclose.titleLabel.font = [UIFont boldSystemFontOfSize:14];
//Set image of button
[btnclose setBackgroundImage:closebtnimg forState:UIControlStateNormal];
add a comment |
Here you can create dynamically a UIButton:
//For button image
UIImage *closebtnimg = [UIImage imageNamed:@"close_btn.png"];
//Custom type button
btnclose = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
//Set frame of button means position
btnclose.frame = CGRectMake(103, 257, 94, 32);
//Button with 0 border so it's shape like image shape
[btnclose.layer setBorderWidth:0];
//Set title of button
[btnclose setTitle:@"CLOSE" forState:UIControlStateNormal];
[btnclose addTarget:self action:@selector(methodname:) forControlEvents:UIControlEventTouchUpInside];
//Font size of title
btnclose.titleLabel.font = [UIFont boldSystemFontOfSize:14];
//Set image of button
[btnclose setBackgroundImage:closebtnimg forState:UIControlStateNormal];
Here you can create dynamically a UIButton:
//For button image
UIImage *closebtnimg = [UIImage imageNamed:@"close_btn.png"];
//Custom type button
btnclose = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
//Set frame of button means position
btnclose.frame = CGRectMake(103, 257, 94, 32);
//Button with 0 border so it's shape like image shape
[btnclose.layer setBorderWidth:0];
//Set title of button
[btnclose setTitle:@"CLOSE" forState:UIControlStateNormal];
[btnclose addTarget:self action:@selector(methodname:) forControlEvents:UIControlEventTouchUpInside];
//Font size of title
btnclose.titleLabel.font = [UIFont boldSystemFontOfSize:14];
//Set image of button
[btnclose setBackgroundImage:closebtnimg forState:UIControlStateNormal];
edited Apr 3 '12 at 20:55
Mazyod
17.5k675136
17.5k675136
answered Mar 27 '12 at 11:42
BirjuBirju
877828
877828
add a comment |
add a comment |
Come on, it's 2014! Why isn't code block evaluation assignment being used yet, as trends show it's the future!
UIButton* button = ({
//initialize button with frame
UIButton* button = [[UIButton alloc] initWithFrame:({
CGRect frame = CGRectMake(10.0, 10.0, 200.0, 75.0);
frame;
})];
//set button background color
[button setBackgroundColor:({
UIColor* color = [UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0];
color;
})];
//set button title for state
[button setTitle:({
NSString* string = [NSString stringWithFormat:@"title words"];
string;
}) forState:({
UIControlState state = UIControlStateNormal;
state;
})];
//set selector
[button addTarget:self action:({
SEL select = @selector(method:);
select;
}) forControlEvents:({
UIControlEvents event = UIControlEventTouchUpInside;
event;
})];
//return button
button;
});
[self.view addSubview:button];
whoa!
Or the exact results can be accomplished as such:
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(10.0, 10.0, 200.0, 75.0)];
[button setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0]];
[button setTitle:@"title words" forState:UIControlStateNormal];
[button addTarget:self action:@selector(method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
6
A little over the top, but it's an interesting new feature.
– Paul Solt
Jan 23 '14 at 5:52
That's the point, to show off code block evaluation and ANSWER the question. It's annoying this is getting -1's...
– John Riselvato
Jan 23 '14 at 14:20
Levelled it out...
– StuartM
Jan 23 '14 at 17:26
1
you will have more upvotes if you give explanation for why code block evaluation assignment is important...
– Fahim Parkar
Mar 1 '16 at 5:53
3
I comment with context to Come on, it's 2014!
– Fahim Parkar
Mar 2 '16 at 5:01
|
show 1 more comment
Come on, it's 2014! Why isn't code block evaluation assignment being used yet, as trends show it's the future!
UIButton* button = ({
//initialize button with frame
UIButton* button = [[UIButton alloc] initWithFrame:({
CGRect frame = CGRectMake(10.0, 10.0, 200.0, 75.0);
frame;
})];
//set button background color
[button setBackgroundColor:({
UIColor* color = [UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0];
color;
})];
//set button title for state
[button setTitle:({
NSString* string = [NSString stringWithFormat:@"title words"];
string;
}) forState:({
UIControlState state = UIControlStateNormal;
state;
})];
//set selector
[button addTarget:self action:({
SEL select = @selector(method:);
select;
}) forControlEvents:({
UIControlEvents event = UIControlEventTouchUpInside;
event;
})];
//return button
button;
});
[self.view addSubview:button];
whoa!
Or the exact results can be accomplished as such:
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(10.0, 10.0, 200.0, 75.0)];
[button setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0]];
[button setTitle:@"title words" forState:UIControlStateNormal];
[button addTarget:self action:@selector(method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
6
A little over the top, but it's an interesting new feature.
– Paul Solt
Jan 23 '14 at 5:52
That's the point, to show off code block evaluation and ANSWER the question. It's annoying this is getting -1's...
– John Riselvato
Jan 23 '14 at 14:20
Levelled it out...
– StuartM
Jan 23 '14 at 17:26
1
you will have more upvotes if you give explanation for why code block evaluation assignment is important...
– Fahim Parkar
Mar 1 '16 at 5:53
3
I comment with context to Come on, it's 2014!
– Fahim Parkar
Mar 2 '16 at 5:01
|
show 1 more comment
Come on, it's 2014! Why isn't code block evaluation assignment being used yet, as trends show it's the future!
UIButton* button = ({
//initialize button with frame
UIButton* button = [[UIButton alloc] initWithFrame:({
CGRect frame = CGRectMake(10.0, 10.0, 200.0, 75.0);
frame;
})];
//set button background color
[button setBackgroundColor:({
UIColor* color = [UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0];
color;
})];
//set button title for state
[button setTitle:({
NSString* string = [NSString stringWithFormat:@"title words"];
string;
}) forState:({
UIControlState state = UIControlStateNormal;
state;
})];
//set selector
[button addTarget:self action:({
SEL select = @selector(method:);
select;
}) forControlEvents:({
UIControlEvents event = UIControlEventTouchUpInside;
event;
})];
//return button
button;
});
[self.view addSubview:button];
whoa!
Or the exact results can be accomplished as such:
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(10.0, 10.0, 200.0, 75.0)];
[button setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0]];
[button setTitle:@"title words" forState:UIControlStateNormal];
[button addTarget:self action:@selector(method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Come on, it's 2014! Why isn't code block evaluation assignment being used yet, as trends show it's the future!
UIButton* button = ({
//initialize button with frame
UIButton* button = [[UIButton alloc] initWithFrame:({
CGRect frame = CGRectMake(10.0, 10.0, 200.0, 75.0);
frame;
})];
//set button background color
[button setBackgroundColor:({
UIColor* color = [UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0];
color;
})];
//set button title for state
[button setTitle:({
NSString* string = [NSString stringWithFormat:@"title words"];
string;
}) forState:({
UIControlState state = UIControlStateNormal;
state;
})];
//set selector
[button addTarget:self action:({
SEL select = @selector(method:);
select;
}) forControlEvents:({
UIControlEvents event = UIControlEventTouchUpInside;
event;
})];
//return button
button;
});
[self.view addSubview:button];
whoa!
Or the exact results can be accomplished as such:
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(10.0, 10.0, 200.0, 75.0)];
[button setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:0.0 alpha:1.0]];
[button setTitle:@"title words" forState:UIControlStateNormal];
[button addTarget:self action:@selector(method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
edited Aug 13 '17 at 8:30
Adaline Simonian
3,1671430
3,1671430
answered Jan 8 '14 at 16:29
John RiselvatoJohn Riselvato
11.1k55284
11.1k55284
6
A little over the top, but it's an interesting new feature.
– Paul Solt
Jan 23 '14 at 5:52
That's the point, to show off code block evaluation and ANSWER the question. It's annoying this is getting -1's...
– John Riselvato
Jan 23 '14 at 14:20
Levelled it out...
– StuartM
Jan 23 '14 at 17:26
1
you will have more upvotes if you give explanation for why code block evaluation assignment is important...
– Fahim Parkar
Mar 1 '16 at 5:53
3
I comment with context to Come on, it's 2014!
– Fahim Parkar
Mar 2 '16 at 5:01
|
show 1 more comment
6
A little over the top, but it's an interesting new feature.
– Paul Solt
Jan 23 '14 at 5:52
That's the point, to show off code block evaluation and ANSWER the question. It's annoying this is getting -1's...
– John Riselvato
Jan 23 '14 at 14:20
Levelled it out...
– StuartM
Jan 23 '14 at 17:26
1
you will have more upvotes if you give explanation for why code block evaluation assignment is important...
– Fahim Parkar
Mar 1 '16 at 5:53
3
I comment with context to Come on, it's 2014!
– Fahim Parkar
Mar 2 '16 at 5:01
6
6
A little over the top, but it's an interesting new feature.
– Paul Solt
Jan 23 '14 at 5:52
A little over the top, but it's an interesting new feature.
– Paul Solt
Jan 23 '14 at 5:52
That's the point, to show off code block evaluation and ANSWER the question. It's annoying this is getting -1's...
– John Riselvato
Jan 23 '14 at 14:20
That's the point, to show off code block evaluation and ANSWER the question. It's annoying this is getting -1's...
– John Riselvato
Jan 23 '14 at 14:20
Levelled it out...
– StuartM
Jan 23 '14 at 17:26
Levelled it out...
– StuartM
Jan 23 '14 at 17:26
1
1
you will have more upvotes if you give explanation for why code block evaluation assignment is important...
– Fahim Parkar
Mar 1 '16 at 5:53
you will have more upvotes if you give explanation for why code block evaluation assignment is important...
– Fahim Parkar
Mar 1 '16 at 5:53
3
3
I comment with context to Come on, it's 2014!
– Fahim Parkar
Mar 2 '16 at 5:01
I comment with context to Come on, it's 2014!
– Fahim Parkar
Mar 2 '16 at 5:01
|
show 1 more comment
'action:@selector(aMethod:)'
write method like this :
- (void)aMethod:(UIButton*)button
{
NSLog(@"Button clicked.");
}
It works for me. Thanks. KS.
I was looking for this , thanks a lot
– smoothumut
Jan 20 '15 at 14:11
add a comment |
'action:@selector(aMethod:)'
write method like this :
- (void)aMethod:(UIButton*)button
{
NSLog(@"Button clicked.");
}
It works for me. Thanks. KS.
I was looking for this , thanks a lot
– smoothumut
Jan 20 '15 at 14:11
add a comment |
'action:@selector(aMethod:)'
write method like this :
- (void)aMethod:(UIButton*)button
{
NSLog(@"Button clicked.");
}
It works for me. Thanks. KS.
'action:@selector(aMethod:)'
write method like this :
- (void)aMethod:(UIButton*)button
{
NSLog(@"Button clicked.");
}
It works for me. Thanks. KS.
edited Aug 29 '13 at 8:59
Shadwell
28.9k147988
28.9k147988
answered Jul 27 '13 at 7:20
user2625094user2625094
361310
361310
I was looking for this , thanks a lot
– smoothumut
Jan 20 '15 at 14:11
add a comment |
I was looking for this , thanks a lot
– smoothumut
Jan 20 '15 at 14:11
I was looking for this , thanks a lot
– smoothumut
Jan 20 '15 at 14:11
I was looking for this , thanks a lot
– smoothumut
Jan 20 '15 at 14:11
add a comment |
Objective-C
// Create the Button with RoundedRect type
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// instend of "Click Me" you can write your own message/Label
[mybutton setTitle:@"Click Me" forState:UIControlStateNormal];
// create the Rectangle Frame with specified size
mybutton.frame = CGRectMake(10, 10, 300, 140); // x,y,width,height [self.view addSubview:mybutton];// add button to your view.
Swift
let button = UIButton(type: UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.greenColor()
button.setTitle("Test Button", forState: UIControlState.Normal)
self.view.addSubview(button)
add a comment |
Objective-C
// Create the Button with RoundedRect type
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// instend of "Click Me" you can write your own message/Label
[mybutton setTitle:@"Click Me" forState:UIControlStateNormal];
// create the Rectangle Frame with specified size
mybutton.frame = CGRectMake(10, 10, 300, 140); // x,y,width,height [self.view addSubview:mybutton];// add button to your view.
Swift
let button = UIButton(type: UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.greenColor()
button.setTitle("Test Button", forState: UIControlState.Normal)
self.view.addSubview(button)
add a comment |
Objective-C
// Create the Button with RoundedRect type
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// instend of "Click Me" you can write your own message/Label
[mybutton setTitle:@"Click Me" forState:UIControlStateNormal];
// create the Rectangle Frame with specified size
mybutton.frame = CGRectMake(10, 10, 300, 140); // x,y,width,height [self.view addSubview:mybutton];// add button to your view.
Swift
let button = UIButton(type: UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.greenColor()
button.setTitle("Test Button", forState: UIControlState.Normal)
self.view.addSubview(button)
Objective-C
// Create the Button with RoundedRect type
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// instend of "Click Me" you can write your own message/Label
[mybutton setTitle:@"Click Me" forState:UIControlStateNormal];
// create the Rectangle Frame with specified size
mybutton.frame = CGRectMake(10, 10, 300, 140); // x,y,width,height [self.view addSubview:mybutton];// add button to your view.
Swift
let button = UIButton(type: UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.greenColor()
button.setTitle("Test Button", forState: UIControlState.Normal)
self.view.addSubview(button)
edited Apr 20 '16 at 12:07
answered Sep 11 '12 at 6:09
Himanshu padiaHimanshu padia
4,4053137
4,4053137
add a comment |
add a comment |
try this code to create a button and repeat it for 2 more times with different coordinates and the method(myButtonClick) is called when the button is pressed
UIButton *editButton = [UIButton buttonWithType: UIButtonTypeCustom];
editButton.frame = CGRectMake(0, 0, width, height);
[editButton setBackgroundImage: editButtonImage forState: UIControlStateNormal];
[myButton addTarget:self action:@selector(myButtonClick:) forControlEvents:UIControlEventTouchUpInside];
editButton.adjustsImageWhenHighlighted = YES;
editButton.titleLabel.text = @"Edit";
editButton.titleLabel.textColor = [UIColor whiteColor];
editButton.titleLabel.textAlignment = UITextAlignmentCenter;
editButton.titleLabel.font = [UIFont fontWithName: @"Helvetica" size: 14];
[self.view addSubview: editButton];
-(void) myButtonClick:(NSString *)myString{
NSLog(@"you clicked on button %@", myString);
}
What is myString set to?
– Doug Null
Aug 15 '13 at 14:58
that is the string to pass as a parameter just for additional info, you can also use that method like this -(void) myButtonClick{ NSLog(@"you clicked on button"); } but be sure that while calling the method remove the : just use like this [myButton addTarget:self action:@selector(myButtonClick) forControlEvents:UIControlEventTouchUpInside];
– ashokdy
Aug 15 '13 at 15:16
+1 for showing how to set multiple settings instead of just creating a button.
– Matt Wagner
Mar 21 '14 at 1:09
thnQ @MattWagner
– ashokdy
Mar 25 '14 at 7:06
add a comment |
try this code to create a button and repeat it for 2 more times with different coordinates and the method(myButtonClick) is called when the button is pressed
UIButton *editButton = [UIButton buttonWithType: UIButtonTypeCustom];
editButton.frame = CGRectMake(0, 0, width, height);
[editButton setBackgroundImage: editButtonImage forState: UIControlStateNormal];
[myButton addTarget:self action:@selector(myButtonClick:) forControlEvents:UIControlEventTouchUpInside];
editButton.adjustsImageWhenHighlighted = YES;
editButton.titleLabel.text = @"Edit";
editButton.titleLabel.textColor = [UIColor whiteColor];
editButton.titleLabel.textAlignment = UITextAlignmentCenter;
editButton.titleLabel.font = [UIFont fontWithName: @"Helvetica" size: 14];
[self.view addSubview: editButton];
-(void) myButtonClick:(NSString *)myString{
NSLog(@"you clicked on button %@", myString);
}
What is myString set to?
– Doug Null
Aug 15 '13 at 14:58
that is the string to pass as a parameter just for additional info, you can also use that method like this -(void) myButtonClick{ NSLog(@"you clicked on button"); } but be sure that while calling the method remove the : just use like this [myButton addTarget:self action:@selector(myButtonClick) forControlEvents:UIControlEventTouchUpInside];
– ashokdy
Aug 15 '13 at 15:16
+1 for showing how to set multiple settings instead of just creating a button.
– Matt Wagner
Mar 21 '14 at 1:09
thnQ @MattWagner
– ashokdy
Mar 25 '14 at 7:06
add a comment |
try this code to create a button and repeat it for 2 more times with different coordinates and the method(myButtonClick) is called when the button is pressed
UIButton *editButton = [UIButton buttonWithType: UIButtonTypeCustom];
editButton.frame = CGRectMake(0, 0, width, height);
[editButton setBackgroundImage: editButtonImage forState: UIControlStateNormal];
[myButton addTarget:self action:@selector(myButtonClick:) forControlEvents:UIControlEventTouchUpInside];
editButton.adjustsImageWhenHighlighted = YES;
editButton.titleLabel.text = @"Edit";
editButton.titleLabel.textColor = [UIColor whiteColor];
editButton.titleLabel.textAlignment = UITextAlignmentCenter;
editButton.titleLabel.font = [UIFont fontWithName: @"Helvetica" size: 14];
[self.view addSubview: editButton];
-(void) myButtonClick:(NSString *)myString{
NSLog(@"you clicked on button %@", myString);
}
try this code to create a button and repeat it for 2 more times with different coordinates and the method(myButtonClick) is called when the button is pressed
UIButton *editButton = [UIButton buttonWithType: UIButtonTypeCustom];
editButton.frame = CGRectMake(0, 0, width, height);
[editButton setBackgroundImage: editButtonImage forState: UIControlStateNormal];
[myButton addTarget:self action:@selector(myButtonClick:) forControlEvents:UIControlEventTouchUpInside];
editButton.adjustsImageWhenHighlighted = YES;
editButton.titleLabel.text = @"Edit";
editButton.titleLabel.textColor = [UIColor whiteColor];
editButton.titleLabel.textAlignment = UITextAlignmentCenter;
editButton.titleLabel.font = [UIFont fontWithName: @"Helvetica" size: 14];
[self.view addSubview: editButton];
-(void) myButtonClick:(NSString *)myString{
NSLog(@"you clicked on button %@", myString);
}
edited Aug 15 '13 at 15:18
answered Jun 27 '13 at 9:09
ashokdyashokdy
9641120
9641120
What is myString set to?
– Doug Null
Aug 15 '13 at 14:58
that is the string to pass as a parameter just for additional info, you can also use that method like this -(void) myButtonClick{ NSLog(@"you clicked on button"); } but be sure that while calling the method remove the : just use like this [myButton addTarget:self action:@selector(myButtonClick) forControlEvents:UIControlEventTouchUpInside];
– ashokdy
Aug 15 '13 at 15:16
+1 for showing how to set multiple settings instead of just creating a button.
– Matt Wagner
Mar 21 '14 at 1:09
thnQ @MattWagner
– ashokdy
Mar 25 '14 at 7:06
add a comment |
What is myString set to?
– Doug Null
Aug 15 '13 at 14:58
that is the string to pass as a parameter just for additional info, you can also use that method like this -(void) myButtonClick{ NSLog(@"you clicked on button"); } but be sure that while calling the method remove the : just use like this [myButton addTarget:self action:@selector(myButtonClick) forControlEvents:UIControlEventTouchUpInside];
– ashokdy
Aug 15 '13 at 15:16
+1 for showing how to set multiple settings instead of just creating a button.
– Matt Wagner
Mar 21 '14 at 1:09
thnQ @MattWagner
– ashokdy
Mar 25 '14 at 7:06
What is myString set to?
– Doug Null
Aug 15 '13 at 14:58
What is myString set to?
– Doug Null
Aug 15 '13 at 14:58
that is the string to pass as a parameter just for additional info, you can also use that method like this -(void) myButtonClick{ NSLog(@"you clicked on button"); } but be sure that while calling the method remove the : just use like this [myButton addTarget:self action:@selector(myButtonClick) forControlEvents:UIControlEventTouchUpInside];
– ashokdy
Aug 15 '13 at 15:16
that is the string to pass as a parameter just for additional info, you can also use that method like this -(void) myButtonClick{ NSLog(@"you clicked on button"); } but be sure that while calling the method remove the : just use like this [myButton addTarget:self action:@selector(myButtonClick) forControlEvents:UIControlEventTouchUpInside];
– ashokdy
Aug 15 '13 at 15:16
+1 for showing how to set multiple settings instead of just creating a button.
– Matt Wagner
Mar 21 '14 at 1:09
+1 for showing how to set multiple settings instead of just creating a button.
– Matt Wagner
Mar 21 '14 at 1:09
thnQ @MattWagner
– ashokdy
Mar 25 '14 at 7:06
thnQ @MattWagner
– ashokdy
Mar 25 '14 at 7:06
add a comment |
You can just put the creator instance within a loop and dynamically add names from an array if you so wish.
yup i tried mahboudz, and at the same time, i tried the looping. thanks
– domlao
Mar 2 '10 at 23:31
add a comment |
You can just put the creator instance within a loop and dynamically add names from an array if you so wish.
yup i tried mahboudz, and at the same time, i tried the looping. thanks
– domlao
Mar 2 '10 at 23:31
add a comment |
You can just put the creator instance within a loop and dynamically add names from an array if you so wish.
You can just put the creator instance within a loop and dynamically add names from an array if you so wish.
edited Mar 17 '14 at 17:57
John Riselvato
11.1k55284
11.1k55284
answered Sep 4 '09 at 11:58
Adam Libonatti-RocheAdam Libonatti-Roche
191214
191214
yup i tried mahboudz, and at the same time, i tried the looping. thanks
– domlao
Mar 2 '10 at 23:31
add a comment |
yup i tried mahboudz, and at the same time, i tried the looping. thanks
– domlao
Mar 2 '10 at 23:31
yup i tried mahboudz, and at the same time, i tried the looping. thanks
– domlao
Mar 2 '10 at 23:31
yup i tried mahboudz, and at the same time, i tried the looping. thanks
– domlao
Mar 2 '10 at 23:31
add a comment |
Check out this code:
CGRect frameimg = CGRectMake(15, 46, 55,70);
UIButton *SelectionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
SelectionButton.frame=frameimg;
SelectionButton.tag=i;
[SelectionButton setTitle:[SelectionArray objectAtIndex:0] forState:UIControlStateNormal];
[SelectionButton addTarget:self action:@selector(BtnSelected:)
forControlEvents:UIControlEventTouchUpInside];
[SelectionButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12.0]];
SelectionButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
SelectionButton.titleLabel.numberOfLines = 2;
SelectionButton.titleLabel.textAlignment = NSTextAlignmentCenter;
[SelectionButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
[SelectionButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[SelectionButton setShowsTouchWhenHighlighted:YES];
[self.view addSubview:SelectionButton];
I hope you find this code useful.
add a comment |
Check out this code:
CGRect frameimg = CGRectMake(15, 46, 55,70);
UIButton *SelectionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
SelectionButton.frame=frameimg;
SelectionButton.tag=i;
[SelectionButton setTitle:[SelectionArray objectAtIndex:0] forState:UIControlStateNormal];
[SelectionButton addTarget:self action:@selector(BtnSelected:)
forControlEvents:UIControlEventTouchUpInside];
[SelectionButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12.0]];
SelectionButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
SelectionButton.titleLabel.numberOfLines = 2;
SelectionButton.titleLabel.textAlignment = NSTextAlignmentCenter;
[SelectionButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
[SelectionButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[SelectionButton setShowsTouchWhenHighlighted:YES];
[self.view addSubview:SelectionButton];
I hope you find this code useful.
add a comment |
Check out this code:
CGRect frameimg = CGRectMake(15, 46, 55,70);
UIButton *SelectionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
SelectionButton.frame=frameimg;
SelectionButton.tag=i;
[SelectionButton setTitle:[SelectionArray objectAtIndex:0] forState:UIControlStateNormal];
[SelectionButton addTarget:self action:@selector(BtnSelected:)
forControlEvents:UIControlEventTouchUpInside];
[SelectionButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12.0]];
SelectionButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
SelectionButton.titleLabel.numberOfLines = 2;
SelectionButton.titleLabel.textAlignment = NSTextAlignmentCenter;
[SelectionButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
[SelectionButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[SelectionButton setShowsTouchWhenHighlighted:YES];
[self.view addSubview:SelectionButton];
I hope you find this code useful.
Check out this code:
CGRect frameimg = CGRectMake(15, 46, 55,70);
UIButton *SelectionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
SelectionButton.frame=frameimg;
SelectionButton.tag=i;
[SelectionButton setTitle:[SelectionArray objectAtIndex:0] forState:UIControlStateNormal];
[SelectionButton addTarget:self action:@selector(BtnSelected:)
forControlEvents:UIControlEventTouchUpInside];
[SelectionButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12.0]];
SelectionButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
SelectionButton.titleLabel.numberOfLines = 2;
SelectionButton.titleLabel.textAlignment = NSTextAlignmentCenter;
[SelectionButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
[SelectionButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[SelectionButton setShowsTouchWhenHighlighted:YES];
[self.view addSubview:SelectionButton];
I hope you find this code useful.
edited Jan 4 '18 at 5:49
cit
77641836
77641836
answered Jan 3 '14 at 10:39
Darshan KunjadiyaDarshan Kunjadiya
3,05112230
3,05112230
add a comment |
add a comment |
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(10.0, 100.0, 300.0, 20.0);
[self.view addSubview:button];
add a comment |
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(10.0, 100.0, 300.0, 20.0);
[self.view addSubview:button];
add a comment |
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(10.0, 100.0, 300.0, 20.0);
[self.view addSubview:button];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(10.0, 100.0, 300.0, 20.0);
[self.view addSubview:button];
answered Oct 22 '12 at 13:20
community wiki
NANNAV
add a comment |
add a comment |
-(UIButton *)addButton:(NSString *)title :(CGRect)frame : (SEL)selector :(UIImage *)image :(int)tag{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = frame;
[btn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:title forState:UIControlStateNormal];
[btn setImage:image forState:UIControlStateNormal];
btn.backgroundColor = [UIColor clearColor];
btn.tag = tag;
return btn;
}
and you can add it to the view:
[self.view addSubview:[self addButton:nil :self.view.frame :@selector(btnAction:) :[UIImage imageNamed:@"img.png"] :1]];
add a comment |
-(UIButton *)addButton:(NSString *)title :(CGRect)frame : (SEL)selector :(UIImage *)image :(int)tag{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = frame;
[btn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:title forState:UIControlStateNormal];
[btn setImage:image forState:UIControlStateNormal];
btn.backgroundColor = [UIColor clearColor];
btn.tag = tag;
return btn;
}
and you can add it to the view:
[self.view addSubview:[self addButton:nil :self.view.frame :@selector(btnAction:) :[UIImage imageNamed:@"img.png"] :1]];
add a comment |
-(UIButton *)addButton:(NSString *)title :(CGRect)frame : (SEL)selector :(UIImage *)image :(int)tag{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = frame;
[btn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:title forState:UIControlStateNormal];
[btn setImage:image forState:UIControlStateNormal];
btn.backgroundColor = [UIColor clearColor];
btn.tag = tag;
return btn;
}
and you can add it to the view:
[self.view addSubview:[self addButton:nil :self.view.frame :@selector(btnAction:) :[UIImage imageNamed:@"img.png"] :1]];
-(UIButton *)addButton:(NSString *)title :(CGRect)frame : (SEL)selector :(UIImage *)image :(int)tag{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = frame;
[btn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:title forState:UIControlStateNormal];
[btn setImage:image forState:UIControlStateNormal];
btn.backgroundColor = [UIColor clearColor];
btn.tag = tag;
return btn;
}
and you can add it to the view:
[self.view addSubview:[self addButton:nil :self.view.frame :@selector(btnAction:) :[UIImage imageNamed:@"img.png"] :1]];
answered May 8 '13 at 13:14
MutaweMutawe
5,40334081
5,40334081
add a comment |
add a comment |
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
add a comment |
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
add a comment |
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
answered May 4 '13 at 14:38
divyam shukladivyam shukla
772
772
add a comment |
add a comment |
This is an example as well to create three buttons. Just move their location.
UIImage *buttonOff = [UIImage imageNamed:@"crysBallNorm.png"];
UIImage *buttonOn = [UIImage imageNamed:@"crysBallHigh.png"];
UIButton *predictButton = [UIButton alloc];
predictButton = [UIButton buttonWithType:UIButtonTypeCustom];
predictButton.frame = CGRectMake(180.0, 510.0, 120.0, 30.0);
[predictButton setBackgroundImage:buttonOff forState:UIControlStateNormal];
[predictButton setBackgroundImage:buttonOn forState:UIControlStateHighlighted];
[predictButton setTitle:@"Predict" forState:UIControlStateNormal];
[predictButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
[predictButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:predictButton];
add a comment |
This is an example as well to create three buttons. Just move their location.
UIImage *buttonOff = [UIImage imageNamed:@"crysBallNorm.png"];
UIImage *buttonOn = [UIImage imageNamed:@"crysBallHigh.png"];
UIButton *predictButton = [UIButton alloc];
predictButton = [UIButton buttonWithType:UIButtonTypeCustom];
predictButton.frame = CGRectMake(180.0, 510.0, 120.0, 30.0);
[predictButton setBackgroundImage:buttonOff forState:UIControlStateNormal];
[predictButton setBackgroundImage:buttonOn forState:UIControlStateHighlighted];
[predictButton setTitle:@"Predict" forState:UIControlStateNormal];
[predictButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
[predictButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:predictButton];
add a comment |
This is an example as well to create three buttons. Just move their location.
UIImage *buttonOff = [UIImage imageNamed:@"crysBallNorm.png"];
UIImage *buttonOn = [UIImage imageNamed:@"crysBallHigh.png"];
UIButton *predictButton = [UIButton alloc];
predictButton = [UIButton buttonWithType:UIButtonTypeCustom];
predictButton.frame = CGRectMake(180.0, 510.0, 120.0, 30.0);
[predictButton setBackgroundImage:buttonOff forState:UIControlStateNormal];
[predictButton setBackgroundImage:buttonOn forState:UIControlStateHighlighted];
[predictButton setTitle:@"Predict" forState:UIControlStateNormal];
[predictButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
[predictButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:predictButton];
This is an example as well to create three buttons. Just move their location.
UIImage *buttonOff = [UIImage imageNamed:@"crysBallNorm.png"];
UIImage *buttonOn = [UIImage imageNamed:@"crysBallHigh.png"];
UIButton *predictButton = [UIButton alloc];
predictButton = [UIButton buttonWithType:UIButtonTypeCustom];
predictButton.frame = CGRectMake(180.0, 510.0, 120.0, 30.0);
[predictButton setBackgroundImage:buttonOff forState:UIControlStateNormal];
[predictButton setBackgroundImage:buttonOn forState:UIControlStateHighlighted];
[predictButton setTitle:@"Predict" forState:UIControlStateNormal];
[predictButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
[predictButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:predictButton];
answered Oct 7 '13 at 7:03
Rob O.Rob O.
312
312
add a comment |
add a comment |
You can create button by this code.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchDragInside];
[btn setTitle:@"click button" forState:UIControlStateNormal];
btn.frame = CGRectMake(50, 100, 80, 40);
[self.view addSubview:btn];
Here is the button action method
-(void)btnAction
{
NSLog(@"button clicked");
}
add a comment |
You can create button by this code.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchDragInside];
[btn setTitle:@"click button" forState:UIControlStateNormal];
btn.frame = CGRectMake(50, 100, 80, 40);
[self.view addSubview:btn];
Here is the button action method
-(void)btnAction
{
NSLog(@"button clicked");
}
add a comment |
You can create button by this code.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchDragInside];
[btn setTitle:@"click button" forState:UIControlStateNormal];
btn.frame = CGRectMake(50, 100, 80, 40);
[self.view addSubview:btn];
Here is the button action method
-(void)btnAction
{
NSLog(@"button clicked");
}
You can create button by this code.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchDragInside];
[btn setTitle:@"click button" forState:UIControlStateNormal];
btn.frame = CGRectMake(50, 100, 80, 40);
[self.view addSubview:btn];
Here is the button action method
-(void)btnAction
{
NSLog(@"button clicked");
}
answered Feb 7 '14 at 12:56
Vivek YadavVivek Yadav
440416
440416
add a comment |
add a comment |
For Swift 2.0:
let btnObject : UIButton = UIButton()
btnObject.frame = CGRect(x: 8, y: 89, width: 70, height: 22)
btnObject.titleLabel?.font = UIFont(name: "Helvetica Neue", size: 13)
btnObject.titleLabel?.textColor = UIColor.whiteColor()
btnObject.backgroundColor = UIColor(red: 189/255, green: 176/255, blue: 0/255, alpha: 1)
btnObject.titleLabel?.textAlignment = NSTextAlignment.Center
btnObject.addTarget(self, action: "btnbtnObjectClick:", forControlEvents: UIControlEvents.TouchUpInside)
subView.addSubview(btnObject)
add a comment |
For Swift 2.0:
let btnObject : UIButton = UIButton()
btnObject.frame = CGRect(x: 8, y: 89, width: 70, height: 22)
btnObject.titleLabel?.font = UIFont(name: "Helvetica Neue", size: 13)
btnObject.titleLabel?.textColor = UIColor.whiteColor()
btnObject.backgroundColor = UIColor(red: 189/255, green: 176/255, blue: 0/255, alpha: 1)
btnObject.titleLabel?.textAlignment = NSTextAlignment.Center
btnObject.addTarget(self, action: "btnbtnObjectClick:", forControlEvents: UIControlEvents.TouchUpInside)
subView.addSubview(btnObject)
add a comment |
For Swift 2.0:
let btnObject : UIButton = UIButton()
btnObject.frame = CGRect(x: 8, y: 89, width: 70, height: 22)
btnObject.titleLabel?.font = UIFont(name: "Helvetica Neue", size: 13)
btnObject.titleLabel?.textColor = UIColor.whiteColor()
btnObject.backgroundColor = UIColor(red: 189/255, green: 176/255, blue: 0/255, alpha: 1)
btnObject.titleLabel?.textAlignment = NSTextAlignment.Center
btnObject.addTarget(self, action: "btnbtnObjectClick:", forControlEvents: UIControlEvents.TouchUpInside)
subView.addSubview(btnObject)
For Swift 2.0:
let btnObject : UIButton = UIButton()
btnObject.frame = CGRect(x: 8, y: 89, width: 70, height: 22)
btnObject.titleLabel?.font = UIFont(name: "Helvetica Neue", size: 13)
btnObject.titleLabel?.textColor = UIColor.whiteColor()
btnObject.backgroundColor = UIColor(red: 189/255, green: 176/255, blue: 0/255, alpha: 1)
btnObject.titleLabel?.textAlignment = NSTextAlignment.Center
btnObject.addTarget(self, action: "btnbtnObjectClick:", forControlEvents: UIControlEvents.TouchUpInside)
subView.addSubview(btnObject)
edited Mar 12 '16 at 11:01
P.J.Radadiya
1,319815
1,319815
answered Dec 2 '15 at 12:36
Suraj SonawaneSuraj Sonawane
605619
605619
add a comment |
add a comment |
For creating UIButton programmatically we can create in both objective c and swift
SWIFT 3
let buttonSwift = UIButton(type: UIButtonType.system) as UIButton
//OR
let buttonSwift = UIButton(type: UIButtonType.Custom) as UIButton
//Set Frame for Button
buttonSwift.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
//Set title for button
buttonSwift.setTitle("ClickMe", for: .normal)
//If you want to set color for button title
buttonSwift.setTitleColor(UIColor.white, for: .normal)
//If you want to set Background color for button
buttonSwift.backgroundColor = UIColor.black
//If you want to set tag for button
buttonSwift.tag = 0
//If you want to add or set image for button
let image = UIImage(named: "YourImageName") as UIImage?
buttonSwift.setImage(image, for: .normal)
//If you want to add or set Background image for button
buttonSwift.setBackgroundImage(image, for: .normal)
//Add action for button
buttonSwift.addTarget(self, action: #selector(actionPressMe), for:.touchUpInside)
//Add button as SubView to Super View
self.view.addSubview(buttonSwift)
UIButton Action Method
func actionPressMe(sender: UIButton!)
{
NSLog("Clicked button tag is %@", sender.tag)
OR
print("Clicked button tag is (sender.tag)")
//Then do whatever you want to do here
........
}
OBJECTIVE C
UIButton *buttonObjectiveC = [UIButton buttonWithType:UIButtonTypeCustom];
OR
UIButton *buttonObjectiveC = [UIButton buttonWithType:UIButtonTypeSystem];
buttonObjectiveC.frame = CGRectMake(200, 100, 200, 100);
//Set title for button
[buttonObjectiveC setTitle:@"ClickMe" forState:UIControlStateNormal];
//If you want to set color for button title
[buttonObjectiveC setTitleColor:[UIColor whiteColor] forState: UIControlStateNormal];
//If you want to set Background color for button
[buttonObjectiveC setBackgroundColor:[UIColor blackColor]];
//If you want to set tag for button
buttonSwift.tag = 0;
//If you want to add or set image for button
UIImage *image = [UIImage imageNamed:@"YourImageName"];
[buttonObjectiveC setImage:image forState:UIControlStateNormal];
//If you want to add or set Background image for button
[buttonObjectiveC setBackgroundImage:image forState:UIControlStateNormal];
//Add action for button
[buttonObjectiveC addTarget:self action:@selector(actionPressMe:)forControlEvents:UIControlEventTouchUpInside];
//Add button as SubView to Super View
[self.view addSubview:buttonObjectiveC];
UIButton Action Method
- (void)actionPressMe:(UIButton *)sender
{
NSLog(@"Clicked button tag is %@",sender.tag);
//Then do whatever you want to do here
..........
}
Output Screenshot is
add a comment |
For creating UIButton programmatically we can create in both objective c and swift
SWIFT 3
let buttonSwift = UIButton(type: UIButtonType.system) as UIButton
//OR
let buttonSwift = UIButton(type: UIButtonType.Custom) as UIButton
//Set Frame for Button
buttonSwift.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
//Set title for button
buttonSwift.setTitle("ClickMe", for: .normal)
//If you want to set color for button title
buttonSwift.setTitleColor(UIColor.white, for: .normal)
//If you want to set Background color for button
buttonSwift.backgroundColor = UIColor.black
//If you want to set tag for button
buttonSwift.tag = 0
//If you want to add or set image for button
let image = UIImage(named: "YourImageName") as UIImage?
buttonSwift.setImage(image, for: .normal)
//If you want to add or set Background image for button
buttonSwift.setBackgroundImage(image, for: .normal)
//Add action for button
buttonSwift.addTarget(self, action: #selector(actionPressMe), for:.touchUpInside)
//Add button as SubView to Super View
self.view.addSubview(buttonSwift)
UIButton Action Method
func actionPressMe(sender: UIButton!)
{
NSLog("Clicked button tag is %@", sender.tag)
OR
print("Clicked button tag is (sender.tag)")
//Then do whatever you want to do here
........
}
OBJECTIVE C
UIButton *buttonObjectiveC = [UIButton buttonWithType:UIButtonTypeCustom];
OR
UIButton *buttonObjectiveC = [UIButton buttonWithType:UIButtonTypeSystem];
buttonObjectiveC.frame = CGRectMake(200, 100, 200, 100);
//Set title for button
[buttonObjectiveC setTitle:@"ClickMe" forState:UIControlStateNormal];
//If you want to set color for button title
[buttonObjectiveC setTitleColor:[UIColor whiteColor] forState: UIControlStateNormal];
//If you want to set Background color for button
[buttonObjectiveC setBackgroundColor:[UIColor blackColor]];
//If you want to set tag for button
buttonSwift.tag = 0;
//If you want to add or set image for button
UIImage *image = [UIImage imageNamed:@"YourImageName"];
[buttonObjectiveC setImage:image forState:UIControlStateNormal];
//If you want to add or set Background image for button
[buttonObjectiveC setBackgroundImage:image forState:UIControlStateNormal];
//Add action for button
[buttonObjectiveC addTarget:self action:@selector(actionPressMe:)forControlEvents:UIControlEventTouchUpInside];
//Add button as SubView to Super View
[self.view addSubview:buttonObjectiveC];
UIButton Action Method
- (void)actionPressMe:(UIButton *)sender
{
NSLog(@"Clicked button tag is %@",sender.tag);
//Then do whatever you want to do here
..........
}
Output Screenshot is
add a comment |
For creating UIButton programmatically we can create in both objective c and swift
SWIFT 3
let buttonSwift = UIButton(type: UIButtonType.system) as UIButton
//OR
let buttonSwift = UIButton(type: UIButtonType.Custom) as UIButton
//Set Frame for Button
buttonSwift.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
//Set title for button
buttonSwift.setTitle("ClickMe", for: .normal)
//If you want to set color for button title
buttonSwift.setTitleColor(UIColor.white, for: .normal)
//If you want to set Background color for button
buttonSwift.backgroundColor = UIColor.black
//If you want to set tag for button
buttonSwift.tag = 0
//If you want to add or set image for button
let image = UIImage(named: "YourImageName") as UIImage?
buttonSwift.setImage(image, for: .normal)
//If you want to add or set Background image for button
buttonSwift.setBackgroundImage(image, for: .normal)
//Add action for button
buttonSwift.addTarget(self, action: #selector(actionPressMe), for:.touchUpInside)
//Add button as SubView to Super View
self.view.addSubview(buttonSwift)
UIButton Action Method
func actionPressMe(sender: UIButton!)
{
NSLog("Clicked button tag is %@", sender.tag)
OR
print("Clicked button tag is (sender.tag)")
//Then do whatever you want to do here
........
}
OBJECTIVE C
UIButton *buttonObjectiveC = [UIButton buttonWithType:UIButtonTypeCustom];
OR
UIButton *buttonObjectiveC = [UIButton buttonWithType:UIButtonTypeSystem];
buttonObjectiveC.frame = CGRectMake(200, 100, 200, 100);
//Set title for button
[buttonObjectiveC setTitle:@"ClickMe" forState:UIControlStateNormal];
//If you want to set color for button title
[buttonObjectiveC setTitleColor:[UIColor whiteColor] forState: UIControlStateNormal];
//If you want to set Background color for button
[buttonObjectiveC setBackgroundColor:[UIColor blackColor]];
//If you want to set tag for button
buttonSwift.tag = 0;
//If you want to add or set image for button
UIImage *image = [UIImage imageNamed:@"YourImageName"];
[buttonObjectiveC setImage:image forState:UIControlStateNormal];
//If you want to add or set Background image for button
[buttonObjectiveC setBackgroundImage:image forState:UIControlStateNormal];
//Add action for button
[buttonObjectiveC addTarget:self action:@selector(actionPressMe:)forControlEvents:UIControlEventTouchUpInside];
//Add button as SubView to Super View
[self.view addSubview:buttonObjectiveC];
UIButton Action Method
- (void)actionPressMe:(UIButton *)sender
{
NSLog(@"Clicked button tag is %@",sender.tag);
//Then do whatever you want to do here
..........
}
Output Screenshot is
For creating UIButton programmatically we can create in both objective c and swift
SWIFT 3
let buttonSwift = UIButton(type: UIButtonType.system) as UIButton
//OR
let buttonSwift = UIButton(type: UIButtonType.Custom) as UIButton
//Set Frame for Button
buttonSwift.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
//Set title for button
buttonSwift.setTitle("ClickMe", for: .normal)
//If you want to set color for button title
buttonSwift.setTitleColor(UIColor.white, for: .normal)
//If you want to set Background color for button
buttonSwift.backgroundColor = UIColor.black
//If you want to set tag for button
buttonSwift.tag = 0
//If you want to add or set image for button
let image = UIImage(named: "YourImageName") as UIImage?
buttonSwift.setImage(image, for: .normal)
//If you want to add or set Background image for button
buttonSwift.setBackgroundImage(image, for: .normal)
//Add action for button
buttonSwift.addTarget(self, action: #selector(actionPressMe), for:.touchUpInside)
//Add button as SubView to Super View
self.view.addSubview(buttonSwift)
UIButton Action Method
func actionPressMe(sender: UIButton!)
{
NSLog("Clicked button tag is %@", sender.tag)
OR
print("Clicked button tag is (sender.tag)")
//Then do whatever you want to do here
........
}
OBJECTIVE C
UIButton *buttonObjectiveC = [UIButton buttonWithType:UIButtonTypeCustom];
OR
UIButton *buttonObjectiveC = [UIButton buttonWithType:UIButtonTypeSystem];
buttonObjectiveC.frame = CGRectMake(200, 100, 200, 100);
//Set title for button
[buttonObjectiveC setTitle:@"ClickMe" forState:UIControlStateNormal];
//If you want to set color for button title
[buttonObjectiveC setTitleColor:[UIColor whiteColor] forState: UIControlStateNormal];
//If you want to set Background color for button
[buttonObjectiveC setBackgroundColor:[UIColor blackColor]];
//If you want to set tag for button
buttonSwift.tag = 0;
//If you want to add or set image for button
UIImage *image = [UIImage imageNamed:@"YourImageName"];
[buttonObjectiveC setImage:image forState:UIControlStateNormal];
//If you want to add or set Background image for button
[buttonObjectiveC setBackgroundImage:image forState:UIControlStateNormal];
//Add action for button
[buttonObjectiveC addTarget:self action:@selector(actionPressMe:)forControlEvents:UIControlEventTouchUpInside];
//Add button as SubView to Super View
[self.view addSubview:buttonObjectiveC];
UIButton Action Method
- (void)actionPressMe:(UIButton *)sender
{
NSLog(@"Clicked button tag is %@",sender.tag);
//Then do whatever you want to do here
..........
}
Output Screenshot is
edited Mar 25 '17 at 12:09
answered Oct 23 '15 at 6:50
user3182143user3182143
7,85132130
7,85132130
add a comment |
add a comment |
-(void)addStuffToView
{
UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 20, 20)]; //(x, y, width, height of button on screen
[aButton setTitle:@"Button" forState:UIControlStateNormal];//puts the text on the button
aButton.titleLabel.font = somefont;//sets the font if one is already stated
aButton.titleLabel.font = [UIFont fontWithName:@"Arial-MT" size:12];//sets the font type and size
[aButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];//see back method below
[aButton setBackgroundImage:[UIImage imageNamed:@"someImage.png"] forState:UIControlStateNormal];//sets the image of the button
[self.view addSubview:back];
}
-(void)back
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle.....]
}
-(void)viewDidLoad
{
[super viewDidLoad];
[self addStuffToView];//adds all items built in this method to the view
}
add a comment |
-(void)addStuffToView
{
UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 20, 20)]; //(x, y, width, height of button on screen
[aButton setTitle:@"Button" forState:UIControlStateNormal];//puts the text on the button
aButton.titleLabel.font = somefont;//sets the font if one is already stated
aButton.titleLabel.font = [UIFont fontWithName:@"Arial-MT" size:12];//sets the font type and size
[aButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];//see back method below
[aButton setBackgroundImage:[UIImage imageNamed:@"someImage.png"] forState:UIControlStateNormal];//sets the image of the button
[self.view addSubview:back];
}
-(void)back
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle.....]
}
-(void)viewDidLoad
{
[super viewDidLoad];
[self addStuffToView];//adds all items built in this method to the view
}
add a comment |
-(void)addStuffToView
{
UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 20, 20)]; //(x, y, width, height of button on screen
[aButton setTitle:@"Button" forState:UIControlStateNormal];//puts the text on the button
aButton.titleLabel.font = somefont;//sets the font if one is already stated
aButton.titleLabel.font = [UIFont fontWithName:@"Arial-MT" size:12];//sets the font type and size
[aButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];//see back method below
[aButton setBackgroundImage:[UIImage imageNamed:@"someImage.png"] forState:UIControlStateNormal];//sets the image of the button
[self.view addSubview:back];
}
-(void)back
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle.....]
}
-(void)viewDidLoad
{
[super viewDidLoad];
[self addStuffToView];//adds all items built in this method to the view
}
-(void)addStuffToView
{
UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 20, 20)]; //(x, y, width, height of button on screen
[aButton setTitle:@"Button" forState:UIControlStateNormal];//puts the text on the button
aButton.titleLabel.font = somefont;//sets the font if one is already stated
aButton.titleLabel.font = [UIFont fontWithName:@"Arial-MT" size:12];//sets the font type and size
[aButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];//see back method below
[aButton setBackgroundImage:[UIImage imageNamed:@"someImage.png"] forState:UIControlStateNormal];//sets the image of the button
[self.view addSubview:back];
}
-(void)back
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle.....]
}
-(void)viewDidLoad
{
[super viewDidLoad];
[self addStuffToView];//adds all items built in this method to the view
}
answered Jun 3 '14 at 21:43
TomG103TomG103
135122
135122
add a comment |
add a comment |
For Swift 2.2 (with the with the new "selector" declaration).
let btn = UIButton(type: UIButtonType.System) as UIButton
btn.frame = CGRectMake(0, 0, 100, 20) // set any frame you want
btn.setTitle("MyAction", forState: UIControlState.Normal)
btn.addTarget(self, action: #selector(MyClass.myAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btn)
func myAction(sender:UIButton!){
// Some action
}
add a comment |
For Swift 2.2 (with the with the new "selector" declaration).
let btn = UIButton(type: UIButtonType.System) as UIButton
btn.frame = CGRectMake(0, 0, 100, 20) // set any frame you want
btn.setTitle("MyAction", forState: UIControlState.Normal)
btn.addTarget(self, action: #selector(MyClass.myAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btn)
func myAction(sender:UIButton!){
// Some action
}
add a comment |
For Swift 2.2 (with the with the new "selector" declaration).
let btn = UIButton(type: UIButtonType.System) as UIButton
btn.frame = CGRectMake(0, 0, 100, 20) // set any frame you want
btn.setTitle("MyAction", forState: UIControlState.Normal)
btn.addTarget(self, action: #selector(MyClass.myAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btn)
func myAction(sender:UIButton!){
// Some action
}
For Swift 2.2 (with the with the new "selector" declaration).
let btn = UIButton(type: UIButtonType.System) as UIButton
btn.frame = CGRectMake(0, 0, 100, 20) // set any frame you want
btn.setTitle("MyAction", forState: UIControlState.Normal)
btn.addTarget(self, action: #selector(MyClass.myAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btn)
func myAction(sender:UIButton!){
// Some action
}
answered May 30 '16 at 13:55
Pavle MijatovicPavle Mijatovic
48256
48256
add a comment |
add a comment |
You can implement it in your ViewDidLoad
Method:
continuebtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 100, view1.frame.size.width-20, 40)];
[continuebtn setBackgroundColor:[UIColor grayColor]];
[continuebtn setTitle:@"Continue" forState:UIControlStateNormal];
continuebtn.layer.cornerRadius = 10;
continuebtn.layer.borderWidth =1.0;
continuebtn.layer.borderColor = [UIColor blackColor].CGColor;
[continuebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[continuebtn addTarget:self action:@selector(continuetonext) forControlEvents:UIControlEventTouchUpInside];
[view1 addSubview:continuebtn];
Where continuetonext
is:
-(void)continuetonext
{
GeneratePasswordVC *u = [[GeneratePasswordVC alloc]init];
[self.navigationController pushViewController:u animated:YES];
}
add a comment |
You can implement it in your ViewDidLoad
Method:
continuebtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 100, view1.frame.size.width-20, 40)];
[continuebtn setBackgroundColor:[UIColor grayColor]];
[continuebtn setTitle:@"Continue" forState:UIControlStateNormal];
continuebtn.layer.cornerRadius = 10;
continuebtn.layer.borderWidth =1.0;
continuebtn.layer.borderColor = [UIColor blackColor].CGColor;
[continuebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[continuebtn addTarget:self action:@selector(continuetonext) forControlEvents:UIControlEventTouchUpInside];
[view1 addSubview:continuebtn];
Where continuetonext
is:
-(void)continuetonext
{
GeneratePasswordVC *u = [[GeneratePasswordVC alloc]init];
[self.navigationController pushViewController:u animated:YES];
}
add a comment |
You can implement it in your ViewDidLoad
Method:
continuebtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 100, view1.frame.size.width-20, 40)];
[continuebtn setBackgroundColor:[UIColor grayColor]];
[continuebtn setTitle:@"Continue" forState:UIControlStateNormal];
continuebtn.layer.cornerRadius = 10;
continuebtn.layer.borderWidth =1.0;
continuebtn.layer.borderColor = [UIColor blackColor].CGColor;
[continuebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[continuebtn addTarget:self action:@selector(continuetonext) forControlEvents:UIControlEventTouchUpInside];
[view1 addSubview:continuebtn];
Where continuetonext
is:
-(void)continuetonext
{
GeneratePasswordVC *u = [[GeneratePasswordVC alloc]init];
[self.navigationController pushViewController:u animated:YES];
}
You can implement it in your ViewDidLoad
Method:
continuebtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 100, view1.frame.size.width-20, 40)];
[continuebtn setBackgroundColor:[UIColor grayColor]];
[continuebtn setTitle:@"Continue" forState:UIControlStateNormal];
continuebtn.layer.cornerRadius = 10;
continuebtn.layer.borderWidth =1.0;
continuebtn.layer.borderColor = [UIColor blackColor].CGColor;
[continuebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[continuebtn addTarget:self action:@selector(continuetonext) forControlEvents:UIControlEventTouchUpInside];
[view1 addSubview:continuebtn];
Where continuetonext
is:
-(void)continuetonext
{
GeneratePasswordVC *u = [[GeneratePasswordVC alloc]init];
[self.navigationController pushViewController:u animated:YES];
}
edited Jul 19 '16 at 12:39
Tom11
1,39841942
1,39841942
answered Oct 30 '15 at 6:07
Abhinandan PratapAbhinandan Pratap
1,2111928
1,2111928
add a comment |
add a comment |
As of Swift 3, several changes have been made to the syntax.
Here is how you would go about creating a basic button as of Swift 3:
let button = UIButton(type: UIButtonType.system) as UIButton
button.frame = CGRect(x: 100, y: 100, width: 100, height: 50)
button.backgroundColor = UIColor.green
button.setTitle("Example Button", for: UIControlState.normal)
self.view.addSubview(button)
Here are the changes that have been made since previous versions of Swift:
let button = UIButton(type: UIButtonType.System) as UIButton
// system no longer capitalised
button.frame = CGRectMake(100, 100, 100, 50)
// CGRectMake has been removed as of Swift 3
button.backgroundColor = UIColor.greenColor()
// greenColor replaced with green
button.setTitle("Example Button", forState: UIControlState.Normal)
// normal is no longer capitalised
self.view.addSubview(button)
add a comment |
As of Swift 3, several changes have been made to the syntax.
Here is how you would go about creating a basic button as of Swift 3:
let button = UIButton(type: UIButtonType.system) as UIButton
button.frame = CGRect(x: 100, y: 100, width: 100, height: 50)
button.backgroundColor = UIColor.green
button.setTitle("Example Button", for: UIControlState.normal)
self.view.addSubview(button)
Here are the changes that have been made since previous versions of Swift:
let button = UIButton(type: UIButtonType.System) as UIButton
// system no longer capitalised
button.frame = CGRectMake(100, 100, 100, 50)
// CGRectMake has been removed as of Swift 3
button.backgroundColor = UIColor.greenColor()
// greenColor replaced with green
button.setTitle("Example Button", forState: UIControlState.Normal)
// normal is no longer capitalised
self.view.addSubview(button)
add a comment |
As of Swift 3, several changes have been made to the syntax.
Here is how you would go about creating a basic button as of Swift 3:
let button = UIButton(type: UIButtonType.system) as UIButton
button.frame = CGRect(x: 100, y: 100, width: 100, height: 50)
button.backgroundColor = UIColor.green
button.setTitle("Example Button", for: UIControlState.normal)
self.view.addSubview(button)
Here are the changes that have been made since previous versions of Swift:
let button = UIButton(type: UIButtonType.System) as UIButton
// system no longer capitalised
button.frame = CGRectMake(100, 100, 100, 50)
// CGRectMake has been removed as of Swift 3
button.backgroundColor = UIColor.greenColor()
// greenColor replaced with green
button.setTitle("Example Button", forState: UIControlState.Normal)
// normal is no longer capitalised
self.view.addSubview(button)
As of Swift 3, several changes have been made to the syntax.
Here is how you would go about creating a basic button as of Swift 3:
let button = UIButton(type: UIButtonType.system) as UIButton
button.frame = CGRect(x: 100, y: 100, width: 100, height: 50)
button.backgroundColor = UIColor.green
button.setTitle("Example Button", for: UIControlState.normal)
self.view.addSubview(button)
Here are the changes that have been made since previous versions of Swift:
let button = UIButton(type: UIButtonType.System) as UIButton
// system no longer capitalised
button.frame = CGRectMake(100, 100, 100, 50)
// CGRectMake has been removed as of Swift 3
button.backgroundColor = UIColor.greenColor()
// greenColor replaced with green
button.setTitle("Example Button", forState: UIControlState.Normal)
// normal is no longer capitalised
self.view.addSubview(button)
answered Nov 14 '16 at 17:21
GJZGJZ
1,47721432
1,47721432
add a comment |
add a comment |
Try it....
UIButton *finalPriceBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
finalPriceBtn.frame=CGRectMake(260, 25, 45, 15);
[finalPriceBtn addTarget:self action:@selector(goBtnClk:) forControlEvents:UIControlEventTouchUpInside];
finalPriceBtn.titleLabel.font=[UIFont systemFontOfSize:12];
[finalPriceBtn setTitle:[NSString stringWithFormat:@"$%.2f",tempVal] forState:UIControlStateNormal];
finalPriceBtn.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1];
finalPriceBtn.titleLabel.textAlignment=UITextAlignmentLeft;
[imageView addSubview:finalPriceBtn];
Hope i helped.
add a comment |
Try it....
UIButton *finalPriceBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
finalPriceBtn.frame=CGRectMake(260, 25, 45, 15);
[finalPriceBtn addTarget:self action:@selector(goBtnClk:) forControlEvents:UIControlEventTouchUpInside];
finalPriceBtn.titleLabel.font=[UIFont systemFontOfSize:12];
[finalPriceBtn setTitle:[NSString stringWithFormat:@"$%.2f",tempVal] forState:UIControlStateNormal];
finalPriceBtn.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1];
finalPriceBtn.titleLabel.textAlignment=UITextAlignmentLeft;
[imageView addSubview:finalPriceBtn];
Hope i helped.
add a comment |
Try it....
UIButton *finalPriceBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
finalPriceBtn.frame=CGRectMake(260, 25, 45, 15);
[finalPriceBtn addTarget:self action:@selector(goBtnClk:) forControlEvents:UIControlEventTouchUpInside];
finalPriceBtn.titleLabel.font=[UIFont systemFontOfSize:12];
[finalPriceBtn setTitle:[NSString stringWithFormat:@"$%.2f",tempVal] forState:UIControlStateNormal];
finalPriceBtn.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1];
finalPriceBtn.titleLabel.textAlignment=UITextAlignmentLeft;
[imageView addSubview:finalPriceBtn];
Hope i helped.
Try it....
UIButton *finalPriceBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
finalPriceBtn.frame=CGRectMake(260, 25, 45, 15);
[finalPriceBtn addTarget:self action:@selector(goBtnClk:) forControlEvents:UIControlEventTouchUpInside];
finalPriceBtn.titleLabel.font=[UIFont systemFontOfSize:12];
[finalPriceBtn setTitle:[NSString stringWithFormat:@"$%.2f",tempVal] forState:UIControlStateNormal];
finalPriceBtn.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1];
finalPriceBtn.titleLabel.textAlignment=UITextAlignmentLeft;
[imageView addSubview:finalPriceBtn];
Hope i helped.
answered Apr 27 '13 at 10:10
Chirag PipaliyaChirag Pipaliya
1,1561020
1,1561020
add a comment |
add a comment |
UIButton *custombutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[custombutton addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[custombutton setTitle:@"Click" forState:UIControlStateNormal];
custombutton.frame = CGRectMake(80.0, 110.0, 160.0, 40.0);
custombutton.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1];
[custombutton setImage:[UIImage imageNamed:@"hh.png"] forState:UIControlStateNormal];
[view addSubview:custombutton];
add a comment |
UIButton *custombutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[custombutton addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[custombutton setTitle:@"Click" forState:UIControlStateNormal];
custombutton.frame = CGRectMake(80.0, 110.0, 160.0, 40.0);
custombutton.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1];
[custombutton setImage:[UIImage imageNamed:@"hh.png"] forState:UIControlStateNormal];
[view addSubview:custombutton];
add a comment |
UIButton *custombutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[custombutton addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[custombutton setTitle:@"Click" forState:UIControlStateNormal];
custombutton.frame = CGRectMake(80.0, 110.0, 160.0, 40.0);
custombutton.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1];
[custombutton setImage:[UIImage imageNamed:@"hh.png"] forState:UIControlStateNormal];
[view addSubview:custombutton];
UIButton *custombutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[custombutton addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[custombutton setTitle:@"Click" forState:UIControlStateNormal];
custombutton.frame = CGRectMake(80.0, 110.0, 160.0, 40.0);
custombutton.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1];
[custombutton setImage:[UIImage imageNamed:@"hh.png"] forState:UIControlStateNormal];
[view addSubview:custombutton];
answered Dec 31 '13 at 10:58
JCP ChandranJCP Chandran
92
92
add a comment |
add a comment |
For Swift 3 (even shorter code)
let button = UIButton(type: UIButtonType.custom)
button.frame = CGRect(x: 0, y: 0, width: 200.0, height: 40.0)
button.addTarget(nil, action: #selector(tapButton(_:)), for: UIControlEvents.touchUpInside)
button.tintColor = UIColor.white
button.backgroundColor = UIColor.red
button.setBackgroundImage(UIImage(named: "ImageName"), for: UIControlState.normal)
button.setTitle("MyTitle", for: UIControlState.normal)
button.isEnabled = true
func tapButton(sender: UIButton) {
}
add a comment |
For Swift 3 (even shorter code)
let button = UIButton(type: UIButtonType.custom)
button.frame = CGRect(x: 0, y: 0, width: 200.0, height: 40.0)
button.addTarget(nil, action: #selector(tapButton(_:)), for: UIControlEvents.touchUpInside)
button.tintColor = UIColor.white
button.backgroundColor = UIColor.red
button.setBackgroundImage(UIImage(named: "ImageName"), for: UIControlState.normal)
button.setTitle("MyTitle", for: UIControlState.normal)
button.isEnabled = true
func tapButton(sender: UIButton) {
}
add a comment |
For Swift 3 (even shorter code)
let button = UIButton(type: UIButtonType.custom)
button.frame = CGRect(x: 0, y: 0, width: 200.0, height: 40.0)
button.addTarget(nil, action: #selector(tapButton(_:)), for: UIControlEvents.touchUpInside)
button.tintColor = UIColor.white
button.backgroundColor = UIColor.red
button.setBackgroundImage(UIImage(named: "ImageName"), for: UIControlState.normal)
button.setTitle("MyTitle", for: UIControlState.normal)
button.isEnabled = true
func tapButton(sender: UIButton) {
}
For Swift 3 (even shorter code)
let button = UIButton(type: UIButtonType.custom)
button.frame = CGRect(x: 0, y: 0, width: 200.0, height: 40.0)
button.addTarget(nil, action: #selector(tapButton(_:)), for: UIControlEvents.touchUpInside)
button.tintColor = UIColor.white
button.backgroundColor = UIColor.red
button.setBackgroundImage(UIImage(named: "ImageName"), for: UIControlState.normal)
button.setTitle("MyTitle", for: UIControlState.normal)
button.isEnabled = true
func tapButton(sender: UIButton) {
}
answered Sep 4 '16 at 16:25
pedrouanpedrouan
9,40924261
9,40924261
add a comment |
add a comment |
Swift3 version should be
let myButton:UIButton = {
let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
myButton.setTitle("Hai Touch Me", for: .normal)
myButton.setTitleColor(UIColor.blue, for: .normal)
myButton.frame = CGRect(x: 20, y: 20, width: 100, height: 40)
myButton.addTarget(self, action: #selector(ViewController.pressedAction(_:)), for: .touchUpInside)
self.view.addSubview(myButton)
return myButton
}()
add a comment |
Swift3 version should be
let myButton:UIButton = {
let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
myButton.setTitle("Hai Touch Me", for: .normal)
myButton.setTitleColor(UIColor.blue, for: .normal)
myButton.frame = CGRect(x: 20, y: 20, width: 100, height: 40)
myButton.addTarget(self, action: #selector(ViewController.pressedAction(_:)), for: .touchUpInside)
self.view.addSubview(myButton)
return myButton
}()
add a comment |
Swift3 version should be
let myButton:UIButton = {
let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
myButton.setTitle("Hai Touch Me", for: .normal)
myButton.setTitleColor(UIColor.blue, for: .normal)
myButton.frame = CGRect(x: 20, y: 20, width: 100, height: 40)
myButton.addTarget(self, action: #selector(ViewController.pressedAction(_:)), for: .touchUpInside)
self.view.addSubview(myButton)
return myButton
}()
Swift3 version should be
let myButton:UIButton = {
let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
myButton.setTitle("Hai Touch Me", for: .normal)
myButton.setTitleColor(UIColor.blue, for: .normal)
myButton.frame = CGRect(x: 20, y: 20, width: 100, height: 40)
myButton.addTarget(self, action: #selector(ViewController.pressedAction(_:)), for: .touchUpInside)
self.view.addSubview(myButton)
return myButton
}()
answered Dec 29 '16 at 11:29
JackyJacky
2,62742943
2,62742943
add a comment |
add a comment |
UIButton *buttonName = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
[buttonName addTarget:self
action:@selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[buttonName setTitle:@"Show View" forState:UIControlStateNormal];
.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); [view
addSubview:buttonName];
add a comment |
UIButton *buttonName = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
[buttonName addTarget:self
action:@selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[buttonName setTitle:@"Show View" forState:UIControlStateNormal];
.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); [view
addSubview:buttonName];
add a comment |
UIButton *buttonName = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
[buttonName addTarget:self
action:@selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[buttonName setTitle:@"Show View" forState:UIControlStateNormal];
.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); [view
addSubview:buttonName];
UIButton *buttonName = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
[buttonName addTarget:self
action:@selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[buttonName setTitle:@"Show View" forState:UIControlStateNormal];
.frame = CGRectMake(80.0, 210.0, 160.0, 40.0); [view
addSubview:buttonName];
edited Apr 17 '18 at 11:03
byJeevan
2,36012244
2,36012244
answered Jun 9 '15 at 13:47
Nilesh ParmarNilesh Parmar
354411
354411
add a comment |
add a comment |
In Swift 4.1 and Xcode 10
Basically we have two types of buttons.
1) System type button
2) Custom type button (In custom type button we can set background image for button)
And these two types of buttons has few control states https://developer.apple.com/documentation/uikit/uicontrol/state
Important states are
1) Normal state
2) Selected state
3) Highlighted state
4) Disabled state etc...
//For system type button
let button = UIButton(type: .system)
button.frame = CGRect(x: 100, y: 250, width: 100, height: 50)
// button.backgroundColor = .blue
button.setTitle("Button", for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 13.0)
button.titleLabel?.textAlignment = .center//Text alighment center
button.titleLabel?.numberOfLines = 0//To display multiple lines in UIButton
button.titleLabel?.lineBreakMode = .byWordWrapping//By word wrapping
button.tag = 1//To assign tag value
button.btnProperties()//Call UIButton properties from extension function
button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button)
//For custom type button (add image to your button)
let button2 = UIButton(type: .custom)
button2.frame = CGRect(x: 100, y: 400, width: 100, height: 50)
// button2.backgroundColor = .blue
button2.setImage(UIImage.init(named: "img.png"), for: .normal)
button2.tag = 2
button2.btnProperties()//Call UIButton properties from extension function
button2.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button2)
@objc func buttonClicked(sender:UIButton) {
print("Button (sender.tag) clicked")
}
//You can add UIButton properties like this also
extension UIButton {
func btnProperties() {
layer.cornerRadius = 10//Set button corner radious
clipsToBounds = true
backgroundColor = .blue//Set background colour
//titleLabel?.textAlignment = .center//add properties like this
}
}
add a comment |
In Swift 4.1 and Xcode 10
Basically we have two types of buttons.
1) System type button
2) Custom type button (In custom type button we can set background image for button)
And these two types of buttons has few control states https://developer.apple.com/documentation/uikit/uicontrol/state
Important states are
1) Normal state
2) Selected state
3) Highlighted state
4) Disabled state etc...
//For system type button
let button = UIButton(type: .system)
button.frame = CGRect(x: 100, y: 250, width: 100, height: 50)
// button.backgroundColor = .blue
button.setTitle("Button", for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 13.0)
button.titleLabel?.textAlignment = .center//Text alighment center
button.titleLabel?.numberOfLines = 0//To display multiple lines in UIButton
button.titleLabel?.lineBreakMode = .byWordWrapping//By word wrapping
button.tag = 1//To assign tag value
button.btnProperties()//Call UIButton properties from extension function
button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button)
//For custom type button (add image to your button)
let button2 = UIButton(type: .custom)
button2.frame = CGRect(x: 100, y: 400, width: 100, height: 50)
// button2.backgroundColor = .blue
button2.setImage(UIImage.init(named: "img.png"), for: .normal)
button2.tag = 2
button2.btnProperties()//Call UIButton properties from extension function
button2.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button2)
@objc func buttonClicked(sender:UIButton) {
print("Button (sender.tag) clicked")
}
//You can add UIButton properties like this also
extension UIButton {
func btnProperties() {
layer.cornerRadius = 10//Set button corner radious
clipsToBounds = true
backgroundColor = .blue//Set background colour
//titleLabel?.textAlignment = .center//add properties like this
}
}
add a comment |
In Swift 4.1 and Xcode 10
Basically we have two types of buttons.
1) System type button
2) Custom type button (In custom type button we can set background image for button)
And these two types of buttons has few control states https://developer.apple.com/documentation/uikit/uicontrol/state
Important states are
1) Normal state
2) Selected state
3) Highlighted state
4) Disabled state etc...
//For system type button
let button = UIButton(type: .system)
button.frame = CGRect(x: 100, y: 250, width: 100, height: 50)
// button.backgroundColor = .blue
button.setTitle("Button", for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 13.0)
button.titleLabel?.textAlignment = .center//Text alighment center
button.titleLabel?.numberOfLines = 0//To display multiple lines in UIButton
button.titleLabel?.lineBreakMode = .byWordWrapping//By word wrapping
button.tag = 1//To assign tag value
button.btnProperties()//Call UIButton properties from extension function
button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button)
//For custom type button (add image to your button)
let button2 = UIButton(type: .custom)
button2.frame = CGRect(x: 100, y: 400, width: 100, height: 50)
// button2.backgroundColor = .blue
button2.setImage(UIImage.init(named: "img.png"), for: .normal)
button2.tag = 2
button2.btnProperties()//Call UIButton properties from extension function
button2.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button2)
@objc func buttonClicked(sender:UIButton) {
print("Button (sender.tag) clicked")
}
//You can add UIButton properties like this also
extension UIButton {
func btnProperties() {
layer.cornerRadius = 10//Set button corner radious
clipsToBounds = true
backgroundColor = .blue//Set background colour
//titleLabel?.textAlignment = .center//add properties like this
}
}
In Swift 4.1 and Xcode 10
Basically we have two types of buttons.
1) System type button
2) Custom type button (In custom type button we can set background image for button)
And these two types of buttons has few control states https://developer.apple.com/documentation/uikit/uicontrol/state
Important states are
1) Normal state
2) Selected state
3) Highlighted state
4) Disabled state etc...
//For system type button
let button = UIButton(type: .system)
button.frame = CGRect(x: 100, y: 250, width: 100, height: 50)
// button.backgroundColor = .blue
button.setTitle("Button", for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 13.0)
button.titleLabel?.textAlignment = .center//Text alighment center
button.titleLabel?.numberOfLines = 0//To display multiple lines in UIButton
button.titleLabel?.lineBreakMode = .byWordWrapping//By word wrapping
button.tag = 1//To assign tag value
button.btnProperties()//Call UIButton properties from extension function
button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button)
//For custom type button (add image to your button)
let button2 = UIButton(type: .custom)
button2.frame = CGRect(x: 100, y: 400, width: 100, height: 50)
// button2.backgroundColor = .blue
button2.setImage(UIImage.init(named: "img.png"), for: .normal)
button2.tag = 2
button2.btnProperties()//Call UIButton properties from extension function
button2.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button2)
@objc func buttonClicked(sender:UIButton) {
print("Button (sender.tag) clicked")
}
//You can add UIButton properties like this also
extension UIButton {
func btnProperties() {
layer.cornerRadius = 10//Set button corner radious
clipsToBounds = true
backgroundColor = .blue//Set background colour
//titleLabel?.textAlignment = .center//add properties like this
}
}
answered Jan 20 at 6:35
iOSiOS
2,37711942
2,37711942
add a comment |
add a comment |
UIButton *btnname = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnname setTitle:@"Click Me" forState:UIControlStateNormal];
btnname.frame = CGRectMake(10, 10, 100, 140);
[self.view addSubview:btnname];
add a comment |
UIButton *btnname = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnname setTitle:@"Click Me" forState:UIControlStateNormal];
btnname.frame = CGRectMake(10, 10, 100, 140);
[self.view addSubview:btnname];
add a comment |
UIButton *btnname = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnname setTitle:@"Click Me" forState:UIControlStateNormal];
btnname.frame = CGRectMake(10, 10, 100, 140);
[self.view addSubview:btnname];
UIButton *btnname = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnname setTitle:@"Click Me" forState:UIControlStateNormal];
btnname.frame = CGRectMake(10, 10, 100, 140);
[self.view addSubview:btnname];
edited Jan 15 '14 at 10:50
Muddu Patil
54211023
54211023
answered Nov 20 '13 at 7:33
HRNHRN
1067
1067
add a comment |
add a comment |
UIButton * tmpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[tmpBtn addTarget:self action:@selector(clearCart:) forControlEvents:UIControlEventTouchUpInside];
tmpBtn.tag = k;
tmpBtn.frame = CGRectMake(45, 0, 15, 15);
[tmpBtn setBackgroundImage:[UIImage imageNamed:@"CloseButton.png"] forState:UIControlStateNormal];
[self.view addSubview:tmpBtn];
add a comment |
UIButton * tmpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[tmpBtn addTarget:self action:@selector(clearCart:) forControlEvents:UIControlEventTouchUpInside];
tmpBtn.tag = k;
tmpBtn.frame = CGRectMake(45, 0, 15, 15);
[tmpBtn setBackgroundImage:[UIImage imageNamed:@"CloseButton.png"] forState:UIControlStateNormal];
[self.view addSubview:tmpBtn];
add a comment |
UIButton * tmpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[tmpBtn addTarget:self action:@selector(clearCart:) forControlEvents:UIControlEventTouchUpInside];
tmpBtn.tag = k;
tmpBtn.frame = CGRectMake(45, 0, 15, 15);
[tmpBtn setBackgroundImage:[UIImage imageNamed:@"CloseButton.png"] forState:UIControlStateNormal];
[self.view addSubview:tmpBtn];
UIButton * tmpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[tmpBtn addTarget:self action:@selector(clearCart:) forControlEvents:UIControlEventTouchUpInside];
tmpBtn.tag = k;
tmpBtn.frame = CGRectMake(45, 0, 15, 15);
[tmpBtn setBackgroundImage:[UIImage imageNamed:@"CloseButton.png"] forState:UIControlStateNormal];
[self.view addSubview:tmpBtn];
answered Mar 1 '14 at 5:25
MohitMohit
2,8332130
2,8332130
add a comment |
add a comment |
1 2
next
protected by Community♦ Sep 25 '15 at 20:15
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
webindream.com/how-to-add-uibutton-programmatically-in-swift has a good tutorial to add uibutton programmatically
– farhad rubel
Sep 26 '16 at 11:10
Create UIButton in Swift, stackoverflow.com/questions/24102191/…
– iOS
Jan 11 at 12:10