2013-06-13 31 views
0

我在界面生成器中創建的視圖控制器中有大約30個UITextField(IBOutlet)。設置文本框數組的邊框顏色

現在我想設置所有這些UITextField的邊框顏色和邊框寬度。

所以下面是我正在嘗試的代碼,它沒有發生。

for (UIView *subView in self.view.subviews) { 
     if ([subView isKindOfClass:[UITextField class]]) { 

      [[subView layer] setBorderColor:[[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor]]; 
      subView.layer.borderWidth= 1.0f; 

     } 
} 

但是,如果我單獨做每個對象,改變是反映。

 [[textfieldOne layer] setBorderColor:[[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor]]; 
     textfieldOne.layer.borderWidth= 1.0f; 

我在哪裏做錯了?

+2

ABove代碼正在工作。我檢查你的代碼.... – Kalpesh

+0

你是對的,Ooops我的錯誤,self.view.subviews是這裏的主要罪魁禍首,在我的情況是不同的。感謝大家的迴應。 – user1227928

+0

你不能使用'appearance'屬性嗎?像'[[UITextField外觀] setBorderColor:[[UIColor redColor] CGColor]];' – Popeye

回答

2

你的代碼應該可以工作,但如果你對你的UIView進行了類型轉換(並不是說我會看到它會如何改變)。試試這個

for (UIView *subView in self.view.subviews) { 
     if ([subView isKindOfClass:[UITextField class]]) { 
      UITextField *aTextField = (UITextField *)subView; 
      [[aTextField layer] setBorderColor:[[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor]]; 
      aTextField.layer.borderWidth = 1.0f; 

     } 
} 

另外在旁邊注意;儘量不要將點符號與括號混合在一起,因爲它會使代碼非常不一致。選擇一個並堅持下去。

點標記

aTextField.layer.borderColor = [[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor]; 
aTextField.layer.borderWidth = 1.0f; 

支架

[[aTextField layer] setBorderColor:[[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor]]; 
[[aTextField layer] setBorderWidth:1.0f]; 
2
for (UITextField *subView in self.view.subviews) { 
     if ([subView isKindOfClass:[UITextField class]]) { 

      [[subView layer] setBorderColor:[[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor]]; 
      subView.layer.borderWidth= 1.0f; 

     } 
} 

與this.May這可以幫助你試試。