开发者

How do I create a simple button without using the Interface builder?

开发者 https://www.devze.com 2023-02-28 01:14 出处:网络
How do I create a simple b开发者_高级运维utton without using the Interface builder?You would need to declare a UIButton like this:

How do I create a simple b开发者_高级运维utton without using the Interface builder?


You would need to declare a UIButton like this:

UIButton *newButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
[newButton setTitle:@"Some Button" forState:UIControlStateNormal];

You would then add the button to a view -

[self.view addSubview: newButton]; // (could be another view other then self.view)

And finally, if you want some action to occur when pressing that button, you would add:

[newButton  addTarget:self action:@selector(doSomething)
     forControlEvents:UIControlEventTouchUpInside];

and of course declare that function:

- (void) doSomething
{
       NSLog(@"Button pressed");
}

And of course don't forget to release the button.


Try with below

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];


like this-

custom button -

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 30.0f)];

system buttom -

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
0

精彩评论

暂无评论...
验证码 换一张
取 消