How do I create a basic UIButton programmatically?

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

    Objective-C

    // Create the Button with RoundedRect type
    UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    // instend of "Click Me" you can write your own message/Label
    [mybutton setTitle:@"Click Me" forState:UIControlStateNormal];
    // create the Rectangle Frame with specified size
    mybutton.frame = CGRectMake(10, 10, 300, 140); // x,y,width,height    [self.view addSubview:mybutton];// add button to your view.
    

    Swift

    let button   = UIButton(type: UIButtonType.System) as UIButton
    button.frame = CGRectMake(100, 100, 100, 50)
    button.backgroundColor = UIColor.greenColor()
    button.setTitle("Test Button", forState: UIControlState.Normal)
    self.view.addSubview(button)
    

提交回复
热议问题