Remove UIButton highlight for all buttons in an app - ios

I'm wondering if there's a way to remove the highlight from UIButtons in my app. I know how to do it for an individual button (adjustsImageWhenHighlighted) but I don't want to go through all the buttons in my app and do it. Is it possible to set adjustsImageWhenHighlighted to false in the App Delegate so it applies to all buttons?

Put this code in didFinishLaunchingWithOptions
UIButton.appearance().adjustsImageWhenHighlighted = false
Then all the default value adjustsImageWhenHighlighted of UIButton is false

private let button: NoHighlightButton = NoHighlightButton()
class NoHighlightButton: UIButton {
override var highlighted: Bool {
didSet{
super.highlighted = false
}
}
}

Related

Hide status bar on button tap Swift 3 / iOS 10

I cannot seem to be able to hide the status bar in my root view controller (A) when a button is pressed.
I have set the properties in my info.plist Status bar is initially hidden and View controller-based status bar appearance to YES.
If I implement the override var prefersStatusBarHidden: Bool { get }, the status bar will be definitively hidden (or not).
What I need
I want the status bar to be displayed in (A) but hidden when I press a button that adds a child view controller to (A).
I have tried setting prefersStatusBarHidden to false and hide it using UIApplication.shared.isStatusBarHidden = true when the button is pressed but this does not work.
There must be something I am getting wrong, could anyone enlighten me ?
Thanks in advance.
PS: I merely need it to be invisible, not necessarily "hidden" in the Swift sense.
To hide the status bar
UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelStatusBar
To bring back the status bar
UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelNormal
You can create a variable to hold a boolean to set if the status bar is hidden or not
var _isStatusBarHidden: Bool = false
Then create its getter and setter
var isStatusBarHidden: Bool {
get{
return _isStatusBarHidden
}
set {
if _isStatusBarHidden != newValue {
_isStatusBarHidden = newValue
self.setNeedsStatusBarAppearanceUpdate()
}
}
}
You must override the prefersStatusBarHidden property
override var prefersStatusBarHidden: Bool {
return self._isStatusBarHidden
}
Last thing is to set the variable to true when the user presses the button:
self.isStatusBarHidden = true
Just a little improvement of #Alexandre Lara `s code:
var isStatusBarHidden: Bool = false {
didSet {
if oldValue != self.isStatusBarHidden {
self.setNeedsStatusBarAppearanceUpdate()
}
}
}
override var prefersStatusBarHidden: Bool {
return self.isStatusBarHidden
}

How do I activate an inputView using a UIButton in Swift?

I am attempting to have a UIDatePicker come up as a keyboard when the user hits a UIButton. I was able to get it to work with a textfield, but I don't like how the cursor is visible and the user could enter in any text if they had an external keyboard. Here is my code:
#IBAction func dateFieldStart(sender: UITextField) {
var datePickerStartView : UIDatePicker = UIDatePicker()
datePickerStartView.datePickerMode = UIDatePickerMode.Time
sender.inputView = datePickerStartView // error when sender is UIButton
}
I tried changing the sender to UIButton but it gave this error on the line that is marked above:
Cannot assign to 'inputView' in 'sender'
I have tried researching it and no one else seems to have had a problem with it. Anyone know how to trigger a UIDatePicker inputView using a UIButton or anything that might work better that the user cannot type into? Thanks!
This is years after the original question, but for anyone who may be looking for solution to this you can subclass UIButton and provide a getter and setter for the inputView property. Be sure to call becomeFirstResponder in the setter and override canBecomeFirstResponder. For example:
class MyButton: UIButton {
var myView: UIView? = UIView()
var toolBarView: UIView? = UIView()
override var inputView: UIView? {
get {
myView
}
set {
myView = newValue
becomeFirstResponder()
}
}
override var inputAccessoryView: UIView? {
get {
toolBarView
}
set {
toolBarView = newValue
}
}
override var canBecomeFirstResponder: Bool {
true
}
}
let tempInput = UITextField( frame:CGRect.zero )
tempInput.inputView = self.myPickerView // Your picker
self.view.addSubview( tempInput )
tempInput.becomeFirstResponder()
It's a good idea to keep a reference to tempInput so you can clean-up on close
I wanted to do the same thing, I ended up just overlaying a UITextField over the button and using the inputView of that instead.
Tip: set tintColor of the UITextField to UIColor.clearColor() to hide the cursor.
You can create a view for the picker off screen view and move it on screen when you need it. Here's another post on this.

Disabling user input for UITextfield in swift

