2014-06-27 67 views
0

我有一個UITextFiled添加到視圖在Xcode 5.1中,我試圖使用下面的代碼將圖像添加到其左視圖,但是當我運行所有我沒有看到圖像時,不明白其中的道理,請大家幫忙無法將圖像添加到UITextField的左視圖ios7

UIImageView *imgView = [[UIImageView alloc] init]; 
imgView.image = [UIImage imageNamed:@"Key.png"]; 

[txtCode setLeftViewMode:UITextFieldViewModeAlways]; 
[txtCode setLeftView:imgView]; 
+3

你嘗試着爲imageview設置一個框架嗎? – DareDevil

+0

'[UIImage imageNamed:@「Key.png」]'是否返回一個對象?還是返回零? – spassas

回答

1

設置圖像視圖幀:

yourTextField.borderStyle = UITextBorderStyleNone; 

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)]; 
imgView.image = [UIImage imageNamed:@"Key.png"]; 

[yourTextField setLeftViewMode:UITextFieldViewModeAlways]; 
[yourTextField setLeftView:imgView]; 
+0

感謝它的工作 –

1

希望這將幫助你...增加圖像中paddingView如果你想。

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)]; 
self.textFiled.leftView = paddingView; 
self.textFiled.leftViewMode = UITextFieldViewModeAlways;