UITextField clear button color Swift - swift

How can I change clear button color or background color in UITextField. I tried get subviews of textField, but in subviews not clear button.

import UIKit
class CustomTextField: UITextField {
override func layoutSubviews() {
super.layoutSubviews()
for view in subviews {
if let button = view as? UIButton {
button.setImage(button.image(for: .normal)?.withRenderingMode(.alwaysTemplate), for: .normal)
button.tintColor = .white
}
}
}
}

Related

Swift: programmatically locate a UITextFelds UILabel and containing UIView container

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.

Set background color of text view based on whether it is the first responder

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.

All objects disappear in view controller (main storyboard) when I use custom #IBDesignable UITextfield class. (Xcode 10, Swift 4.2)

All objects disappear in view controller (main storyboard) when I use custom #IBDesignable UITextfield class. Main storyboard looks like this:
(Xcode 10, Swift 4.2)
As you see, it shows an error also. But Xcode shows 'Build Succeeded' when I run the app.
I use custom #IBDesignable UITextfield class like this below:
import UIKit
import AKMaskField
#IBDesignable
class MyTextFieldStyle: AKMaskField {
#IBInspectable var secureEntry : Bool = false
#IBInspectable var myBorderColor : UIColor = UIColor.white {
didSet {
self.layer.borderColor = myBorderColor.cgColor
}
}
#objc func secureButtonPressed (sender: UIButton) {
self.secureEntry = !self.secureEntry
self.isSecureTextEntry = self.secureEntry
}
override func awakeFromNib() {
super.awakeFromNib()
if self.secureEntry {
let btn = UIButton(type: .custom)
btn.setImage(#imageLiteral(resourceName: "eye-17-glyph-16"), for: .normal)
btn.addTarget(self, action: #selector(self.secureButtonPressed(sender:)), for: .touchUpInside)
btn.sizeToFit()
btn.frame = CGRect(x: self.frame.size.width-(btn.frame.size.width+5), y: (self.frame.size.height-btn.frame.size.height)/2, width: btn.frame.size.width, height: btn.frame.size.height)
self.addSubview(btn)
}
self.layer.borderColor = self.myBorderColor.cgColor
}
}
It becomes fine when I close and open Xcode. It looks like a bug. This was working fine.

Extension vs Subclass. How to explain different behavior when I set background color for UIButton

I want to change background color for UIButton in highlighted state.
I use two different ways.
Case #1 with extension:
extension UIButton {
override open func awakeFromNib() {
super.awakeFromNib()
backgroundColor = .green
setTitleColor(.white, for: .normal)
}
override open var isHighlighted :Bool {
didSet{
if isHighlighted {
backgroundColor = .red
} else {
backgroundColor = .green
}
}
}
}
Case #2 with subclass:
class SHButton: UIButton {
override open func awakeFromNib() {
super.awakeFromNib()
backgroundColor = .green
setTitleColor(.white, for: .normal)
}
override open var isHighlighted :Bool {
didSet{
if isHighlighted {
backgroundColor = .red
} else {
backgroundColor = .green
}
}
}
}
I use very similar code, but I see different results in runtime.
In second case button title has not a white color.
Why we can see so different results in runtime?
Updated. This question has been resolved. I'm dummy. I forgot to change button Type to Custom. When I have did it both examples began to work identically.
this should also fix your problem
button.setTitleColor(.white, for: .highlighted)

Swift UIButton not appearing on screen

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.