如何在iphone中以编程方式实现带有背景图像的按钮
Posted
技术标签:
【中文标题】如何在iphone中以编程方式实现带有背景图像的按钮【英文标题】:how to implement button with backgroundimage programmatically in iphone 【发布时间】:2011-02-16 03:40:20 【问题描述】:我正在 iphone 中以编程方式开发带有按钮的视图中的图像。现在我想为下一个和上一个按钮添加按钮背景图像。我想以编程方式为这些按钮添加图像。
如何在 iphone 中添加按钮图像。
【问题讨论】:
【参考方案1】:// 像这样添加按钮
UIImage *redButtonImage = [UIImage imageNamed:@"ExitButton.png"];
UIButton *redButton = [UIButton buttonWithType:UIButtonTypeCustom];
redButton.frame = CGRectMake(280.0, 10.0, 29.0, 29.0);
[redButton setBackgroundImage:redButtonImage forState:UIControlStateNormal];
[view addSubview:redButton];
【讨论】:
【参考方案2】:你可以试试这个
UIImage *buttonImage = [UIImage imageNamed:@"image.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//create a UIBarButtonItem with the button as a custom view
//UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[button addTarget:self action:@selector(clickActionItem:) forControlEvents:UIControlEventTouchUpInside];
祝你好运
【讨论】:
【参考方案3】:使用它来设置按钮中的图像
[yourButton setBackgroundImage:yourImage forState:UIControlStateNormal];
【讨论】:
【参考方案4】:试试这个,
UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
yourButton.frame = CGRectMake(0, 0, 80, 40);
[yourButton setBackgroundImage:[UIImage imageNamed:@"backgroungImage.png"] forState:UIControlStateNormal];
[self.view yourButton];
【讨论】:
以上是关于如何在iphone中以编程方式实现带有背景图像的按钮的主要内容,如果未能解决你的问题,请参考以下文章