Preventing button image highlight when selected swift 4 - swift

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.

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.

Toolbar doesn't center the button vertically

Can anyone explain to me why Xcode doesn't vertically center the button when you programmatically add the button to the toolbar? When I work with the toolbar added using the Storyboard, the button appears centered horizontally and vertically.
Toolbar added via a storyboard(looks perfect):
Toolbar added via a code:
My code:
self.navigationController?.isToolbarHidden = false
let testButton = UIButton(type: .custom)
testButton.setTitle("Test", for: .normal)
testButton.addTarget(self, action: #selector(test), for: .touchUpInside)
testButton.backgroundColor = .black
testButton.setTitleColor(.white, for: .normal)
testButton.titleLabel?.font = .boldSystemFont(ofSize: 17)
let tb = UIBarButtonItem(customView: testButton)
self.toolbarItems = [tb]

Use UINavigationBar appearance "back" icon for custom button?

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)
}

UIButton text not showing up

I am trying to create UIButton and display a text in it, as well as designate a font size but it will not show up in my Storyboard, using the setTitleColor and the titleLabel!. It will work if I comment those 2 lines out and uncomment the forgot.setTitle (That is currently commented out) however with that I cannot set the UIButton text fontsize. Any suggestions? I have added the subview to my view in my viewdidload.
let forgotPassword: UIButton = {
let forgot = UIButton()
forgot.translatesAutoresizingMaskIntoConstraints = false
//forgot.setTitle("Forgot your password?", for: .normal)
forgot.titleLabel! .font = UIFont(name:"Forgot your password?", size: 14)
forgot.setTitleColor(.white, for: .normal)
forgot.addTarget(self, action: #selector(forgot(_ :)), for: .touchUpInside)
forgot.sizeToFit()
//forgot.backgroundColor = .orange
return forgot
}()
You need to uncomment the line that sets the title - without a title it's not surprising that nothing turns up. You also need to change the font line - the name is meant to be the name of the font, not the text string to display. The below works:
let button = UIButton()
button.setTitle("Forgot your password?", for: .normal)
button.sizeToFit()
button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
button.setTitleColor(.white, for: .normal)
The button is not visible because it has no text.
The problem here is that these two lines do not actually set the text, only the appearance.
forgot.titleLabel!.font = UIFont(name: "System", size: 14) // This is the font, not the text itself
forgot.setTitleColor(.white, for: .normal)
You have to call this line as well to actually set any text to appear.
forgot.setTitle("Forgot your password?", for: .normal)

How to enable the userinteraction only in the barbutton item of the navigation bar?

I have an application which is having the new Facebook App like UI. In that, a side menu controller is there. It has a button on the navigation bar while clicking on to it will give u the menu. But my problem is in the sample I am using the navigation bar is also can be movable, I fix this by setting userinteractionenabled=no in the navigation bar. But I need to enable the userinteraction only in the buttons in the navigation bar. If I do like what I did the whole navigation bar is became not clickable. Can anybody help me in achieving this?
If you have custom button then use this
UIBarButtonItem *button = self.navigationItem.rightBarButtonItem;
button.enabled = NO;
This is the code I use. It doesn't quite enable user interaction, but I figured out how to set the title white then when clicked change colors. (look at setTitleColor lines
override func viewDidLoad() {
super.viewDidLoad()
let handleButton = UIButton.init(type: .custom)
button.setTitle("Register", for: .normal)
button.layer.backgroundColor = CGColor.init(gray: 0.0, alpha: 0.0)
button.setTitleColor(.white, for: .normal)
button.setTitleColor(.red, for: .highlighted)
button.layer.borderWidth = 1
button.layer.cornerRadius = 5
button.layer.borderColor = UIColor.white.cgColor
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
button.addTarget(self, action: #selector(didTapRegister), for: .touchUpInside)
if let button = self.navigationItem.rightBarButtonItem?.customView {
button.frame = CGRect(x:0, y:0, width:80, height:34)
}