How do I create a basic UIButton programmatically?

后端 未结 30 1386
清歌不尽
清歌不尽 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:25

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

提交回复
热议问题