如何在不使用“界面”构建器的情况下创建简单按钮?
答案 0 :(得分:2)
你需要声明一个像这样的UIButton:
UIButton *newButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
[newButton setTitle:@"Some Button" forState:UIControlStateNormal];
然后,您可以将按钮添加到视图中 -
[self.view addSubview: newButton]; // (could be another view other then self.view)
最后,如果你想在按下该按钮时发生一些动作,你可以添加:
[newButton addTarget:self action:@selector(doSomething)
forControlEvents:UIControlEventTouchUpInside];
当然要宣布该功能:
- (void) doSomething
{
NSLog(@"Button pressed");
}
当然不要忘记释放按钮。
答案 1 :(得分:1)
自定义按钮 -
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 30.0f)];
system buttom -
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
答案 2 :(得分:1)
尝试使用以下
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect];
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];