Breaking News
Home / iOS / iOS Snippets / How to create a UIButton programatically in iOS?

How to create a UIButton programatically in iOS?

  1. UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  2.  
  3. button.frame = CGRectMake(100.0, 200.0, 180.0, 60.0);
  4.  
  5. // on click, which method to call
  6. [button addTarget:self      action:@selector(onClickMethodToCall:) forControlEvents:UIControlEventTouchDown];
  7.  
  8. // title of button
  9. [button setTitle:@"Click Me" forState:UIControlStateNormal];
  10.  
  11. // if has to have a image
  12. UIImage *image = [UIImage imageNamed:@"image.png"];
  13. [image setImage:img forState:UIControlStateNormal];
  14.  
  15. // finally add
  16. [view addSubview:button];
[pb_builder]

Check Also

EKCalendar – That account does not allow calendars to be added or removed

Usecase When I am trying to create a new Calendar programmatically using the EventKit API …

Advertisment ad adsense adlogger