Remove rightView property from textfield swift 4 - ios

I'm using rightview property to a textfield if it contains some text in it. I need to remove the property if the textfield doesn't contain any text. I have searched so many links but didn't find any answer for this. Please help me.
To add rightView property:
let cardImage = UIImageView(image: UIImage(named: icon))
cardImage.frame = CGRect(x: 10, y: 20, width: 70, height: self.cardTxtFld.frame.size.height-20);
cardImage.contentMode = UIViewContentMode.scaleAspectFit
self.cardTxtFld.rightView = cardImage;
self.cardTxtFld.rightViewMode = UITextFieldViewMode.always

Related

Material design Outline textfield with white background

Im trying to use material components library and make a textfield with outline and background color, but this is what I get
The white is outside the bounds.
Can you help me?
this is my code
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imageView.image = UIImage(named: "user")
txtfieldEmail.placeholderLabel.textColor = UIColor.gray
txtfieldEmail.leftView = imageView
txtfieldEmail.leftViewMode = .always
//txtfieldEmail.backgroundColor = UIColor.white
//txtfieldEmail.translatesAutoresizingMaskIntoConstraints = false
//txtfieldEmail.clipsToBounds = true
txtfieldEmail.layer.backgroundColor = UIColor.white.cgColor
usernameTextFieldController = MDCTextInputControllerOutlined(textInput: txtfieldEmail)
After I kept editing the form and adding more content, I ended up finding that we should be defining the style in the MDCTextInputControllerOutlined
So all I had to do was usernameTextFieldController?.borderFillColor = UIColor.white

UITextField not getting displayed in View

I'm programatically creating a UITextField, called nameTextField in my viewDidLoad something like this:
var nameTextField = UITextField()
nameTextField = UITextField(frame: CGRect(x: 10, y: 20, width: 30, height: 20))
nameTextField.borderStyle = .roundedRect
nameTextField.autocorrectionType = UITextAutocorrectionType.no
nameTextField.returnKeyType = UIReturnKeyType.done
nameTextField.keyboardType = UIKeyboardType.default
nameTextField.backgroundColor = UIColor.white
nameTextField.placeholder = "Enter Your Full Name"
nameTextField.delegate = self
self.view.addSubview(nameTextField)
I'm not seeing this my view, what mistake Am I making? Thanks in advance.
may be your view background color is white so that reason it wont show change the background color
nameTextField.backgroundColor = UIColor.red
choice 2
if your VC is embed with navigation controller , change the Y frame and check
nameTextField = UITextField(frame: CGRect(x: 10, y: 90, width: 30, height: 20))
as well as change the bg color
nameTextField.backgroundColor = UIColor.red
You need to specify where you want the text field to show up relative to the main view of your view controller. For instance this would work: nameTextField.center = self.view.center

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

Swift how to make for navigation middle section image and text next to each other

I have searched a lot on stackoverflow and I havent been able to find solution for my problem which is that I need to put in navigationBar in middle section image and text next to each other but apparently I was able only to place just image or just text
Here is my code
let nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Default
nav?.tintColor = UIColor.blackColor()
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imageView.contentMode = .ScaleAspectFit
let image = UIImage(named: "icon_heart")
imageView.image = image
self.navigationItem.title = "App name"
self.navigationItem.titleView = imageView
I am aware that it on this way it accept only image which is titleView but I cant think of some normal solution and dont know is it even possible. Also if that is not possible how can I make then blank image and draw that icon image and text next to each other and append then that new image.
Edit:
Based on awph answer I have added code like this and it works fine for my case where I have always same characters in title
let screenSize: CGRect = UIScreen.mainScreen().bounds
let nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Default
nav?.tintColor = UIColor.blackColor()
let imageView = UIImageView(frame: CGRect(x: screenSize.width / 2 - 40, y: 15, width: 20, height: 20))
imageView.contentMode = .ScaleAspectFit
let image = UIImage(named: "icon_heart")
imageView.image = image
nav!.addSubview(imageView)
// label on top of the application that contain word
var label = UILabel(frame: CGRectMake(0, 0, 0, 0))
label.center = CGPointMake(screenSize.width / 2 - 15, 17)
label.textColor = UIColor.blackColor()
label.font = UIFont.systemFontOfSize(14)
label.textAlignment = NSTextAlignment.Center
label.text = "App Name"
label.sizeToFit()
nav!.addSubview(label)
You right, as said in documentation:
// Custom view to use in lieu of a title. May be sized horizontally. Only used when item is topmost on the stack.
What you need to do is to create an UIView, add an UImageView (that contains your image) as subview (view.addSubview(imageView)) and then add a UILabel on it view.addSubview(label).

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