UITextField w/ drop down button - ios

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.

Related

Remove rightView property from textfield swift 4

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

How to add badge to UINavigationItem (UIBarButtonItem) - Swift 3

Dose ios providing badge on UIBarButtonItem?
if not then how to do it programmatically or use any library(what are the best library)
In image i want to show badge on UIBarButtonItem (Refresh button icon) like red color round or numbers any thing.
I'm using MIBadgeButton its working like a charm.
var appNotificationBarButton: MIBadgeButton! // Make it global
self.appNotificationBarButton = MIBadgeButton(frame: CGRect(x: 0, y: 0, width: 50, height: 40))
self.appNotificationBarButton.setImage(UIImage(named: "bell"), for: .normal)
self.appNotificationBarButton.badgeEdgeInsets = UIEdgeInsetsMake(10, 0, 0, 10)
self.appNotificationBarButton.addTarget(self, action: #selector(self.appNotificationClicked), for: .touchUpInside)
let App_NotificationBarButton : UIBarButtonItem = UIBarButtonItem(customView: self.appNotificationBarButton)
self.navigationItems.rightBarButtonItem = App_NotificationBarButton

Why don't UIButtons interact (change state to highlighted) when touched?

I always faced that issue and never been able to get the answer. When I create the UIButton programmatically and add to the UI and then when you touch the button that state on the UI not being changed to highlighted/down as the result of interacting with that button.
Another thing, 'sometimes' UIButtons when they are added through the interface builder, they work fine and when you touch them you can see the state in the UI changed to highlighted/down.
Any idea why is this happening?
If your button type is custom you had to set image / color for highlighted state, to differ the state change (From normal to highlighted).
If your button Type is system, you will see the state change effect without changing image / color.
I think you are noticing the difference between UIButton with type Custom vs. System.
I put together this playground to show the difference.
import UIKit
import XCPlayground
let view = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
view.backgroundColor = .whiteColor()
let button1 = UIButton(type: .System)
button1.frame = CGRect(x: 0, y: 0, width: 200, height: 50)
button1.setTitle("One", forState: .Normal)
button1.backgroundColor = .blackColor()
view.addSubview(button1)
let button2 = UIButton()
button2.frame = CGRect(x: 0, y: 60, width: 200, height: 50)
button2.setTitle("Two", forState: .Normal)
button2.backgroundColor = .grayColor()
view.addSubview(button2)
XCPlaygroundPage.currentPage.liveView = view
Please post your programatic button code, so we can investigate more

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

Enter a rightview UIImageView to a UITextField

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

Resources