How do I create a basic UIButton programmatically?

后端 未结 30 1328
清歌不尽
清歌不尽 2020-11-22 14:41

How can I create a basic UIButton programmatically? For example in my view controller, when executing the viewDidLoad method, three UIButton<

30条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 15:19

    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.

提交回复
热议问题