Use UINavigationBar appearance "back" icon for custom button? - swift

I need to achieve a custom navigation bar's "< back" button as seen in the push segue, but on a modal.
I want to avoid too much hard-coding and found out about the property UINavigationBar.appearance().backIndicatorImage and UINavigationBar.appearance().backIndicatorTransitionMaskImage.
I want to use these to put my own text beside them for my button, since using a .png doesn't look as natural as the real thing.
I tried this but the UIImage from those properties returns as nil.
func addBackButton() {
let backButton = UIButton(type: .custom)
backButton.setImage(UINavigationBar.appearance().backIndicatorImage, for: .normal)
backButton.imageView?.contentMode = .scaleAspectFit
backButton.setTitle("Back", for: .normal)
backButton.setTitleColor(backButton.tintColor, for: .normal)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
}

UINavigationBar.appearance().backIndicatorImage is an optional value, therefore you won't be able to get the system default chevron from this. Rather, the system will use the image provided here if not null, otherwise revert to the system default.
If targeting iOS 13+, you can make use of Apple's SF Symbols, in particular the back button icon is referred to as chevron.left. To use this, call UIImage(systemName: "chevron.left"). For earlier versions of iOS, you'll have to use an image set asset. You could target all versions of iOS using if #available(iOS 13.0, *) { ... } else { ... }, where you display the system image if on iOS 13+ for improved UI appearance.
func addBackButton() {
let backButton = UIButton(type: .custom)
if #available(iOS 13.0, *) {
backButton.setImage(UIImage(systemName: "chevron.left"), for: .normal)
}
else {
backButton.setImage(UIImage(named: "backChevon"), for: .normal)
}
backButton.imageView?.contentMode = .scaleAspectFit
backButton.setTitle("Back", for: .normal)
backButton.setTitleColor(backButton.tintColor, for: .normal)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
}

Related

How can I change the placeholder (label) color for MDCOutlinedTextFields?

I'm having a hard time changing the text color for the placeholder label that is used as a hint when not in focus and as a label on top when in focus. (Photo below)
The is reason I'm switching out all my MDC-TextFields and MDC-TextInputControllers is because they are all being deprecated for the New MDC-Outlined Textfields.
The code below is a function within an extension that would simply setup the general background & sub-label colors for all MDC-Outlined Textfields throughout the app.
I have tried a number of functions with no luck(commented out below).
extension MDCOutlinedTextField {
func setUpGeneralBackgroundColors(){
//Text color
self.setTextColor(UIColor.white, for: .normal)
self.setTextColor(UIColor.white, for: .editing)
//Border color
self.setOutlineColor(UIColor.white, for: .normal)
self.setOutlineColor(UIColor.white, for: .editing)
//self.setFloatingLabelColor(UIColor.white, for: .normal)
//self.setFloatingLabelColor(UIColor.white, for: .editing)
//self.setFloatingLabelColor(UIColor.white, for: .disabled)
// self.setNormalLabelColor(UIColor.purple, for: .normal)
// self.setNormalLabelColor(UIColor.purple, for: .editing)
// self.setNormalLabelColor(UIColor.purple, for: .disabled)
// self.label.tintColor = UIColor.purple
//self.label.textColor = UIColor.systemPink
//self.label.shadowColor = UIColor.cyan
//self.label.backgroundColor = UIColor.red
//Changes icon colors within the text field if any
self.tintColor = .green
}
}
I figured the problem, in the storyboard where I have these new textfields I had a raw string for the placeholder which override the swift code behind the scenes and prevented me from changing colors.
Note: This problem will NOT occur with the now deprecated MDC-Textfields.
If you guys have a placeholder value in the storyboard when using these MDC-Outlined Textfields in storyboards, (not to mixed up with the swift code) then get rid of them.

How can I change my custom cell background when pressing a button?

