How do I create a basic UIButton programmatically?

后端 未结 30 899
清歌不尽
清歌不尽 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:06
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self addMyButton];   // Call add button method on view load
    }
    
    - (void)addMyButton{    // Method for creating button, with background image and other properties
    
        UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
        playButton.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
        [playButton setTitle:@"Play" forState:UIControlStateNormal];
        playButton.backgroundColor = [UIColor clearColor];
        [playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
        UIImage *buttonImageNormal = [UIImage imageNamed:@"blueButton.png"];
        UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
        [playButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
        UIImage *buttonImagePressed = [UIImage imageNamed:@"whiteButton.png"];
        UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
        [playButton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
        [playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:playButton];  
    }
    
    0 讨论(0)
  • 2020-11-22 15:08

    Check out this code:

    Swift 4.2

    let frameimg = CGRect(x: 15, y: 46, width: 55, height: 70)
    let btnTest = UIButton(type: .roundedRect)
    btnTest.frame = frameimg
    btnTest.tag = 11
    btnTest.setTitle("Test Button", for: .normal)
    btnTest.addTarget(self, action: #selector(self.buttonAction(sender:)), for: .touchUpInside)
    btnTest.titleLabel?.font = UIFont.boldSystemFont(ofSize: 12.0)
    btnTest.titleLabel?.lineBreakMode = .byWordWrapping
    btnTest.titleLabel?.numberOfLines = 2
    btnTest.titleLabel?.textAlignment = .center
    btnTest.setTitleColor(UIColor.gray, for: .normal)
    btnTest.setTitleColor(UIColor.blue, for: .selected)
    btnTest.showsTouchWhenHighlighted = true
    view.addSubview(btnTest)
    

    Objective C

    CGRect frameimg = CGRectMake(15, 46, 55,70);
    UIButton  *SelectionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    SelectionButton.frame=frameimg;
    SelectionButton.tag=i;
    [SelectionButton setTitle:[SelectionArray objectAtIndex:0] forState:UIControlStateNormal];
    [SelectionButton addTarget:self action:@selector(BtnSelected:)
                      forControlEvents:UIControlEventTouchUpInside];
    [SelectionButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12.0]];
    SelectionButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    SelectionButton.titleLabel.numberOfLines = 2;
    SelectionButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [SelectionButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    [SelectionButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    [SelectionButton setShowsTouchWhenHighlighted:YES];
    [self.view addSubview:SelectionButton];
    

    I hope you this code works for you.

    0 讨论(0)
  • 2020-11-22 15:08
    UIButton *buttonName = [UIButton
    buttonWithType:UIButtonTypeRoundedRect];
    [buttonName addTarget:self 
        action:@selector(aMethod:)forControlEvents:UIControlEventTouchDown];
    [buttonName setTitle:@"Show View" forState:UIControlStateNormal];
    .frame = CGRectMake(80.0, 210.0, 160.0, 40.0); [view
    addSubview:buttonName];
    
    0 讨论(0)
  • 2020-11-22 15:08

    In Swift 5 and Xcode 10.2

    Basically we have two types of buttons.

    1) System type button

    2) Custom type button (In custom type button we can set background image for button)

    And these two types of buttons has few control states https://developer.apple.com/documentation/uikit/uicontrol/state

    Important states are

    1) Normal state

    2) Selected state

    3) Highlighted state

    4) Disabled state etc...

    //For system type button
    let button = UIButton(type: .system)
    button.frame = CGRect(x: 100, y: 250, width: 100, height: 50)
    //  button.backgroundColor = .blue
    button.setTitle("Button", for: .normal)
    button.setTitleColor(.white, for: .normal)
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 13.0)
    button.titleLabel?.textAlignment = .center//Text alighment center
    button.titleLabel?.numberOfLines = 0//To display multiple lines in UIButton
    button.titleLabel?.lineBreakMode = .byWordWrapping//By word wrapping
    button.tag = 1//To assign tag value
    button.btnProperties()//Call UIButton properties from extension function
    button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
    self.view.addSubview(button)
    
    //For custom type button (add image to your button)
    let button2 = UIButton(type: .custom)
    button2.frame = CGRect(x: 100, y: 400, width: 100, height: 50)
    //        button2.backgroundColor = .blue
    button2.setImage(UIImage.init(named: "img.png"), for: .normal)
    button2.tag = 2
    button2.btnProperties()//Call UIButton properties from extension function
    button2.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
    self.view.addSubview(button2)
    
    @objc func buttonClicked(sender:UIButton) {
        print("Button \(sender.tag) clicked")
    }
    
    //You can add UIButton properties using extension
    extension UIButton {
        func btnProperties() {
            layer.cornerRadius = 10//Set button corner radious
            clipsToBounds = true
            backgroundColor = .blue//Set background colour
            //titleLabel?.textAlignment = .center//add properties like this
        }
    }
    
    0 讨论(0)
  • 2020-11-22 15:09

    Try it....

    UIButton *finalPriceBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    finalPriceBtn.frame=CGRectMake(260, 25, 45, 15);
    [finalPriceBtn addTarget:self action:@selector(goBtnClk:) forControlEvents:UIControlEventTouchUpInside];
    finalPriceBtn.titleLabel.font=[UIFont systemFontOfSize:12];
    [finalPriceBtn setTitle:[NSString stringWithFormat:@"$%.2f",tempVal] forState:UIControlStateNormal];
    finalPriceBtn.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f  alpha:1];
    finalPriceBtn.titleLabel.textAlignment=UITextAlignmentLeft;
    [imageView addSubview:finalPriceBtn];
    

    Hope i helped.

    0 讨论(0)
  • 2020-11-22 15:10
    -(void)addStuffToView
    {
        UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 20, 20)]; //(x, y, width, height of button on screen
        [aButton setTitle:@"Button" forState:UIControlStateNormal];//puts the text on the button
        aButton.titleLabel.font = somefont;//sets the font if one is already stated
        aButton.titleLabel.font = [UIFont fontWithName:@"Arial-MT" size:12];//sets the font type and size
        [aButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];//see back method below
        [aButton setBackgroundImage:[UIImage imageNamed:@"someImage.png"] forState:UIControlStateNormal];//sets the image of the button
        [self.view addSubview:back];
    }
    
    -(void)back
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle.....]
    }
    
    -(void)viewDidLoad
    {
        [super viewDidLoad];
        [self addStuffToView];//adds all items built in this method to the view
    }
    
    0 讨论(0)
提交回复
热议问题