iphone定制UIButtons

时间:2011-05-10 05:35:53

标签: iphone

如何自定义UIButton,使其显示除默认按钮样式以外的其他内容。

9 个答案:

答案 0 :(得分:2)

UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(10.0, 20.0, 50.0, 50.0); // You can change the x, y, width, height.
[btn setImage:[UIImage imageNamed:@"img.png"]  forState:UIControlStateNormal];
[self.view addSubView:btn];

答案 1 :(得分:1)

您可以这样做:

UIImage *btnImage = [UIImage imageNamed:@"images-01.png"];

UIButton *btnGo = [UIButton buttonWithType:UIButtonTypeCustom];
btnGo.frame = CGRectMake(85,123, 100, 26);
btnGo.backgroundcolor = [UIColor clearColor];
btnGo. setImage:btnImage forState: UIcontrolStateNormal];
[btnGo.titleLabel setFont: [UIFont systemFontOfSize:14]];
btnGo addTarget: self action:@selector(btnGoClicked) forControlEvents: UIControlEventTouchUpInside];

答案 2 :(得分:1)

您可以使用此

UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame = CGRectMake(899, 5, 82, 55);
customButton.tag = 123; 
[customButton addTarget:self action:@selector(buttonToggleForHide) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:customButton];

并使用

制作事件
-(void)buttonToggleForHide{...}

答案 3 :(得分:0)

在界面构建中,只需将类型从圆角矩形更改为自定义,然后指定新的背景图像。

在代码中:

UIButton* b = [UIButton buttonWithType:UIButtonTypeCustom];

答案 4 :(得分:0)

UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];

答案 5 :(得分:0)

在其上设置背景图像,在界面构建器中设置Type = Custom。

答案 6 :(得分:0)

@shabbir首先选择按钮然后按cmd + 1将按钮类型更改为自定义 如果正确选择我的答案 另一种方式是

UIButton* b = [UIButton buttonWithType:UIButtonTypeCustom];

如果您想在按钮上调用图像,请使用此

UIButton *button = [[UIButton alloc]initWithImage:[UIImage imageNamed:@"button.png"]];
[button release];

答案 7 :(得分:0)

你的意思是显示自定义图像的按钮吗?你可以试试这个:

UIButton *button = [[UIButton alloc]initWithImage:[UIImage imageNamed:@"button.png"]];
[button release];

答案 8 :(得分:0)

UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame = urRequiredFrame;
[button setImage:yourCustomImage forState:UIControlStateNormal];
button addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];

[self.view addSubView:button];