Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a basic UIButton programmatically?

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

like image 966
domlao Avatar asked Sep 04 '09 11:09

domlao


People also ask

How do I code a button in Swift?

swift in your assistant editor. You can do this by holding Option on your keyboard and clicking the ViewController. swift file in your Project Navigator. Now click on the button, hold CTRL and then drag it to your Swift file.


1 Answers

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]; 
like image 84
mahboudz Avatar answered Oct 29 '22 11:10

mahboudz