I have a form with a UIView wrapping around a UILabel and UITextField. When a user enters the field I would like to change the colour of the label and the border colour of the view container.
If I call a function on the firstResponder I will need to find the text field's corresponding label and view copntainer.
I thought to have a firstResonder function for each field and in each function send the corresponding outlets (textfield, label, view) to a function which handles the colour changes for the label and view border.
This is not terrible but I and sure this can be accomplished more efficiently.
Any pointers please.
edit:
I changed my requirement slightly to place the label inside the textfield and highlight the textfield border instead of the encapsulating view.
This is what I ended up with:
override func viewDidLoad() {
super.viewDidLoad()
firstNameLabel.connect(with: firstName)
}
extension UILabel {
#objc
func editingChanged(textField: UITextField) {
}
#objc
func editingDidBegin(textField: UITextField) {
self.textColor = UIColor.blue
textField.layer.borderColor = UIColor.blue.cgColor
}
#objc
func editingDidEnd(textField: UITextField) {
self.textColor = UIColor.green
textField.layer.borderColor = UIColor.green.cgColor
}
func connect(with textField:UITextField){
//textField.addTarget(self, action: #selector(UILabel.editingChanged(textField:)), for: .editingChanged)
textField.addTarget(self, action: #selector(UILabel.editingDidBegin(textField:)), for: .editingDidBegin)
textField.addTarget(self, action: #selector(UILabel.editingDidEnd(textField:)), for: .editingDidEnd)
textField.layer.borderColor = UIColor.gray.cgColor
textField.layer.borderWidth = 1;
textField.layer.cornerRadius=10
}
}
The usual thing is to give each member of each group a corresponding tag. Since viewWithTag drills down to find any view with the given tag, the problem is solved if you know how to convert the tag value of the view you have to the tag value of the view you want.
For example, text field 10, view 110, label 210; text field 11, view 111, label 211; and so on. Or whatever system suits your fancy.
Alternatively just walk the view hierarchy. The view is the text field's superview, and the label is the first subview of the view that is a label.
Related
I want to animate a card when pressed.
I made a table view, and added a custom cell to it.
and in custom cells, I created a UIButton and applied .touchUpInside gesture on the button.
when the button is pressed, the background color of the card will turn to red.
but weird thing is, if I press the first button,
6th, 10th, 15th and more cards' background-color also changed.
I check the function called only once but it happens to several cards.
I assume this might be related to dequeueReusableCell cell but not sure...
can anybody please help me out?
#objc func cardPressed(){
changeCardBackground()
}
private func changeCardBackground(){
var color:UIColor = .red
if(isCardRed){
color = .white
}
isCardRed = !isCardRed
UIView.animate(withDuration: 0.3) {
self.cardButton.backgroundColor = color
}
}
TableviewCells are reused you need to add prepareForReuse Method like :
override func prepareForReuse() {
super.prepareForReuse()
self.cardButton.backgroundColor = defaultColor
}
How can you set the background color of a text view based on whether it is the first responder?
It looks like a variation of this has been answered but it's in Objective C. I'm hoping there's a swift equivalent that constantly checks the state of the text view. Here's my code:
if messageInputBar.inputTextView.isFirstResponder {
messageInputBar.inputTextView.backgroundColor = .clear
} else {
messageInputBar.inputTextView.backgroundColor = .white
}
Implement the textViewDidBeginEditing and textViewDidEndEditing delegate methods. Update the text view's backgroundColor as desired.
func textViewDidBeginEditing(_ textView: UITextView) {
textView.backgroundColor = .blue
}
func textViewDidEndEditing(_ textView: UITextView) {
textView.backgroundColor = .white
}
And don't forget to set the delegate of each text view.
I have a collection view with multiple cells in it, I figured out how to add a border to each cell but still stuck with adding a border to the CollectionView (expected Result).
Currently, I'm placing the button outside the CollectionView but don't know how to make it looks like the expected result.
For set the collection view border you can follow this
collectionView.layer.borderColor = UIColor.gray.cgColor
collectionView.layer.borderWidth = 5
And for setting cell position to the middle you need to set the cell insect.
You can easily set the cell insect from storyboard from this. Update the Section Insects value that you want.
you can try like this:
#IBOutlet var CollectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
CollectionView.delegate = self
CollectionView.dataSource = self
CollectionView.layer.borderColor = UIColor.gray.cgColor //change to required color
CollectionView.layer.borderWidth = 5 //change to required borderwidth
}
Hello I am trying to have a number pad appear after a timer is up. Then have my user type numbers on the pad and their input be saved to a variable in my code not a text box. I can't seem to find anything on popping up a number pad without using a text box. Any help is appreciated.
Ok I'm going to give you some code that will greatly help you. You need some sort of UITextView or UITextField to get the system keyboard. So essentially what we will do is have a textField without showing it, and then grab the info off it and store it into the variable.
//Dummy textField instance as a VC property.
let textField = UITextField()
//Add some setup to viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self //Don't forget to make vc conform to UITextFieldDelegateProtocol
textField.keyboardType = .phonePad
//http://stackoverflow.com/a/40640855/5153744 for setting up toolbar
let keyboardToolbar = UIToolbar()
keyboardToolbar.sizeToFit()
let flexBarButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneBarButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismissKeyboard))
keyboardToolbar.items = [flexBarButton, doneBarButton]
textField.inputAccessoryView = keyboardToolbar
//You can't get the textField to become the first responder without adding it as a subview
//But don't worry because its frame is 0 so it won't show.
self.view.addSubview(textField)
}
//When done button is pressed this will get called and initate `textFieldDidEndEditing:`
func dismissKeyboard() {
view.endEditing(true)
}
//This is the whatever function you call when your timer is fired. Important thing is just line of code inside that our dummy code becomes first responder
func timerUp() {
textField.becomeFirstResponder()
}
//This is called when done is pressed and now you can grab value out of the textField and store it in any variable you want.
func textFieldDidEndEditing(_ textField: UITextField) {
textField.resignFirstResponder()
let intValue = Int(textField.text ?? "0") ?? 0
print(intValue)
}
I am using storyboard and this is what I did:
drag-drop a text field
On the storyboard, in the attributes inspector (having selected the text field), under "Drawing", select hidden
make an outlet for the text field in your view controller
make sure your view controller extends the UITextViewDelegate
make your current view controller the delegate
in the required location simply call <textfieldOutlet>.becomeFirstResponder()
Now that this is simply a textfield's data, u can always store the value and use it else where.
I have a view in my tabbar controller where I would like to show a button. I create this button programmatically based of a condition, therefore I use the following code but nothing is appearing:
override func viewDidLoad() {
super.viewDidLoad()
if !Settings.getIsConnected() {
notConnected()
}
}
func notConnected() {
let connectBtn = UIButton(frame: CGRect(x: self.view.center.x, y: self.view.center.y, width: 200, height: 45))
connectBtn.setTitle("Connect", forState: .Normal)
connectBtn.addTarget(self, action:#selector(self.pressedConnect(_:)), forControlEvents: .TouchUpInside)
self.view.addSubview(connectBtn)
print("Button created")
}
func pressedConnect(sender: UIButton!) {
}
I am clueless on what I am doing wrong. Anyone got suggestions? Cause it does print out "Button created" so it definitely runs the code inside the noConnected() method.
Add a background color to your UIButton and add a tint color to the title. This will resolve the problem
Try moving the code to viewDidAppear and see if the button is showing up.
The frame is not correctly set when in viewDidLoad. Use the method viewDidLayoutSubviews for the earliest possible time where the frame is correctly setup for a ViewController.
With this code change, you will need some additional logic for when your button should be added as a subview though.
A programmatically created button may not show up because of more reasons, e.g:
the tint color is not set
the background color is not set
the button is not added to the view hierarchy
the button is hidden
In your case, you should change the tint color or the background color of your button.
E.g.:
Swift 4.2:
private lazy var connectButton: UIButton = {
let button = UIButton(type: .custom)
button.backgroundColor = .green
button.setTitleColor(.black, for: .normal)
button.setTitle(NSLocalizedString("Connect", comment: ""), for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(connectButton)
}
You can re-check the button properties in the storyboard that it is not hidden.