How do I create a basic UIButton programmatically?

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

    This is an example as well to create three buttons. Just move their location.

    UIImage *buttonOff = [UIImage imageNamed:@"crysBallNorm.png"];
    UIImage *buttonOn = [UIImage imageNamed:@"crysBallHigh.png"];
    
    UIButton *predictButton = [UIButton alloc];
    predictButton = [UIButton buttonWithType:UIButtonTypeCustom];
    predictButton.frame = CGRectMake(180.0, 510.0, 120.0, 30.0);
    [predictButton setBackgroundImage:buttonOff forState:UIControlStateNormal];
    [predictButton setBackgroundImage:buttonOn forState:UIControlStateHighlighted];
    [predictButton setTitle:@"Predict" forState:UIControlStateNormal];
    [predictButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
    [predictButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:predictButton];
    

提交回复
热议问题