一個UIButton啓動它想要顯示的應用程序「workout_heart.png」。編寫該按鈕的按鈕點擊動作。當用戶點擊那個圖片想要改變「workout_heart_red.png」的按鈕時。再次點擊該按鈕意味着圖像變成「workout_heart.png」。它想要改變交替。我怎樣才能實現這個幫助我!如何更改UIButton中的UIButton背景圖像點擊ios
回答
嘗試這個代碼
對於按鈕設置背景圖像等來選擇和正常狀態低於
[<btn> setBackgroundImage:[UIImage imageNamed:@"workout_heart.png"] forState:UIControlStateNormal];
[<btn> setBackgroundImage:[UIImage imageNamed:@"workout_heart_red.png"] forState:UIControlStateSelected];
和寫入操作邏輯如下默認在廈門國際銀行如「workout_heart.png」
- (IBAction)buttonTapped:(id)sender {
UIButton *tempButton = (UIButton *)sender;
if(tempButton.isSelected){
[tempButton setSelected:NO];
} else {
[tempButton setSelected:YES];
}
}
它不在桌面視圖中的一個按鈕在視圖控制器 – 2014-11-25 12:09:11
這裏tableview來了 – 2014-11-25 12:09:58
你試過這段代碼。 – 2014-11-25 12:15:26
嘗試此
[btn setBackgroundImage:[UIImage imageWithString:@"workout_heart.png" forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageWithString:@"workout_heart_red.png" forState:UIControlStateHighlighted];
嘗試此,聲明SATE作爲實例變量
- (IBAction)buttonTapped:(id)sender {
if (state == 0) {
state = 1;
[btn setBackgroundImage:[UIImage imageNamed:@"workout_heart_red.png"] forState:UIControlStateHighlighted];
}
else if (state == 1){
state = 0;
[btn setBackgroundImage:[UIImage imageNamed:@"workout_heart.png"] forState:UIControlStateNormal];
}
}
- (IBAction)btn_MyButton_Click:(id)sender
{
if ([sender isSelected] == YES)
{
[sender setSelected:NO];
[btn setBackgroundImage:[UIImage imageNamed:@"workout_heart.png"] forState:UIControlStateNormal];
}
else
{
[sender setSelected:YES];
[btn setBackgroundImage:[UIImage imageNamed:@"workout_heart_red.png"] forState:UIControlStateNormal];
}
}
imageWithString錯誤,setBackgroundImage – 2014-11-25 12:13:27
什麼是'[UIImage imageWithString:'? – Popeye 2014-11-25 12:15:19
編輯..試試這個1 – 2014-11-25 12:15:24
- (void)viewDidLoad {
[super viewDidLoad];
// ...
self.btn1ImageName = @"workout_heart.png";
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(367, 117, 97, 25)];
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(toggleImage:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
}
- (void)toggleImage:(UIButton *)btn1 {
self.btn1ImageName = ([self.btn1ImageName isEqualToString:@"workout_heart.png"]) ? @"workout_heart_red.png" : @"workout_heart.png";
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
}
設置的背景圖像,然後在檢查狀態即
if(button.selected)
{
[button setBackgroundImage:[UIImage imageNamed:"workout_heart_red.png"] forState: UIControlStateNormal];
}
else
{
[button setBackgroundImage:[UIImage imageNamed:"workout_heart.png"] forState: UIControlStateNormal];
}
- 1. UIButton背景圖像點擊更改
- 2. UIButton背景更改點擊
- 3. 如何更改UIButton的背景圖像?
- 4. 與更改UIButton背景圖像問題
- 5. 隨機更改UIButton背景圖像
- 6. 單擊更改UIbutton圖像
- 7. 多次點擊時更改UIButton圖像
- 8. UIButton的圖像和背景
- 9. UIButton背景圖像拉伸Xcode iOS
- 10. 更改UIButton背景顏色
- 11. 動畫UIButton背景圖像
- 12. UIButton背景圖像不變
- 13. 圖像和背景圖像的UIButton
- 14. UIButton背景IOS7中的圖像
- 15. UIButton圖像/背景圖像古怪
- 16. 如何更改UIButton backgroundimage,同時點擊相同的UIbutton?
- 17. 如何在點擊後三次更改uibutton圖像
- 18. 更改UIButton IPhone的背景顏色
- 19. 以編程方式更改UIButton上的背景圖像
- 20. 訪問UIButton的背景圖像的UIImageView
- 21. UIButton的背景圖像按鈕
- 22. 獲取UIButton的背景圖像
- 23. 如何在Interface Builder中更改UIButton的背景圖像並保留其形狀?
- 24. UIButton背景圖像以編程方式更改
- 25. 如何釋放UIButton的背景圖片?
- 26. 點擊時更改背景圖像 - Android
- 27. UIButton背景圖像製作器?
- 28. 爲UIButton設置一個背景圖像
- 29. UIButton禁用並啓用背景圖像
- 30. UIButton背景圖像未從Singleton字典
請繼續通過現有的問題和更好的文檔。看[這裏](http://stackoverflow.com/questions/1702194/how-to-use-a-uibutton-as-a-toggle-switch)。您只需在您的按鈕的目標方法中切換圖像。 – n00bProgrammer 2014-11-25 11:55:29
@ n00bProgrammer如果你認爲它是重複的,那麼它就不是非常有建設性的,只要把它標記爲一個就可以了。我確定如果我們在新的時候看到你的問題,我們會發現你會提出一些不完全適合該網站的問題(例如http://stackoverflow.com/questions/16231048/new-to- ios-development-need-resources) – Popeye 2014-11-25 11:57:28
@Popeye,我確實把它標記爲重複的。我添加了評論,將其標記爲重複,然後繼續。另外,如果我聽起來粗魯,我很抱歉,不是我的意圖。 – n00bProgrammer 2014-11-25 12:01:47