How do I create a basic UIButton programmatically?

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

    For creating UIButton programmatically we can create in both objective c and swift

    SWIFT 3

    let buttonSwift   = UIButton(type: UIButtonType.system) as UIButton
                          //OR 
    let buttonSwift   = UIButton(type: UIButtonType.Custom) as UIButton
    //Set Frame for Button
    buttonSwift.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
    //Set title for button
    buttonSwift.setTitle("ClickMe", for: .normal)
    //If you want to set color for button title
    buttonSwift.setTitleColor(UIColor.white, for: .normal)
    //If you want to set Background color for button
    buttonSwift.backgroundColor = UIColor.black
    //If you want to set tag for button
    buttonSwift.tag = 0
    //If you want to add or set image for button
    let image = UIImage(named: "YourImageName") as UIImage?
    buttonSwift.setImage(image, for: .normal)
    //If you want to add or set Background image for button
    buttonSwift.setBackgroundImage(image, for: .normal)
    //Add action for button
    buttonSwift.addTarget(self, action: #selector(actionPressMe), for:.touchUpInside)
    //Add button as SubView to Super View
    self.view.addSubview(buttonSwift)
    

    UIButton Action Method

    func actionPressMe(sender: UIButton!) 
    {
        NSLog("Clicked button tag is %@", sender.tag)
                   OR
        print("Clicked button tag is \(sender.tag)")
    
        //Then do whatever you want to do here
    
        ........  
    }
    

    OBJECTIVE C

    UIButton *buttonObjectiveC = [UIButton buttonWithType:UIButtonTypeCustom];
               OR
    UIButton *buttonObjectiveC = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonObjectiveC.frame = CGRectMake(200, 100, 200, 100);
    //Set title for button
    [buttonObjectiveC setTitle:@"ClickMe" forState:UIControlStateNormal];
    //If you want to set color for button title
    [buttonObjectiveC setTitleColor:[UIColor  whiteColor] forState: UIControlStateNormal];
    //If you want to set Background color for button
    [buttonObjectiveC setBackgroundColor:[UIColor blackColor]];
    //If you want to set tag for button
    buttonSwift.tag = 0;
    //If you want to add or set image for button
    UIImage *image = [UIImage imageNamed:@"YourImageName"];
    [buttonObjectiveC setImage:image forState:UIControlStateNormal];
    //If you want to add or set Background image for button
    [buttonObjectiveC setBackgroundImage:image forState:UIControlStateNormal];
    //Add action for button
    [buttonObjectiveC addTarget:self action:@selector(actionPressMe:)forControlEvents:UIControlEventTouchUpInside];
    //Add button as SubView to Super View
    [self.view addSubview:buttonObjectiveC];
    

    UIButton Action Method

    - (void)actionPressMe:(UIButton *)sender 
    {
        NSLog(@"Clicked button tag is %@",sender.tag);
        //Then do whatever you want to do here
        ..........
    }
    

    Output Screenshot is

提交回复
热议问题