How do I create a basic UIButton programmatically?

后端 未结 30 1208
清歌不尽
清歌不尽 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条回答
  •  旧时难觅i
    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];  
    }
    

提交回复
热议问题