How do I create a basic UIButton programmatically?












528















How can I create a basic UIButton programmatically? For example in my view controller, when executing the viewDidLoad method, three UIButtons will be created dynamically and its layout or properties are set.










share|improve this question

























  • 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
















528















How can I create a basic UIButton programmatically? For example in my view controller, when executing the viewDidLoad method, three UIButtons will be created dynamically and its layout or properties are set.










share|improve this question

























  • 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














528












528








528


136






How can I create a basic UIButton programmatically? For example in my view controller, when executing the viewDidLoad method, three UIButtons will be created dynamically and its layout or properties are set.










share|improve this question
















How can I create a basic UIButton programmatically? For example in my view controller, when executing the viewDidLoad method, three UIButtons will be created dynamically and its layout or properties are set.







objective-c uibutton programmatically-created






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












34 Answers
34






active

oldest

votes













1 2
next












1162














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





share|improve this answer


























  • 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



















92














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





share|improve this answer


























  • 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



















77














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)")
}





share|improve this answer


























  • 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













  • 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



















30














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.






share|improve this answer


























  • 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



















24














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





share|improve this answer

































    23














    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!



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








    share|improve this answer





















    • 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





















    20














    'action:@selector(aMethod:)' write method like this :



    - (void)aMethod:(UIButton*)button
    {
    NSLog(@"Button clicked.");
    }


    It works for me. Thanks. KS.






    share|improve this answer


























    • I was looking for this , thanks a lot

      – smoothumut
      Jan 20 '15 at 14:11



















    14














    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)





    share|improve this answer

































      9














      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);
      }





      share|improve this answer


























      • 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



















      6














      You can just put the creator instance within a loop and dynamically add names from an array if you so wish.






      share|improve this answer


























      • yup i tried mahboudz, and at the same time, i tried the looping. thanks

        – domlao
        Mar 2 '10 at 23:31



















      5














      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.






      share|improve this answer

































        4














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





        share|improve this answer

































          4














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





          share|improve this answer































            3














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





            share|improve this answer































              3














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





              share|improve this answer































                3














                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");
                }





                share|improve this answer































                  2














                  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)





                  share|improve this answer

































                    2














                    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



                    enter image description here



                    enter image description here






                    share|improve this answer

































                      1














                      -(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
                      }





                      share|improve this answer































                        1














                        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
                        }





                        share|improve this answer































                          1














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





                          share|improve this answer

































                            1














                            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)





                            share|improve this answer































                              0














                              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.






                              share|improve this answer































                                0














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





                                share|improve this answer































                                  0














                                  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) {

                                  }





                                  share|improve this answer































                                    0














                                    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

                                    }()





                                    share|improve this answer































                                      0














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





                                      share|improve this answer

































                                        0














                                        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
                                        }
                                        }





                                        share|improve this answer































                                          -1














                                          UIButton *btnname = [UIButton buttonWithType:UIButtonTypeRoundedRect];

                                          [btnname setTitle:@"Click Me" forState:UIControlStateNormal];

                                          btnname.frame = CGRectMake(10, 10, 100, 140);

                                          [self.view addSubview:btnname];





                                          share|improve this answer

































                                            -1














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





                                            share|improve this answer



























                                              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










                                              1162














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





                                              share|improve this answer


























                                              • 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
















                                              1162














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





                                              share|improve this answer


























                                              • 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














                                              1162












                                              1162








                                              1162







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





                                              share|improve this answer















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






                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              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



















                                              • 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













                                              92














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





                                              share|improve this answer


























                                              • 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
















                                              92














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





                                              share|improve this answer


























                                              • 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














                                              92












                                              92








                                              92







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





                                              share|improve this answer















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






                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              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



















                                              • 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











                                              77














                                              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)")
                                              }





                                              share|improve this answer


























                                              • 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













                                              • 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
















                                              77














                                              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)")
                                              }





                                              share|improve this answer


























                                              • 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













                                              • 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














                                              77












                                              77








                                              77







                                              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)")
                                              }





                                              share|improve this answer















                                              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)")
                                              }






                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Aug 21 '17 at 7:50

























                                              answered Oct 18 '13 at 13:13









                                              Anbu.KarthikAnbu.Karthik

                                              61.6k16121111




                                              61.6k16121111













                                              • 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













                                              • 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











                                              • @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

















                                              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











                                              30














                                              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.






                                              share|improve this answer


























                                              • 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
















                                              30














                                              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.






                                              share|improve this answer


























                                              • 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














                                              30












                                              30








                                              30







                                              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.






                                              share|improve this answer















                                              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.







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              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



















                                              • 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











                                              24














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





                                              share|improve this answer






























                                                24














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





                                                share|improve this answer




























                                                  24












                                                  24








                                                  24







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





                                                  share|improve this answer















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






                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Apr 3 '12 at 20:55









                                                  Mazyod

                                                  17.5k675136




                                                  17.5k675136










                                                  answered Mar 27 '12 at 11:42









                                                  BirjuBirju

                                                  877828




                                                  877828























                                                      23














                                                      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!



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








                                                      share|improve this answer





















                                                      • 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


















                                                      23














                                                      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!



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








                                                      share|improve this answer





















                                                      • 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
















                                                      23












                                                      23








                                                      23







                                                      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!



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








                                                      share|improve this answer















                                                      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!



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









                                                      share|improve this answer














                                                      share|improve this answer



                                                      share|improve this answer








                                                      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
















                                                      • 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













                                                      20














                                                      'action:@selector(aMethod:)' write method like this :



                                                      - (void)aMethod:(UIButton*)button
                                                      {
                                                      NSLog(@"Button clicked.");
                                                      }


                                                      It works for me. Thanks. KS.






                                                      share|improve this answer


























                                                      • I was looking for this , thanks a lot

                                                        – smoothumut
                                                        Jan 20 '15 at 14:11
















                                                      20














                                                      'action:@selector(aMethod:)' write method like this :



                                                      - (void)aMethod:(UIButton*)button
                                                      {
                                                      NSLog(@"Button clicked.");
                                                      }


                                                      It works for me. Thanks. KS.






                                                      share|improve this answer


























                                                      • I was looking for this , thanks a lot

                                                        – smoothumut
                                                        Jan 20 '15 at 14:11














                                                      20












                                                      20








                                                      20







                                                      'action:@selector(aMethod:)' write method like this :



                                                      - (void)aMethod:(UIButton*)button
                                                      {
                                                      NSLog(@"Button clicked.");
                                                      }


                                                      It works for me. Thanks. KS.






                                                      share|improve this answer















                                                      'action:@selector(aMethod:)' write method like this :



                                                      - (void)aMethod:(UIButton*)button
                                                      {
                                                      NSLog(@"Button clicked.");
                                                      }


                                                      It works for me. Thanks. KS.







                                                      share|improve this answer














                                                      share|improve this answer



                                                      share|improve this answer








                                                      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



















                                                      • 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











                                                      14














                                                      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)





                                                      share|improve this answer






























                                                        14














                                                        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)





                                                        share|improve this answer




























                                                          14












                                                          14








                                                          14







                                                          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)





                                                          share|improve this answer















                                                          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)






                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited Apr 20 '16 at 12:07

























                                                          answered Sep 11 '12 at 6:09









                                                          Himanshu padiaHimanshu padia

                                                          4,4053137




                                                          4,4053137























                                                              9














                                                              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);
                                                              }





                                                              share|improve this answer


























                                                              • 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
















                                                              9














                                                              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);
                                                              }





                                                              share|improve this answer


























                                                              • 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














                                                              9












                                                              9








                                                              9







                                                              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);
                                                              }





                                                              share|improve this answer















                                                              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);
                                                              }






                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              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



















                                                              • 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











                                                              6














                                                              You can just put the creator instance within a loop and dynamically add names from an array if you so wish.






                                                              share|improve this answer


























                                                              • yup i tried mahboudz, and at the same time, i tried the looping. thanks

                                                                – domlao
                                                                Mar 2 '10 at 23:31
















                                                              6














                                                              You can just put the creator instance within a loop and dynamically add names from an array if you so wish.






                                                              share|improve this answer


























                                                              • yup i tried mahboudz, and at the same time, i tried the looping. thanks

                                                                – domlao
                                                                Mar 2 '10 at 23:31














                                                              6












                                                              6








                                                              6







                                                              You can just put the creator instance within a loop and dynamically add names from an array if you so wish.






                                                              share|improve this answer















                                                              You can just put the creator instance within a loop and dynamically add names from an array if you so wish.







                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              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



















                                                              • 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











                                                              5














                                                              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.






                                                              share|improve this answer






























                                                                5














                                                                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.






                                                                share|improve this answer




























                                                                  5












                                                                  5








                                                                  5







                                                                  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.






                                                                  share|improve this answer















                                                                  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.







                                                                  share|improve this answer














                                                                  share|improve this answer



                                                                  share|improve this answer








                                                                  edited Jan 4 '18 at 5:49









                                                                  cit

                                                                  77641836




                                                                  77641836










                                                                  answered Jan 3 '14 at 10:39









                                                                  Darshan KunjadiyaDarshan Kunjadiya

                                                                  3,05112230




                                                                  3,05112230























                                                                      4














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





                                                                      share|improve this answer






























                                                                        4














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





                                                                        share|improve this answer




























                                                                          4












                                                                          4








                                                                          4







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





                                                                          share|improve this answer















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






                                                                          share|improve this answer














                                                                          share|improve this answer



                                                                          share|improve this answer








                                                                          answered Oct 22 '12 at 13:20


























                                                                          community wiki





                                                                          NANNAV
























                                                                              4














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





                                                                              share|improve this answer




























                                                                                4














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





                                                                                share|improve this answer


























                                                                                  4












                                                                                  4








                                                                                  4







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





                                                                                  share|improve this answer













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






                                                                                  share|improve this answer












                                                                                  share|improve this answer



                                                                                  share|improve this answer










                                                                                  answered May 8 '13 at 13:14









                                                                                  MutaweMutawe

                                                                                  5,40334081




                                                                                  5,40334081























                                                                                      3














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





                                                                                      share|improve this answer




























                                                                                        3














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





                                                                                        share|improve this answer


























                                                                                          3












                                                                                          3








                                                                                          3







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





                                                                                          share|improve this answer













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






                                                                                          share|improve this answer












                                                                                          share|improve this answer



                                                                                          share|improve this answer










                                                                                          answered May 4 '13 at 14:38









                                                                                          divyam shukladivyam shukla

                                                                                          772




                                                                                          772























                                                                                              3














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





                                                                                              share|improve this answer




























                                                                                                3














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





                                                                                                share|improve this answer


























                                                                                                  3












                                                                                                  3








                                                                                                  3







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





                                                                                                  share|improve this answer













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






                                                                                                  share|improve this answer












                                                                                                  share|improve this answer



                                                                                                  share|improve this answer










                                                                                                  answered Oct 7 '13 at 7:03









                                                                                                  Rob O.Rob O.

                                                                                                  312




                                                                                                  312























                                                                                                      3














                                                                                                      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");
                                                                                                      }





                                                                                                      share|improve this answer




























                                                                                                        3














                                                                                                        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");
                                                                                                        }





                                                                                                        share|improve this answer


























                                                                                                          3












                                                                                                          3








                                                                                                          3







                                                                                                          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");
                                                                                                          }





                                                                                                          share|improve this answer













                                                                                                          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");
                                                                                                          }






                                                                                                          share|improve this answer












                                                                                                          share|improve this answer



                                                                                                          share|improve this answer










                                                                                                          answered Feb 7 '14 at 12:56









                                                                                                          Vivek YadavVivek Yadav

                                                                                                          440416




                                                                                                          440416























                                                                                                              2














                                                                                                              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)





                                                                                                              share|improve this answer






























                                                                                                                2














                                                                                                                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)





                                                                                                                share|improve this answer




























                                                                                                                  2












                                                                                                                  2








                                                                                                                  2







                                                                                                                  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)





                                                                                                                  share|improve this answer















                                                                                                                  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)






                                                                                                                  share|improve this answer














                                                                                                                  share|improve this answer



                                                                                                                  share|improve this answer








                                                                                                                  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























                                                                                                                      2














                                                                                                                      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



                                                                                                                      enter image description here



                                                                                                                      enter image description here






                                                                                                                      share|improve this answer






























                                                                                                                        2














                                                                                                                        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



                                                                                                                        enter image description here



                                                                                                                        enter image description here






                                                                                                                        share|improve this answer




























                                                                                                                          2












                                                                                                                          2








                                                                                                                          2







                                                                                                                          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



                                                                                                                          enter image description here



                                                                                                                          enter image description here






                                                                                                                          share|improve this answer















                                                                                                                          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



                                                                                                                          enter image description here



                                                                                                                          enter image description here







                                                                                                                          share|improve this answer














                                                                                                                          share|improve this answer



                                                                                                                          share|improve this answer








                                                                                                                          edited Mar 25 '17 at 12:09

























                                                                                                                          answered Oct 23 '15 at 6:50









                                                                                                                          user3182143user3182143

                                                                                                                          7,85132130




                                                                                                                          7,85132130























                                                                                                                              1














                                                                                                                              -(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
                                                                                                                              }





                                                                                                                              share|improve this answer




























                                                                                                                                1














                                                                                                                                -(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
                                                                                                                                }





                                                                                                                                share|improve this answer


























                                                                                                                                  1












                                                                                                                                  1








                                                                                                                                  1







                                                                                                                                  -(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
                                                                                                                                  }





                                                                                                                                  share|improve this answer













                                                                                                                                  -(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
                                                                                                                                  }






                                                                                                                                  share|improve this answer












                                                                                                                                  share|improve this answer



                                                                                                                                  share|improve this answer










                                                                                                                                  answered Jun 3 '14 at 21:43









                                                                                                                                  TomG103TomG103

                                                                                                                                  135122




                                                                                                                                  135122























                                                                                                                                      1














                                                                                                                                      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
                                                                                                                                      }





                                                                                                                                      share|improve this answer




























                                                                                                                                        1














                                                                                                                                        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
                                                                                                                                        }





                                                                                                                                        share|improve this answer


























                                                                                                                                          1












                                                                                                                                          1








                                                                                                                                          1







                                                                                                                                          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
                                                                                                                                          }





                                                                                                                                          share|improve this answer













                                                                                                                                          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
                                                                                                                                          }






                                                                                                                                          share|improve this answer












                                                                                                                                          share|improve this answer



                                                                                                                                          share|improve this answer










                                                                                                                                          answered May 30 '16 at 13:55









                                                                                                                                          Pavle MijatovicPavle Mijatovic

                                                                                                                                          48256




                                                                                                                                          48256























                                                                                                                                              1














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





                                                                                                                                              share|improve this answer






























                                                                                                                                                1














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





                                                                                                                                                share|improve this answer




























                                                                                                                                                  1












                                                                                                                                                  1








                                                                                                                                                  1







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





                                                                                                                                                  share|improve this answer















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






                                                                                                                                                  share|improve this answer














                                                                                                                                                  share|improve this answer



                                                                                                                                                  share|improve this answer








                                                                                                                                                  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























                                                                                                                                                      1














                                                                                                                                                      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)





                                                                                                                                                      share|improve this answer




























                                                                                                                                                        1














                                                                                                                                                        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)





                                                                                                                                                        share|improve this answer


























                                                                                                                                                          1












                                                                                                                                                          1








                                                                                                                                                          1







                                                                                                                                                          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)





                                                                                                                                                          share|improve this answer













                                                                                                                                                          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)






                                                                                                                                                          share|improve this answer












                                                                                                                                                          share|improve this answer



                                                                                                                                                          share|improve this answer










                                                                                                                                                          answered Nov 14 '16 at 17:21









                                                                                                                                                          GJZGJZ

                                                                                                                                                          1,47721432




                                                                                                                                                          1,47721432























                                                                                                                                                              0














                                                                                                                                                              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.






                                                                                                                                                              share|improve this answer




























                                                                                                                                                                0














                                                                                                                                                                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.






                                                                                                                                                                share|improve this answer


























                                                                                                                                                                  0












                                                                                                                                                                  0








                                                                                                                                                                  0







                                                                                                                                                                  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.






                                                                                                                                                                  share|improve this answer













                                                                                                                                                                  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.







                                                                                                                                                                  share|improve this answer












                                                                                                                                                                  share|improve this answer



                                                                                                                                                                  share|improve this answer










                                                                                                                                                                  answered Apr 27 '13 at 10:10









                                                                                                                                                                  Chirag PipaliyaChirag Pipaliya

                                                                                                                                                                  1,1561020




                                                                                                                                                                  1,1561020























                                                                                                                                                                      0














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





                                                                                                                                                                      share|improve this answer




























                                                                                                                                                                        0














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





                                                                                                                                                                        share|improve this answer


























                                                                                                                                                                          0












                                                                                                                                                                          0








                                                                                                                                                                          0







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





                                                                                                                                                                          share|improve this answer













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






                                                                                                                                                                          share|improve this answer












                                                                                                                                                                          share|improve this answer



                                                                                                                                                                          share|improve this answer










                                                                                                                                                                          answered Dec 31 '13 at 10:58









                                                                                                                                                                          JCP ChandranJCP Chandran

                                                                                                                                                                          92




                                                                                                                                                                          92























                                                                                                                                                                              0














                                                                                                                                                                              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) {

                                                                                                                                                                              }





                                                                                                                                                                              share|improve this answer




























                                                                                                                                                                                0














                                                                                                                                                                                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) {

                                                                                                                                                                                }





                                                                                                                                                                                share|improve this answer


























                                                                                                                                                                                  0












                                                                                                                                                                                  0








                                                                                                                                                                                  0







                                                                                                                                                                                  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) {

                                                                                                                                                                                  }





                                                                                                                                                                                  share|improve this answer













                                                                                                                                                                                  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) {

                                                                                                                                                                                  }






                                                                                                                                                                                  share|improve this answer












                                                                                                                                                                                  share|improve this answer



                                                                                                                                                                                  share|improve this answer










                                                                                                                                                                                  answered Sep 4 '16 at 16:25









                                                                                                                                                                                  pedrouanpedrouan

                                                                                                                                                                                  9,40924261




                                                                                                                                                                                  9,40924261























                                                                                                                                                                                      0














                                                                                                                                                                                      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

                                                                                                                                                                                      }()





                                                                                                                                                                                      share|improve this answer




























                                                                                                                                                                                        0














                                                                                                                                                                                        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

                                                                                                                                                                                        }()





                                                                                                                                                                                        share|improve this answer


























                                                                                                                                                                                          0












                                                                                                                                                                                          0








                                                                                                                                                                                          0







                                                                                                                                                                                          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

                                                                                                                                                                                          }()





                                                                                                                                                                                          share|improve this answer













                                                                                                                                                                                          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

                                                                                                                                                                                          }()






                                                                                                                                                                                          share|improve this answer












                                                                                                                                                                                          share|improve this answer



                                                                                                                                                                                          share|improve this answer










                                                                                                                                                                                          answered Dec 29 '16 at 11:29









                                                                                                                                                                                          JackyJacky

                                                                                                                                                                                          2,62742943




                                                                                                                                                                                          2,62742943























                                                                                                                                                                                              0














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





                                                                                                                                                                                              share|improve this answer






























                                                                                                                                                                                                0














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





                                                                                                                                                                                                share|improve this answer




























                                                                                                                                                                                                  0












                                                                                                                                                                                                  0








                                                                                                                                                                                                  0







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





                                                                                                                                                                                                  share|improve this answer















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






                                                                                                                                                                                                  share|improve this answer














                                                                                                                                                                                                  share|improve this answer



                                                                                                                                                                                                  share|improve this answer








                                                                                                                                                                                                  edited Apr 17 '18 at 11:03









                                                                                                                                                                                                  byJeevan

                                                                                                                                                                                                  2,36012244




                                                                                                                                                                                                  2,36012244










                                                                                                                                                                                                  answered Jun 9 '15 at 13:47









                                                                                                                                                                                                  Nilesh ParmarNilesh Parmar

                                                                                                                                                                                                  354411




                                                                                                                                                                                                  354411























                                                                                                                                                                                                      0














                                                                                                                                                                                                      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
                                                                                                                                                                                                      }
                                                                                                                                                                                                      }





                                                                                                                                                                                                      share|improve this answer




























                                                                                                                                                                                                        0














                                                                                                                                                                                                        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
                                                                                                                                                                                                        }
                                                                                                                                                                                                        }





                                                                                                                                                                                                        share|improve this answer


























                                                                                                                                                                                                          0












                                                                                                                                                                                                          0








                                                                                                                                                                                                          0







                                                                                                                                                                                                          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
                                                                                                                                                                                                          }
                                                                                                                                                                                                          }





                                                                                                                                                                                                          share|improve this answer













                                                                                                                                                                                                          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
                                                                                                                                                                                                          }
                                                                                                                                                                                                          }






                                                                                                                                                                                                          share|improve this answer












                                                                                                                                                                                                          share|improve this answer



                                                                                                                                                                                                          share|improve this answer










                                                                                                                                                                                                          answered Jan 20 at 6:35









                                                                                                                                                                                                          iOSiOS

                                                                                                                                                                                                          2,37711942




                                                                                                                                                                                                          2,37711942























                                                                                                                                                                                                              -1














                                                                                                                                                                                                              UIButton *btnname = [UIButton buttonWithType:UIButtonTypeRoundedRect];

                                                                                                                                                                                                              [btnname setTitle:@"Click Me" forState:UIControlStateNormal];

                                                                                                                                                                                                              btnname.frame = CGRectMake(10, 10, 100, 140);

                                                                                                                                                                                                              [self.view addSubview:btnname];





                                                                                                                                                                                                              share|improve this answer






























                                                                                                                                                                                                                -1














                                                                                                                                                                                                                UIButton *btnname = [UIButton buttonWithType:UIButtonTypeRoundedRect];

                                                                                                                                                                                                                [btnname setTitle:@"Click Me" forState:UIControlStateNormal];

                                                                                                                                                                                                                btnname.frame = CGRectMake(10, 10, 100, 140);

                                                                                                                                                                                                                [self.view addSubview:btnname];





                                                                                                                                                                                                                share|improve this answer




























                                                                                                                                                                                                                  -1












                                                                                                                                                                                                                  -1








                                                                                                                                                                                                                  -1







                                                                                                                                                                                                                  UIButton *btnname = [UIButton buttonWithType:UIButtonTypeRoundedRect];

                                                                                                                                                                                                                  [btnname setTitle:@"Click Me" forState:UIControlStateNormal];

                                                                                                                                                                                                                  btnname.frame = CGRectMake(10, 10, 100, 140);

                                                                                                                                                                                                                  [self.view addSubview:btnname];





                                                                                                                                                                                                                  share|improve this answer















                                                                                                                                                                                                                  UIButton *btnname = [UIButton buttonWithType:UIButtonTypeRoundedRect];

                                                                                                                                                                                                                  [btnname setTitle:@"Click Me" forState:UIControlStateNormal];

                                                                                                                                                                                                                  btnname.frame = CGRectMake(10, 10, 100, 140);

                                                                                                                                                                                                                  [self.view addSubview:btnname];






                                                                                                                                                                                                                  share|improve this answer














                                                                                                                                                                                                                  share|improve this answer



                                                                                                                                                                                                                  share|improve this answer








                                                                                                                                                                                                                  edited Jan 15 '14 at 10:50









                                                                                                                                                                                                                  Muddu Patil

                                                                                                                                                                                                                  54211023




                                                                                                                                                                                                                  54211023










                                                                                                                                                                                                                  answered Nov 20 '13 at 7:33









                                                                                                                                                                                                                  HRNHRN

                                                                                                                                                                                                                  1067




                                                                                                                                                                                                                  1067























                                                                                                                                                                                                                      -1














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





                                                                                                                                                                                                                      share|improve this answer




























                                                                                                                                                                                                                        -1














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





                                                                                                                                                                                                                        share|improve this answer


























                                                                                                                                                                                                                          -1












                                                                                                                                                                                                                          -1








                                                                                                                                                                                                                          -1







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





                                                                                                                                                                                                                          share|improve this answer













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






                                                                                                                                                                                                                          share|improve this answer












                                                                                                                                                                                                                          share|improve this answer



                                                                                                                                                                                                                          share|improve this answer










                                                                                                                                                                                                                          answered Mar 1 '14 at 5:25









                                                                                                                                                                                                                          MohitMohit

                                                                                                                                                                                                                          2,8332130




                                                                                                                                                                                                                          2,8332130






















                                                                                                                                                                                                                              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?



                                                                                                                                                                                                                              Popular posts from this blog

                                                                                                                                                                                                                              Liquibase includeAll doesn't find base path

                                                                                                                                                                                                                              How to use setInterval in EJS file?

                                                                                                                                                                                                                              Petrus Granier-Deferre