2012-11-22 73 views
64

enter image description here在Xcode中添加圖像到UITextField?

我需要在Xcode中實現dropdown的概念。爲此,我使用的是UIPickerview

這個pickerView會在點擊textfield(在textFieldDidBeginEditing:)事件時加載。

問:

有沒有一種方法,我可以將圖像添加到TextField

圖像就像是一個箭頭標記,用戶可以通過該標記了解dropdowntextfield被點擊時出現。我可以如何製作它?

+0

您可以添加一個按鈕(與您的箭頭圖片)旁邊的文本框,打開pickerview。 –

+0

我可以通過編程方式向textfield添加像imageview之類的東西嗎? – Honey

+1

如果您不希望用戶編輯文本字段,而不是文本字段,則可以使用按鈕。 –

回答

144

的UITextField有rightView屬性,如果你有像喜歡這個 - enter image description here然後你可以伊斯利設置的ImageView對象rightView:

UITextField *myTextField = [[UITextField alloc] init]; 

myTextField.rightViewMode = UITextFieldViewModeAlways; 
myTextField.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"downArrow.png"]];
+0

@arizah plz check updated anwser。 –

+0

邊境PLZ通過這個博客http://www.icodeblog.com/2010/01/04/uitextfield-a-complete-api-overview/ –

+7

如果我在textField左側添加圖像。我們如何管理圖像和文字之間的間距。 –

2

的UITextField具有以下特性:

@property(nonatomic, retain) UIImage *background 

例如:

UITextField *myTextField = [[UITextField alloc] init]; 
myTextField.background = [UIImage imageNamed:@"myImage.png"]; 
+0

亞,但我應該如何鏈接這個uiimage到文本框? – Honey

+0

請參閱編輯答案 –

3

嘿隊友你想下拉列表視圖,然後看到這個定製下拉觀點..

  1. http://code.google.com/p/dropdowndemo/downloads/list
  2. http://ameyashetti.wordpress.com/2010/09/26/drop-down-demo/

,爲此需要使用此代碼

UITextField *txtstate =[[UITextField alloc]init]; [txtstate setFrame:CGRectMake(10, 30,170, 30)]; 
txtstate.delegate=self; 

[email protected]"Fruits"; 

txtstate.borderStyle = UITextBorderStyleLine; 

txtstate.background = [UIImage imageNamed:@"dropdownbtn.png"]; 

[txtstate setAutocorrectionType:UITextAutocorrectionTypeNo]; 
[self.view addSubview:txtstate]; 

並設置文本框的邊框樣式沒有..

+0

txtstate = [[UITextField alloc] init]; [txtstate setFrame:CGRectMake(10,30,170,30)]; txtstate.delegate = self; txtstate.text = @「Fruits」; txtstate.borderStyle = UITextBorderStyleNone; txtstate.background = [UIImage imageNamed:@「Arrow-Down-black-32.png」]; [txtstate setAutocorrectionType:UITextAutocorrectionTypeNo]; [self.view addSubview:txtstate]; 這樣做,但我也需要邊界..我可以怎麼做? – Honey

+0

你使用mate?意味着你使用這個自定義下拉列表? –

+0

@arizah看到我編輯的答案,你只需要設置邊界與線路夥計... –

30

你可以把UIImageView到您的UITextField的左側。

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(96, 40, 130, 20)]; 
[textField setLeftViewMode:UITextFieldViewModeAlways]; 
textField.leftView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"searchIccon.png"]]; 
+1

謝謝Erhan,IMO你的解決方案是最好的和最短的!它幫助了我很多:) –

+0

歡迎Davit :)。真的,我欣賞聽到你好聲音:) –

35

在斯威夫特:

textField.leftViewMode = UITextFieldViewMode.Always 
textField.leftView = UIImageView(image: UIImage(named: "imageName")) 
+0

優雅的解決方案!,但我也有一些麻煩,爲我的uitexfield(不是左視圖,但整個背景)設置背景圖像,你能告訴我怎麼樣,在迅速。 – Bhimbim

+0

在圖像上設置正確的邊距/填充,首先創建一個尺寸比圖像大一點的視圖。並創建圖像視圖,居中在UIView中,將UIImageView添加到UIView。現在最後一步是將此UIView設置爲.leftView。 – kishorer747

5

要添加圖像和文本字段之間適當的利潤率/墊襯,試試下面這段代碼。 擴展@ King-Wizard的答案。

這裏我的TextField的高度是44 檢查附加的圖像以供參考。

斯威夫特版本:

emailTF.leftViewMode = .Always 
let emailImgContainer = UIView(frame: CGRectMake(emailTF.frame.origin.x, emailTF.frame.origin.y, 40.0, 30.0)) 
let emailImView = UIImageView(frame: CGRectMake(0, 0, 25.0, 25.0)) 
emailImView.image = UIImage(named: "image1") 
emailImView.center = emailImgContainer.center 
emailImgContainer.addSubview(emailImView) 
emailTF.leftView = emailImgContainer 

enter image description here

1

第1步:這個類添加到您的項目,並把它添加到您的textFiled的身份檢查類,

class MyCustomTextField: UITextField { 

    @IBInspectable var inset: CGFloat = 0 

    @IBInspectable var leftImage: String = String(){ 
     didSet{ 
      leftViewMode = UITextFieldViewMode.Always 
      leftView = UIImageView(image: UIImage(named: leftImage)) 
     } 
    } 

    override func textRectForBounds(bounds: CGRect) -> CGRect { 
     return CGRectInset(bounds, inset, inset) 
    } 

    override func editingRectForBounds(bounds: CGRect) -> CGRect { 
     return textRectForBounds(bounds) 
    } 

    override func leftViewRectForBounds(bounds: CGRect) -> CGRect { 
     return CGRectInset(CGRectMake(0, 2, 40, 40), 10, 10) //Change frame according to your needs 
    } 
} 

步驟2:從界面構建器設置文本插圖和左側圖像。

enter image description here

第3步:享受。

+0

您可以製作UIImage類型的leftImage,它將爲您提供一個很好的直接圖像輸入。 –

8

雨燕3.0

txtField.rightViewMode = .always 
txtField.rightView = UIImageView(image: UIImage(named: "selectdrop")) 

TextField With DropDown Icon