Enter a rightview UIImageView to a UITextField - ios

I have a target action that when a button is pressed, I validate the UITextField:
// Get the text from the UITextField
NSString *nameStr = _name.text;
_name.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"error.png"]];
[self.view addSubview:_name];
if (nameStr.length == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Invalid Name"
message:#"You must enter a name."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
My UITextField are added to the view in viewDidLoad like so:
[self.view addSubview:_name];
How do I make a rightView UIImageView appear?

You have to set rightViewMode of the Textfield because The default value for this property is UITextFieldViewModeNever.
so You have to set Mode from following,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways

Are you sure you are not missing the UITextField frame ? And are you setting the rightViewMode ?
This is an example i just wrote seem to be working fine.
UITextField *rightField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 140, 50)];
rightField.backgroundColor = [UIColor redColor]; // For testing purpose
rightField.rightViewMode = UITextFieldViewModeAlways;// Set rightview mode
UIImageView *rightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Tick_white_color"]];
rightField.rightView = rightImageView; // Set right view as image view
[self.view addSubview:rightField];
Here is the tick image as result :

For Swift 3.To show image before placeholder text in UITextField
func setPaddingView(strImgname: String,txtField: UITextField){
let imageView = UIImageView(image: UIImage(named: strImgname))
imageView.frame = CGRect(x: 0, y: 5, width: imageView.image!.size.width , height: imageView.image!.size.height)
let paddingView: UIView = UIView.init(frame: CGRect(x: 0, y: 0, width: 50, height: 30))
paddingView.addSubview(imageView)
txtField.leftViewMode = .always
txtField.leftView = paddingView
}
To call Function
self.setPaddingView(strImgname: "mail", txtField: txtEmail)

Solution in Swift 3:
class CustomText: UITextField {
#IBInspectable var rightImage : UIImage?{
didSet{
self.rightView = UIImageView(image: rightImage)
// select mode -> .never .whileEditing .unlessEditing .always
self.rightViewMode = .always
}
}
}

Solution with #IBDesignable and Swift 3
Firstly, you can create #IBDesignable for UITextField like this.
#IBDesignable extension UITextField{
#IBInspectable var setImageName: String {
get{
return ""
}
set{
let imageView: UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imageView.image = UIImage(named: newValue)!
self.rightView = imageView
self.rightViewMode = UITextFieldViewMode.always
}
}
Note: You must be write get and set points for #IBInspactable block because Swift has warn to apply getter and setter together. We've not need to use the get statement, so returned empty string. Also the attribute name has include Button but not confuse, its a mistake.
Then click the UITextField on StoryBoard and give the spesific image name to the setAButtonImageName attribute.
Then you will get the following result
The other situation is the create rightView with seems like has right margin
Here is the screenshot.
You can use a UIView as container than can add the UIImageView into the this UIView. In here, you must calculate the right margin, container view's width and `UIImageViews' width.
#IBInspectable var setImageName: String {
get{
return ""
}
set{
let containerView: UIView = UIView(frame: CGRect(x: 0, y: 0, width:50, height: self.frame.height))
let imageView: UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imageView.image = UIImage(named: newValue)!
containerView.addSubview(imageView)
imageView.center = containerView.center
self.rightView = containerView
self.rightViewMode = UITextFieldViewMode.always
}
}

