How do I create a basic UIButton programmatically?

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

    try this code to create a button and repeat it for 2 more times with different coordinates and the method(myButtonClick) is called when the button is pressed

    UIButton *editButton = [UIButton buttonWithType: UIButtonTypeCustom];
    editButton.frame = CGRectMake(0, 0, width, height);
    [editButton setBackgroundImage: editButtonImage forState: UIControlStateNormal];
    [myButton addTarget:self action:@selector(myButtonClick:)    forControlEvents:UIControlEventTouchUpInside]; 
    editButton.adjustsImageWhenHighlighted = YES;
    editButton.titleLabel.text = @"Edit";
    editButton.titleLabel.textColor = [UIColor whiteColor];
    editButton.titleLabel.textAlignment = UITextAlignmentCenter;
    editButton.titleLabel.font = [UIFont fontWithName: @"Helvetica" size: 14];
    [self.view addSubview: editButton];
    
    -(void) myButtonClick:(NSString *)myString{
    
       NSLog(@"you clicked on button %@", myString);
    }
    

提交回复
热议问题