[IOS] iphone开发之[美化按钮]

美化按钮

//添加一个button,美化按钮
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(40, 300, 100, 30);

UIImage *buttonImageNormal = [UIImage imageNamed:@"whiteButton.png"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
//[btn1 setImage:stretchableButtonImageNormal forState:UIControlStateNormal];
[btn1 setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal]; //set backgroundImage:

UIImage *buttonImagePressed = [UIImage imageNamed:@"blueButton.png"];
UIImage *stretchableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[btn1 setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];

[self.view addSubview:btn1];

解说一下:whiteButton.png    blueButton.png  图片在苹果官方网站提供的DEMO:UICatalog中可以找到。

你可能感兴趣的:(iPhone开发)