2015-09-12 114 views
6

使用夫特的UITextField圖標位置設置

var leftImageView = UIImageView() 
leftImageView.image = leftImage 
textField.leftView = leftImageView 
textField.leftViewMode = UITextFieldViewMode.Always 
leftImageView.frame = CGRectMake(15, 10, 15, 20) 
textField.addSubview(leftImageView) 

O/P爲這個Image is close to textfield border

我發現像從上述代碼

textField.leftView = leftImageView 

它給圖標刪除此代碼溶液添加圖標中的UITextField對齊正確,但每當開始編輯像這樣的圖標上的文本字段文本enter image description here

我想這樣

enter image description here

+0

在swift 3.0中試試這個很酷的庫https://github.com/Sahilberi/ImageTextField – Sahil

回答

12

首先O/P,你絕對不應該添加圖像作爲一個子視圖的標籤。設置leftView屬性就足夠了。其次,您應用於左視圖幀的任何x或y偏移量都將被忽略。文本字段只關心視圖的大小。如果你想在圖像周圍添加填充,一個選項是使用容器視圖並將圖像視圖放置在其中。

let leftImageView = UIImageView() 
leftImageView.image = leftImage 

let leftView = UIView() 
leftView.addSubview(leftImageView) 

leftView.frame = CGRectMake(0, 0, 30, 20) 
leftImageView.frame = CGRectMake(0, 0, 15, 20) 

textField.leftView = leftView 

另一種選擇是子類UITextField並覆蓋leftViewRectForBounds

2

試試這個。可能會幫助你。

var padding: Float = 20 

var envelopeView: UIImageView = UIImageView(frame: CGRectMake(padding, 0, 30, 30)) 
envelopeView.image = UIImage.imageNamed("comment-128.png") 
envelopeView.contentMode = UIViewContentModeScaleAspectFit 

var viewLeft: UIView = UIView(frame: CGRectMake(padding, 0, 30, 30)) 
viewLeft.addSubview(envelopeView) 
textField.leftView.setFrame(envelopeView.frame) 
textField.leftView = viewLeft 
textField.leftViewMode = UITextFieldViewModeAlways 

var viewRight: UIView = UIView(frame: CGRectMake(textField.frame.size.width - (textField.frame.size.width + 30 + padding), 0, 30, 30)) 
viewRight.addSubview(envelopeView) 
textField.rightView.setFrame(envelopeView.frame) 
textField.rightView = viewRight 
textField.rightViewMode = UITextFieldViewModeAlways 
4

參考@hennes語法,還有一些語法迅速缺少類似的CGRect語法變化,userNameTextField.leftViewMode = .always缺少

與SWIFT語法這個工作對我來說:

let leftImageView = UIImageView() 
    leftImageView.image = UIImage(named: "email") 

    let leftView = UIView() 
    leftView.addSubview(leftImageView) 

    leftView.frame = CGRect(x: 0, y: 0, width: 40, height: 40) 
    leftImageView.frame = CGRect(x: 10, y: 10, width: 20, height: 20) 
    userNameTextField.leftViewMode = .always 
    userNameTextField.leftView = leftView 
1

雨燕3.1

extension UITextField 
{ 
    enum Direction 
    { 
     case Left 
     case Right 
    } 

    func AddImage(direction:Direction,imageName:String,Frame:CGRect,backgroundColor:UIColor) 
    { 
     let View = UIView(frame: Frame) 
     View.backgroundColor = backgroundColor 

     let imageView = UIImageView(frame: Frame) 
     imageView.contentMode = .center 
     imageView.image = UIImage(named: imageName) 

     View.addSubview(imageView) 

     if Direction.Left == direction 
     { 
      self.leftViewMode = .always 
      self.leftView = View 
     } 
     else 
     { 
      self.rightViewMode = .always 
      self.rightView = View 
     } 
    } 
} 
+1

如何使用此@jethava yogesh –

+0

yourtextbox.AddImage(direction:.Left,imageName:「imageName」,Frame:CGRect(x:0,y:0,width:100,height:40),backgroundColor:UIColor.red ) –