Resigning textfield by clicking on other textfield in swift - swift

i have three text fields. one is for showing date picker, second is to showing options for gender and another one is for showing country code. let say gender dropdown box is showing now, if i will click on dateofbirth or contrycode textfield, then gender will be hidden. like that if date of birth is showing and i will click gender or country code textfield , then date of birth textfield will be hidden. same will work for countrycode.
code for showing gender textfield:-
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
if textField == gender_TextField{
tableViewOne.snp.updateConstraints { (make) in
make.top.equalTo(textField.frame.maxY)
make.left.equalTo(textField.frame.origin.x)
make.right.equalTo(0-(self.containerView.frame.maxX-textField.frame.maxX))
make.height.equalTo(0)
}
self.containerView.layoutIfNeeded()
UIView .animate(withDuration: 0.4, animations: {
self.tableViewOne.snp.updateConstraints({ (make) -> Void in
make.height.equalTo(130)
})
self.containerView.layoutIfNeeded()
}, completion: nil)
if(textField == gender_TextField){
self.selectedTextfeild = 1
self.selectedTextFieldType = DROPDOWN_TEXTFIELD.gender
}
tableViewOne.tableFooterView = UIView()
tableViewOne.reloadData()
tableViewOne.isHidden = !isTableViewHiden
isTableViewHiden = !isTableViewHiden
return false
}
return true
}
code for showing date picker
datePickerView.addTarget(self, action: #selector(dateChanged(datePicker:)), for: .valueChanged)
datePickerView.datePickerMode = .date
dob_TextField.inputView = datePickerView
datePickerView.maximumDate = Date()
code for showing countrycode
countryPickerView.delegate = self
if countryPickerView.selectedCountry.phoneCode != "+93"{
countryCode_Lable.text = countryPickerView.selectedCountry.phoneCode
}
else{
countryCode_Lable.text = "+1"
}

You can hide the other two views and can resign the other two textFields when one is active.

Related

How to highlight one part of a tableview cell in swift?

I have two clicks of gesture recognizer in my tableview cell to access different storyboards.
My second click is in the extreme right of my cell and I want when I click on it to just highlight that button and not the whole cell which includes the other button
So my question is : how to separate highlights of different clicks in a tableview cell ?
First you need to set the
tableView.allowsSelection = false
You can add two views in the cell and add tap
func setAccessoryViewTapGestures() {
var tapGestureforLeftAccesoryView = UITapGestureRecognizer()
tapGestureforLeftAccesoryView = UITapGestureRecognizer(target: self, action: #selector(leftAccessoryViewTapped(_:)))
tapGestureforLeftAccesoryView.numberOfTapsRequired = 1
tapGestureforLeftAccesoryView.numberOfTouchesRequired = 1
leftAccessoryView.addGestureRecognizer(tapGestureforLeftAccesoryView)
leftAccessoryView.isUserInteractionEnabled = true
var tapGestureforRightAccesoryView = UITapGestureRecognizer()
tapGestureforRightAccesoryView = UITapGestureRecognizer(target: self, action: #selector(rightAccessoryViewTapped(_:)))
tapGestureforRightAccesoryView.numberOfTapsRequired = 1
tapGestureforRightAccesoryView.numberOfTouchesRequired = 1
rightAccessoryView.addGestureRecognizer(tapGestureforRightAccesoryView)
rightAccessoryView.isUserInteractionEnabled = true
}
Here rightAccessoryView and leftAccessoryView are the view in the cell
#objc private func leftAccessoryViewTapped(_ sender: UITapGestureRecognizer) {
// Add code to highlight the view
}
#objc private func rightAccessoryViewTapped(_ sender: UITapGestureRecognizer) {
// Add code to highlight the view
}
I think this helps

Trouble setting UIButton title of selected Action picker item, SWIFT

I am finding trouble to set title for button of action picker item.
ActionSheetMultipleStringPicker.show(withTitle: "Select Nationality", rows: [
nationalityArray], initialSelection: [selectedNationalityIdStr],
doneBlock: {
picker, indexes, values in
let selectedEmirate = "\(values)"
self.btnNationality.setTitle(selectedEmirate, for: .normal)
print("values = \(values)")
print("indexes = \(finalIntId)")
print("picker = \(picker)")
return
}, cancel:
{
ActionMultipleStringCancelBlock in return
}, origin: sender)
the print of Values inside the Action sheet picker is this
values = Optional(<__NSSingleObjectArrayI 0x600001ba1740>(Expat)
so when I write this below line to set title for a UIButton then it is setting up whole
self.btnNationality.setTitle("\(selectedEmirate)", for: .normal)
As you can see here, it is taking whole values string instead of only "expat":
Some one help me setting up only "expat" from the values of my action sheet picker please

tableview custom cell button single selection/deselction

i have been coding for a while i am stuck with tableview single selection/deselection.
I have a tableview with custom cell which has a button. if i click on one button another button in bottom gets selected as well.For example when i click on button with index 2 another button gets click in the button.
it should be like when i click on one button other buttons should be deselected.
Thanks!
func QuickReview( sender: UIButton){
if cell.EventReviewQuickeReviewBtn.isSelected == true {
cell.EventReviewQuickeReviewBtn.layer.borderColor = UIColor.red
cell.EventReviewQuickeReviewBtn.backgroundColor = UIColor.white
cell.EventReviewQuickeReviewBtn.layer.borderWidth = 1
cell.EventReviewQuickeReviewBtn.isSelected = false
}
else {
cell.EventReviewQuickeReviewBtn.layer.backgroundColor = UIColor.green
cell.EventReviewQuickeReviewBtn.setTitleColor(UIColor.white, for: .normal)
cell.EventReviewQuickeReviewBtn.isSelected = true
}
}
Cells get reused. You need to ensure the exact same set of properties are set/reset in both conditions.
func QuickReview( sender: UIButton){
if cell.EventReviewQuickeReviewBtn.isSelected {
cell.EventReviewQuickeReviewBtn.layer.borderColor = UIColor.red
cell.EventReviewQuickeReviewBtn.backgroundColor = UIColor.white
cell.EventReviewQuickeReviewBtn.layer.backgroundColor = UIColor.white // or whatever
cell.EventReviewQuickeReviewBtn.layer.borderWidth = 1
cell.EventReviewQuickeReviewBtn.setTitleColor(UIColor.red, for: .normal) // or whatever
} else {
cell.EventReviewQuickeReviewBtn.layer.borderColor = UIColor.black // or whatever
cell.EventReviewQuickeReviewBtn.backgroundColor = UIColor.green // or whatever
cell.EventReviewQuickeReviewBtn.layer.backgroundColor = UIColor.green
cell.EventReviewQuickeReviewBtn.layer.borderWidth = 3 // or whatever
cell.EventReviewQuickeReviewBtn.setTitleColor(UIColor.white, for: .normal)
}
cell.EventReviewQuickeReviewBtn.isSelected = !cell.EventReviewQuickeReviewBtn.isSelected
}
when you select any button then you should have mark isSelected property to false
assuming you have a list of models for your table view like
class Example{
...
var isSelected: Bool
...
}
(in ViewController)
let list: [Example] = [example1, example2, example3]
on tap of any button deselect all then selected desire button like
list.forEach { (example) in
example.isSelected = false
}
list[selectedButtonIndex].isSelected = true
and then reload your tableView.

SCLAlertView - Focus on textfield

I have a SCLAlertView with a textfield inside it. How can I make that textfield become first responder so that when the alert view is shown, i can automatically start writing instead of clicking on the textfield first?
After showing alert you can put this code at the end of function.
#IBAction func showEdit(_ sender: AnyObject) {
let appearance = SCLAlertView.SCLAppearance(showCloseButton: true)
let alert = SCLAlertView(appearance: appearance)
let txt = alert.addTextField("Enter your name")
_ = alert.addButton("Show Name") {
print("Text value: \(txt.text ?? "NA")")
}
_ = alert.showEdit(kInfoTitle, subTitle:kSubtitle)
//Add this code in show function to set textfield as first responder
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
txt.becomeFirstResponder()
}
}
If you have more than one textfield than you have to set first textfield as first responder.

How to put self.navigationItem.titleView with title after replace it with searchbar in swift

I have searchbar , when click image, it will create searchbar at top navigation , replace with title. Then when tap outside searchbar , the title don't show again , it still with searchbar (textfield), how to show again title if tap or click outside from searchbar ?
this is code to create searchBar at top navigation , replace with title.
func createSearchBar(){
searchBar.sizeToFit()
searchBar.showsCancelButton = false
searchBar.placeholder = "Enter key"
searchBar.delegate = self
self.navigationItem.titleView = searchBar
}
This is code when tap outside searchbar
extension ViewCon {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewCon.dismissKeyboard))
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
self.searchBar.resignFirstResponder()
self.navigationController?.navigationBar.topItem?.title="Title List"
}
}
But how i want show title on top navigation again ?
you need to set the to nil
self.navigationItem.titleView = nil
see: https://developer.apple.com/reference/uikit/uinavigationitem/1624935-titleview