If you have two or more field then try this code! its working for me.
dropDownImageViewForTextField1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
dropDownImageViewForTextField1.image = [UIImage imageNamed:#"dropdown_n_icon.png"];
dropDownImageViewForTextField2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
dropDownImageViewForTextField2.image = [UIImage imageNamed:#"dropdown_n_icon.png"];
txtField1.rightViewMode = UITextFieldViewModeAlways;
txtField1.rightView = dropDownImageViewForTextField1;
txtField2.rightViewMode = UITextFieldViewModeAlways;
txtField2.rightView = dropDownImageViewForTextField2;
Do check 2 things
txtField.rightViewMode = UITextFieldViewModeAlways
Separate imageView for each textField

Related

Swift - response view in every view?

I've got a small question: is it possible somehow (without storyboard) to create a little view at the top of the screen (if there's a navigationbar, then under that), that displays errors / responses if needed?
Without creating views on every single viewController I made, just by code?
Or is there some extension you could recommend?
For example: "No Internet Connection"
You can check this answer - https://stackoverflow.com/a/49129636/6080920
Code required
extension UIViewController
{
func showNotificationView(message : String)
{
//base View
let baseView = UIView(frame: CGRect(x: 20, y: self.view.frame.size.height-(self.view.frame.size.height*0.15), width: self.view.frame.size.width-40, height: self.view.frame.size.height*0.08))
baseView.backgroundColor = UIColor.gray
baseView.clipsToBounds=true
self.view.addSubview(baseView)
//Image View
let imageView = UIImageView(image: UIImage(named: "RM_3"))
imageView.clipsToBounds=true
imageView.frame = CGRect(x: 0, y: 0, width: baseView.frame.size.width*0.2, height: baseView.frame.size.height)
baseView.addSubview(imageView)
//Label
let textLabel = UILabel(frame: CGRect(x: baseView.frame.size.width*0.2+10, y: 0, width: baseView.frame.size.width, height: baseView.frame.size.height))
textLabel.textColor = UIColor.white
textLabel.backgroundColor = UIColor.clear
textLabel.textAlignment = .left;
textLabel.numberOfLines = 0
textLabel.font = UIFont(name: "Montserrat-Light", size: 12.0)
textLabel.text = message
baseView.addSubview(textLabel)
}
}
Usage
#IBAction func navigate(_ sender: Any) {
self.showNotificationView(message: "hihihihh")
}

Custom navigation bar image and text

I need in my app a custom navigation bar with an image and a text but I can't add the text.
Here is the code to add the image, how can I add the title?
let logo = #imageLiteral(resourceName: "navigationbaricon")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
Thanks
Where is the frame assigned for self.navigationItem.titleView? Set the frame for imageView and it will work.
You can wrap the UIImageView and the UILabel (which will hold the custom title) in an UIView and then assign the UIView to the self.navigationItem.titleView. Something like this:
let view = UIView(...);
let label = UILabel(...);
label.text = "Custom Title";
let image = UIImageView(image: UIImage(named: "..."));
view.addSubview(image);
view.addSubview(label);
self.navigationItem.titleView = view;
This one is worked for me
override func viewDidLoad() {
super.viewDidLoad()
let titleView = UIView()
titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
titleView.backgroundColor = UIColor.redColor()
let imageView = UIImageView(image: UIImage(named: "img")!)
imageView.frame = CGRectMake(0, 0, 40, 40)
imageView.contentMode = .ScaleAspectFill
titleView.addSubview(imageView)
self.navigationItem.titleView = titleView
}

Adding space in UITextField After image

My problem is that I have created a textfield which has leftviewmode as an image and the placeholder is in somewhere middle of textfield where the text editing should get start when user starts typing. So how to get my text started from middle of textfield just where the placeholder is. I cannot set the leftviewmode in imageview because I already added image there. What should I do?
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"a_point30"]];
image.frame = CGRectMake(0, 0, image.image.size.width, image.image.size.height-10);
self.enterSource.leftViewMode = UITextFieldViewModeAlways; self.enterSource.leftView = image;
For example textediting should began from hello world placeholder.
You can do something like this,
UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imgName]];
imgView.frame = CGRectMake(0.0, 0.0, dropDown.image.size.width+10.0, dropDown.image.size.height);
imgView.contentMode = UIViewContentModeLeft;
textField.leftView = imgView;
textField.leftViewMode = UITextFieldViewModeAlways;
In Swift 2.2.1, it will be something like this:
let imageView = UIImageView(image: UIImage(named: "ImageName"))
imageView.frame = CGRectMake(0.0, 0.0, imageView.image!.size.width + 10, imageView.image!.size.height)
imageView.contentMode = UIViewContentMode.Left
textField.leftView = imageView
textField.leftViewMode = UITextFieldViewMode.Always

