2016-12-14 55 views
0

我正在使用UISwitch,我想在圖像上設置並關閉故事板中的UISwitch圖像,但圖像未設置在開關上。如何在storyboard中爲UISwitch設置On Image和Off Image?

我試試這個: enter image description here

我想創建這樣的:

enter image description here

不喜歡這樣--->

enter image description here

在此先感謝。

+1

如果故事板沒有完美工作,然後嘗試設置圖像編碼部分。 –

+0

你能否詳細說明它不工作? – Desdenova

+0

@Desdenova請參閱屏幕截圖,我已經在故事板上設置圖像,但在工作空間區域沒有任何工作 –

回答

0

您可以編程方式檢測開關的開/關狀態並設置圖像。

[YourSwitch addTarget:self action:@selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside]; 

    - (void) switchToggled:(id)sender { 
     UISwitch *mySwitch = (UISwitch *)sender; 
     if ([mySwitch isOn]) { 
      [switchControl setThumbTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Thumb.png"]]]; 
     } else { 
      [switchControl setThumbTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Thumb.png"]]]; 
     } 
    } 
+0

此答案對我而言不適用。此代碼是更改縮略圖圖像 –

0

你可以像你使用UISwitch 到位UISwitch的使用的UIButton爲有用的,也節省了時間和易於定製。對於UIButton的

使用圖像切片,並將其設置爲像背景圖片這

[cell.btnSwitch setBackgroundImage:[UIImage imageNamed:@"switchOn"] forState:UIControlStateNormal]; 
[cell.btnSwitch setBackgroundImage:[UIImage imageNamed:@"switchOff"] forState:UIControlStateNormal];