Codesnipp.it Social Code Sharing

Scott Rapson

Create UIButton pragmatically with options

by Scott Rapson on Feb 18, 2011

//In header file //in the @interface {} IBOutlet UIButton *submitButton; //under the @interface {} @property(nonatomic, retain) IBOutlet UIButton *submitButton; //in the method file //add the button pragmatically myButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; //the button type myButton.frame = CGRectMake(100.0, 320.0, 120.0, 40.0); //size/shape [myButton setTitle:@"Title" forState:UIControlStateNormal]; //the text on the button [myButton addTarget:self action:@selector(submit:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:myButton]; [myButton release]; //other options. I have submitButton here... rename at will /* submitButton.backgroundColor = [UIColor clearColor]; //the background colour of the button [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ]; //colour of text UIImage *buttonImageNormal = [UIImage imageNamed:@"blueButton.png"]; UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0]; [submitButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal]; UIImage *buttonImagePressed = [UIImage imageNamed:@"whiteButton.png"]; UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0]; [submitButton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted]; */

Can't see the comments? Please login first :)