Add custom button UISearchController in swift - swift

i want to add a custom button on UISearchController in swift.
Like this image i want to add the custom button. In this image it is a search bar. The plus button will show if there is text in the search bar and it will disappear if search bar is empty. can any one help me to implement it.

When the TextField changes, check for whitespace.
Hide or position the button depending on whether it is blank or not.
After the view is loaded, the text field is blank, so it is initialized with ishidden().
I hope my answer is helpful to you.
SearchViewController.swift
import UIKit
class SearchViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var searchTextField: UITextField!
#IBOutlet weak var plusButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
searchTextField.delegate = self
plusButton.isHidden = true
}
func textFieldDidChangeSelection(_ textField: UITextField) {
if searchTextField.text == "" {
plusButton.isHidden = true
} else {
plusButton.isHidden = false
}
}
}
Preview

Related

How to change Weight property of UIButton title in a custom UITableViewCell?

I have a custom cell that has three buttons, that when one of them is pressed, its title weight changes to bold and the font size of the other two buttons gets shrunked.
As it is shown above, while the titles of the other buttons do change their font size, the weight of the title of the pressed button does not seem to change at all. What am I missing? Thanks.
The code of the custom cell:
import UIKit
class SymptomTableViewCell: UITableViewCell {
#IBOutlet weak var severeButton: UIButton!
#IBOutlet weak var moderateButton: UIButton!
#IBOutlet weak var lowButton: UIButton!
var selectedButton = UIButton()
var buttons = [UIButton]()
override func awakeFromNib() {
super.awakeFromNib()
buttons = [severeButton, moderateButton, lowButton]
}
#IBAction func rateButtonPressed(_ sender: UIButton) {
if sender != selectedButton {
selectedButton = sender
for button in buttons {
if button != selectedButton {
button.titleLabel?.font = .systemFont(ofSize: 12)
}
}
selectedButton.titleLabel?.font = .boldSystemFont(ofSize: 16)
}
}
}
Changing button style from plain to default worked.

How do I change the text of a label with a segmented controller that has three segments>

For class I have a segmented controller that has three segments.
First segment: make label say Hello
Second segment make label say goodbye
Third segment make label say you rock.
I also need to add a name entered by a text field and when a button is pushed it goes into the label either starting with Hello, Goodbye, or You Rock.The picture is showing what I have so far.
second picture shows what I need to do
You can get the title of the selected segment and insert it into the name label text.
class ViewController: UIViewController {
// MARK: Outlets
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var greetingControl: UISegmentedControl!
// MARK: Properties
var name: String? {
return nameTextField.text
}
var greeting: String? {
return greetingControl.titleForSegment(
at: greetingControl.selectedSegmentIndex
)
}
// MARK: Methods
private func updateOutlets() {
nameLabel.text = "\(greeting!), \(name!)!"
}
// MARK: Actions
#IBAction func changeNameOnTap(_ sender: UIButton) {
updateOutlets()
}
#IBAction func nameController(_ sender: UISegmentedControl) {
updateOutlets()
}
}

textfield not empty show save button in Swift

My save button is hidden with the function saveBtnHidden() in the code below. However, the save button won't reappear when text is typed into the text field. I have tried multiple solutions similar to this. Every time that I type into the text field, the save button just won't show up.
import UIKit
class TableViewController: UITableViewController, UITextFieldDelegate {
#IBOutlet weak var saveBtn: UIButton!
#IBOutlet var nicknameField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
saveBtnHidden()
}
func saveBtnHidden() {
if (nicknameField.text?.isEmpty ?? true) {
// is empty
saveBtn.isHidden = true
} else {
saveBtn.isHidden = false
}
}
#IBAction func saveBtnPressed(_ sender: Any) {
performSegue(withIdentifier: "nextPage", sender: nil)
}
}
You are getting this error because your function saveBtnHidden() is only called once in viewDidLoad(). It does not get called again when the text in your text field changes. To detect when text changes, you will need to add a target to your text field that calls a function when it changes (.editingChanged) like this:
nicknameField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
Then in the textFieldDidChange call your saveBtnHidden() function:
func textFieldDidChange(_ textField: UITextField) {
saveBtnHidden()
}
Code adapted from: How do I check when a UITextField changes?
Use delegate to be notify of any change. Delegates is a key part of iOS development and Apple's framework.
class TableViewController: UITableViewController, UITextFieldDelegate {
#IBOutlet weak var saveBtn: UIButton!
#IBOutlet var nicknameField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
saveBtnHidden()
nicknameField.delegate = self
}
func textFieldDidChange(_ textField: UITextField) {
saveBtnHidden()
}
// More of your source code below...

Change cell title in TableViewController in another ViewController

I'm trying to change the title in a TableViewController from another ViewController. (see image)
The second ViewController is the one with the 3 cells and the third one is the one with a textfield (inputText in code), a button (changeText) and a label (outputLabel). I would like this app to remember what I put in the text field when I go back to the table view and then back into the ViewController. What happens now is:
- I change the text, hit the button and the label changes.
- I go back to the TableViewController and then I go into the ViewController that I was just in with a changed label
- The label is what it was before...
How can I make the app 'remember' what I put in in the text field and what the label was like? My code (ViewController.swift, I linked the 3rd controller to this file, haven't linked the 2nd controller to anything (yet?)):
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var outputLabel: UILabel!
#IBOutlet weak var inputText: UITextField!
#IBAction func changeText(_ sender: UIButton) {
outputLabel.text = inputText.text
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Thanks in advance!
You can reference your ViewController in first controller (TableViewController),
make public inputText
#IBOutlet public weak var inputText: UITextField!
and in viewDidAppear get your text
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let text = ViewControllerVar.inputText.text //your text
}

How would I go about adding a uiview vertically above a uiview within an inputAccessoryView?

I'm trying to add a uiview containing multiple buttons, above my current input accessory view. My current input accessory view is a growing textField (like iOS standard Text Message app).
class ViewController: UIViewController {
let textInputBar = ALTextInputBar()
// The magic sauce
// This is how we attach the input bar to the keyboard
override var inputAccessoryView: UIView? {
get {
return textInputBar
}
}
// Another ingredient in the magic sauce
override func canBecomeFirstResponder() -> Bool {
return true
}
}
Example of what I'm trying to do, the app (Facebook Messenger) has a growing textfield or textinput, and in this case, an bar of buttons bellow.
My current view, as mentioned earlier.
try this code
class ViewController: UIViewController {
#IBOutlet weak var myTextField: UITextField!
var textInputBar:UIView = ALTextInputBar()// if it is a uiview type
override func viewDidLoad() {
super.viewDidLoad()
myTextField.inputAccessoryView = textInputBar
}
}