Convert C to Swift: Add magnifying glass icon to UITextField

How can I add a magnifying glass icon to the left of a UITextField?
I found an answer to a similar question here but I'm having trouble converting it to swift.
The answer:
So, here's the code with the unicode character:
UILabel *magnifyingGlass = [[UILabel alloc] init];
[magnifyingGlass setText:[[NSString alloc] initWithUTF8String:"\xF0\x9F\x94\x8D"]];
[magnifyingGlass sizeToFit];
[textField setLeftView:magnifyingGlass];
[textField setLeftViewMode:UITextFieldViewModeAlways];
Edit: For plain look that fits iOS 7 style, add Unicode variation selector \U000025B6.
My code:
let searchIconView = UILabel()
// Doesn't work: searchIconView.text = NSString.init(UTF8String: "\xF0\x9F\x94\x8D")
searchIconView.sizeToFit()
searchTextField.leftView = searchIconView
searchTextField.leftViewMode = .Always
Do you have a specific reason for trying to add the icon as a UTF8String? If you have a magnifying glass icon as a PNG image, you can use the "leftView" property of UITextField like this;
let imageView = UIImageView()
let magnifyingGlassImage = UIImage(named: "magnifyingGlass")
imageView.image = magnifyingGlassImage
//arrange the frame according to your textfield height and image aspect
imageView.frame = CGRect(x: 0, y: 5, width: 45, height: 20)
imageView.contentMode = .ScaleAspectFit
txtField.leftViewMode = .Always
txtField.leftView = imageView
In Swift you can assign emoji characters directly
searchIconView.text = "🔍"
two simple approaches:
You can use XCode to add Unicode symbols in your code directly (Edit->Emoji&Symbols).
You can use something like that
searchIconView.text = NSString(unicodeScalarLiteral: "\u{D83D}") as String
(you have to use your character here)
Updated for Swift 5 and using the System magnifying glass:
let imageView = UIImageView()
let magnifyingGlassImage = UIImage(systemName: "magnifyingglass", withConfiguration: UIImage.SymbolConfiguration(weight: .regular))?.withTintColor(.systemYellow, renderingMode: .alwaysOriginal)
imageView.image = magnifyingGlassImage
//arrange the frame according to your textfield height and image aspect
imageView.frame = CGRect(x: 0, y: 5, width: 45, height: 20)
imageView.contentMode = .scaleAspectFit
txtField.leftViewMode = .always
textField.leftView = imageView

UITextField w/ drop down button

can a UITextField do this UI with codes alone or I need to use an image or use another component for this? like button,label.
Try this.
UIImageView *imgViewForDropDown = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
imgViewForDropDown.image = [UIImage imageNamed:#"dropDown.png"];
yourTextField.rightView = imgViewForDropDown;
yourTextField.rightViewMode = UITextFieldViewModeAlways;
For Swift Code:
let imgViewForDropDown = UIImageView()
imgViewForDropDown.frame = CGRect(x: 0, y: 0, width: 30, height: 48)
imgViewForDropDown.image = UIImage(named: "ic_keyboard_arrow_down_white")
yourTextField.rightView = imgViewForDropDown
yourTextField.rightViewMode = .always
This works:
fileprivate func addBtnToTextField() {
let dropDownBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 24, height: 24))
dropDownBtn.setBackgroundImage(UIImage(named: "dropDownTringle"), for: UIControlState.normal)
startDateTxt.rightViewMode = UITextFieldViewMode.always
startDateTxt.rightView = dropDownBtn
}
You can implement this by using UITextField,UIButton and UITableView with some animations,anyway Once have look at this example ios dropdown
UITextField not provide this kind of feature. you need to take a UIImageView, UIButton and UITextField , set a image in UIImageView which contain your textfield background image, disable the UserInteraction property of textfield and keep it on a imageView , take a custom button without title and perform the action with button which you want.

Resources