I have a CollectionView and a Button inside of an UIView. The purpose of the button is change the theme of the app, and I can change all colors, except for the backgroundColor of a nameContainerView on my TatodexCell (a custom cell).
I can't figure out how to access that property, and then, change the color I want.
THE FOLLOWING CODE IS LOCATED ON MY TatodexController FILE
My button property is:
let buttonChangeTheme: UIButton? = {
let button = UIButton()
button.setTitle("Change to blue theme", for: .normal)
button.addTarget(self, action: #selector(themeButtonClicked), for: .touchUpInside)
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.layer.borderWidth = 3
button.layer.borderColor = Colors.mainBlack?.cgColor
button.backgroundColor = Colors.darkBlue
button.tintColor = Colors.mainWhite
return button
}()
My button-func is this one:
#objc func themeButtonClicked() {
clickCheck = !clickCheck
if clickCheck {
navigationController?.navigationBar.barTintColor = Colors.lightBlue
collectionViewPokemon?.backgroundColor = Colors.darkBlue
buttonChangeTheme?.backgroundColor = Colors.darkRed
buttonChangeTheme?.setTitle("Return to red theme", for: .normal)
}
else {
navigationController?.navigationBar.barTintColor = Colors.lightRed
collectionViewPokemon?.backgroundColor = Colors.darkRed
buttonChangeTheme?.backgroundColor = Colors.darkBlue
buttonChangeTheme?.setTitle("Change to blue theme", for: .normal)
}
}
THE FOLLOWING CODE IS LOCATED ON MY TatodexCell FILE
The nameContainerView I wanna modify it's bg color is:
lazy var nameContainerView: UIView = {
let nameView = UIView()
nameView.backgroundColor = Colors.lightRed
nameView.addSubview(nameLabel)
nameLabel.center(inView: nameView)
return nameView
}()
My goal is to change nameView.backgroundColor = Colors.lightRed to nameView.backgroundColor = Colors.lightBlue, but I can't access that property.
I would really appreciate any help or advice. Perhaps the solution is hiding in plain sight, but I've tried many ways and none of them worked. Let me know if another chunk of code is needed to be shown.
If you implement the tintColorDidChange() method in your custom collection view cell, and in that method, you set the cell's background color to the tint color, then when you change the owning UICollectionView's tint color, the cells's tintColorDidChange() methods will fire and you'll see the change.

Preventing button image highlight when selected swift 4

I do my swift project using code only no storyboards, so no storyboard solutions please I have an agree to terms button and when selected the checkmark is covered in the tint color I am trying to prevent the tint color highlighting the image when selected I am not sure why this is happening here is the button code
let agreeToTermsAndConditions : UIButton = {
let button = UIButton(type: .system)
button.tintColor = .gray
button.backgroundColor = .white
button.adjustsImageWhenHighlighted = false
button.setImage(UIImage(named:"unchecked" )!, for: .normal)
button.setImage(UIImage(named:"agreechecked" )!, for: .selected)
button.addTarget(self, action: #selector(userAgreedToTerms), for: .touchUpInside)
return button
}()
The code is running on xcode 10.2.1 using swift 4 and iOS 12 deployment
Change
UIButton(type: .system)
to
UIButton(type: .custom)
Now you have complete control over the look and behavior of the button.

how to change bordercolor when Button.isHighlighted

I wonder how I get my border around my UIButton to change opacity together with the text inside it, when it is either clicked or highlighted.
My logic tells me, that it should be something like this.. but it doesn't seem to work:
//BTN STYLING
btnstd.layer.cornerRadius = 5
btnstd.layer.borderWidth = 1.5
btnstd.layer.borderColor = UIColor.white.cgColor
//Change bordercolor when highlighted
if(btnstd.isHighlighted) {
btnstd.layer.borderColor = UIColor(white:1,alpha:0.3).cgColor
}
This is by the way put inside my ViewDidLoad() function
The actions you are looking for are .touchDown and anything .touchUp:
override func viewDidLoad() {
super.viewDidLoad()
theButton.setTitle("Normal", for: .normal)
theButton.setTitle("Highlighted", for: .highlighted)
theButton.layer.borderColor = UIColor.red.cgColor
theButton.layer.borderWidth = 1
theButton.addTarget(self, action: #selector(startHighlight), for: .touchDown)
theButton.addTarget(self, action: #selector(stopHighlight), for: .touchUpInside)
theButton.addTarget(self, action: #selector(stopHighlight), for: .touchUpOutside)
}
func startHighlight(sender: UIButton) {
theButton.layer.borderColor = UIColor.green.cgColor
theButton.layer.borderWidth = 1
}
func stopHighlight(sender: UIButton) {
theButton.layer.borderColor = UIColor.red.cgColor
theButton.layer.borderWidth = 1
}
It depends on what you are trying to do.
Case #1: You want this change to happen when the button is highlighted, but in a normal state have a different set of properties.
let theButton = UIButton()
// set common properties and layout code
theButton.setTitle("Normal", for: .normal)
theButton.setTitle("Highlighted", for: .highlighted)
In addition, you have setTitleColor(), setAttributedTitle, setTitleShadowColor(), setImage(), and setBackgroundImage() that you can code directly.
Border color in this case would need a subclass (not an extension, you want public properties) where you will check self.layer.hitTest() after wiring up a tap gesture on self.
Case #2: You want the button state to change when clicked, and stay changed.
You are part way there. If you supply the button in IB, make sure you add an IBAction for event touchUpInside. If you are working in code, here's the Swift 3 syntax.
theButton.addTarget(self, action: #selector(changeButton), for: .touchUpInside)
func changeButton(sender: UIButton) {
sender.setTitle("New Title", for: .normal)
sender.layer.borderColor = UIColor.red.cgColor
}
My preference (but only that) is to more strongly-type the sender (I think that's the correct term) for my actions. I'm sure there are pros and cons for using a specific sender (like UIButton) over AnyObject, but in this case I think the biggest reason is you don't need to force-cast the sender to UIButton.

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.