Add a Button on Right View of Uitextfield in Such Way That, Text Should Not Overlap the Button

Add a button on right view of UItextfield in such way that, text should not overlap the button

use the rightView property of the UITextField. Basically, there are two properties (this one and leftView accordingly) that allow you to place custom views/controls inside the UITextField. You can control whether those views are shown or not by means of rightViewMode/leftViewMode properties:

textField.rightView = btnColor
textField.rightViewMode = .unlessEditing

for e.g

let button = UIButton(type: .custom)
button.setImage(UIImage(named: "send.png"), for: .normal)
button.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0)
button.frame = CGRect(x: CGFloat(txt.frame.size.width - 25), y: CGFloat(5), width: CGFloat(25), height: CGFloat(25))
button.addTarget(self, action: #selector(self.refresh), for: .touchUpInside)
textField.rightView = button
textField.rightViewMode = .always

and call the action as

@IBAction func refresh(_ sender: Any) {
}

Add text and image to UITextField right side

For that you can create on UIView instance and add the UILabel and UIImageView inside it then finally set that view as rightView of textField. Some thing like below.

let rightView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 40))

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 60, height: 40))
label.text = "Text"
label.textColor = UIColor.red
label.textAlignment = .center

let imageView = UIImageView(frame: CGRect(x: 40, y: 0, width: 40, height: 40))
imageView.image = UIImage(named: "selectdrop")
rightView.addSubview(label)
rightView.addSubview(imageView)

txtField.rightView = rightView
txtField.rightViewMode = .always

Put two (side by side) buttons as a right side overlay of an UITextField

Create UIButton as type of .custom instead of .system.

And as you already know renameField's height in viewDidLoad, you don't need to bother with constraint.

The code below is enough.

override func
viewDidLoad() {
super.viewDidLoad()

let wSize = renameField.frame.size.height - 2

let saveButton = UIButton( type: .custom )
saveButton.setImage( #imageLiteral(resourceName: "buttonCheckmark"), for: .normal )
saveButton.addTarget( self, action: #selector(renameSave), for: .touchUpInside )
saveButton.frame = CGRect( x: 0, y: 0, width: wSize, height: wSize )

let cancelButton = UIButton( type: .custom )
cancelButton.setImage( #imageLiteral(resourceName: "buttonX"), for: .normal )
cancelButton.addTarget( self, action: #selector(renameCancel), for: .touchUpInside )
cancelButton.frame = CGRect( x: wSize, y: 0, width: wSize, height: wSize )

let wV = UIView()
wV.frame = CGRect( x:0, y:0, width: wSize * 2, height: wSize )
wV.addSubview( saveButton )
wV.addSubview( cancelButton )

renameField.rightView = wV;
renameField.rightViewMode = .always;
}

Show a Custom View on textField tap just as we show a picker in the place of keyboard

let myTFrame = CGRectMake(0, 0, DC.screenWidth/2-53, 30)
myT = UITextField(frame: myTFrame)
myT.delegate = self
myT.textAlignment = .Right
myT.text = "תת קטגוריה"
myT.textColor = .grayColor()
myT.delegate = self
tempView.frame = CGRectMake(0, 0, 320, 256)
myT.inputView = tempView
view.addSubview(myT)


Related Topics



Leave a reply



Submit