How do I create a basic UIButton programmatically?

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

提交回复
热议问题