pretty trivial question, I know. But I can not find anything online.
I need to disable the user from being able to edit the text inside of a text field. So that when the click on the text, a keyboard doesn't show up.
Any ideas?
A programmatic solution or if it is possible through storyboards would be great.
Try this:
Swift 2.0:
textField.userInteractionEnabled = false
Swift 3.0:
textField.isUserInteractionEnabled = false
Or in storyboard uncheck "User Interaction Enabled"
Another solution, declare your controller as UITextFieldDelegate, implement this call-back:
#IBOutlet weak var myTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
myTextField.delegate = self
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
if textField == myTextField {
return false; //do not show keyboard nor cursor
}
return true
}
In storyboard you have two choise:
set the control's 'enable' to false.
set the view's 'user interaction enable' false
The diffierence between these choise is:
the appearance of UITextfild to display in the screen.
First is set the control's enable. You can see the backgroud color is
changed.
Second is set the view's 'User interaction enable'. The backgroud color is NOT changed.
Within code:
textfield.enable = false
textfield.userInteractionEnabled = NO
Updated for Swift 3
textField.isEnabled = false
textfield.isUserInteractionEnabled = false
If you want to do it while keeping the user interaction on.
In my case I am using (or rather misusing) isFocused
self.myField.inputView = UIView()
This way it will focus but keyboard won't show up.
I like to do it like old times. You just use a custom UITextField Class like this one:
//
// ReadOnlyTextField.swift
// MediFormulas
//
// Created by Oscar Rodriguez on 6/21/17.
// Copyright © 2017 Nica Code. All rights reserved.
//
import UIKit
class ReadOnlyTextField: UITextField {
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
override init(frame: CGRect) {
super.init(frame: frame)
// Avoid keyboard to show up
self.inputView = UIView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Avoid keyboard to show up
self.inputView = UIView()
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
// Avoid cut and paste option show up
if (action == #selector(self.cut(_:))) {
return false
} else if (action == #selector(self.paste(_:))) {
return false
}
return super.canPerformAction(action, withSender: sender)
}
}
In swift 5, I used following code to disable the textfield
override func viewDidLoad() {
super.viewDidLoad()
self.textfield.isEnabled = false
//e.g
self.design.isEnabled = false
}
Swift 4.2 / Xcode 10.1:
Just uncheck behavior Enabled in your storyboard -> attributes inspector.
you can use UILabel instead if you don't want the user to be able to modify anything in your UITextField
A programmatic solution would be to use enabled property:
yourTextField.enabled = false
A way to do it in a storyboard:
Uncheck the Enabled checkbox in the properties of your UITextField
textField.isUserInteractionEnabled = false

Disable an IBAction

I've created an IBAction from a button on the storyboard. After it is clicked, I want to disable it so its no longer functional. How can I do this?
#IBAction func b1(sender: AnyObject) {
displayLabel(1)
}
Create an outlet for your button and set its enabled property to false in your b1 func. Say your outlet is "button1":
button1.enabled = false
#Mike Taverne is right about disabling button. But from the line
button.enabled = false
will make button dark greyed out. And user clearly see that it is not tappable or clickable.
But if we use this line:
button.userInteractionEnabled = false
Then button UI will not change and user thinks that it is tappable. But related action event will not called.
In both case action will not called but the look of button will matter here.
Code will suppose to be like:
#IBAction func b1(sender: AnyObject) {
var view = UIView()
view = sender as UIView
view.userInteractionEnabled = false
}
You will get the button instance in that function as sender (AnyObject).
You need to convert it to UIButton and set it's userInteractionEnabled or enabled property to false.
#IBAction func b1(sender: AnyObject)
{
displayLabel(1)
var button = sender as UIButton;
button.userInteractionEnabled = false;
}
Alternatively, you can do it using:
#IBAction func b1(sender: UIButton)
{
displayLabel(1)
sender.enabled = false;
}

Disable touches but not all user interaction on UITextField

I have a UITextField but I want to be in control of when it is focussed and when it is unfocussed.
In order to achieve this I need to be able to block touch events on that text field. I know I could just put a button in front of it and manually do it, but I wanted to know if there was any way to just disable touches on the element.
Many thanks.
There does not appear to be a way to block touches with a property of UITextField. I solved this problem by placing a UIView over the text field to block the touches. I set the UIView to "Clear Color" using the attributes inspector. Works great for my application.
How about the userInteractionEnabled property?
I had to have cursor interaction while editing, so before becomeFirstResponder() i set isUserInteractionEnabled = true and on end editing set it on false
#IBAction func editTapped(_ sender: UIButton) {
textField.isUserInteractionEnabled = true
textField.becomeFirstResponder()
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
print(textField.textValue)
textField.isUserInteractionEnabled = false
return true
}

Resources