I created a textfield and a button for the eye.
The privacy eye is superimposed on the password when it reaches more than 35 characters.
Do you have any idea how to solve this problem? here is my code for the button
public var pincodeVisibilityButton: UIButton = {
let button = UIButton()
button.setTitleColor(.SEBlack, for: .normal)
button.titleLabel?.font = UIFont.icon(ofSize: 34)
button.translatesAutoresizingMaskIntoConstraints = false
button.isUserInteractionEnabled = true
return button
}()
It is best to use built in functionality.
Reference: https://developer.apple.com/documentation/uikit/uitextfield
Sample code is below:
let overlayButton = UIButton(type: .custom)
let bookmarkImage = UIImage(systemName: "bookmark")
overlayButton.setImage(bookmarkImage, for: .normal)
overlayButton.addTarget(self, action: #selector(displayBookmarks),
for: .touchUpInside)
overlayButton.sizeToFit()
// Assign the overlay button to the text field
textField.rightView = overlayButton
textField.rightViewMode = .always
Refer:
Related
I just created a UIButton and I want to do some action when I click on it i don't know how to do it , here is my way to just create uibutton only !!!:
lazy var test: UIButton = {
let test = UIButton()
test.translatesAutoresizingMaskIntoConstraints = false
test.setTitle("See More Answers", for: .normal)
test.setTitleColor(.systemBlue, for: .normal)
return seeMoreBtn
}()
The modern way is to add the action as a UIAction.
lazy var test: UIButton = {
let test = UIButton()
test.translatesAutoresizingMaskIntoConstraints = false
test.setTitle("See More Answers", for: .normal)
test.setTitleColor(.systemBlue, for: .normal)
let action = UIAction { action in
print("howdy!")
}
test.addAction(action, for: .touchUpInside)
return test
}()
Nicer syntax can be achieved through an extension, as I demonstrate here.
lazy var test: UIButton = {
let test = UIButton()
test.translatesAutoresizingMaskIntoConstraints = false
test.setTitle("See More Answers", for: .normal)
test.setTitleColor(.systemBlue, for: .normal)
test.addTarget(self, action: #selector(self.buttonPressedAction), for: .touchUpInside) // add target
return seeMoreBtn
}()
#objc func buttonPressedAction() {
//This function will get called when u press the button.
//include here what u want to do.
}
#objc func buttonAction() {
print("Button Tapped")
}
test.addTarget(self, action: #selector(buttonAction(_:)), for: .touchUpInside)
I have given the example for create the custom button and the custom label and set constraint in coding. The below code also contains the programmatically button action.
import UIKit
class ViewController: UIViewController {
let button = UIButton(frame: CGRect(x: 100,y: 400,width: 200,height: 60))
var label = UILabel(frame: CGRect(x: 100, y: 200, width: 200, height: 60))
var count : Int = 0
override func viewDidLoad() {
super.viewDidLoad()
button.setTitle("Click Button",for: .normal)
button.backgroundColor = UIColor.blue
button.setTitleColor(.white, for: .normal)
button.addTarget(self,action: #selector(buttonAction),for: .touchUpInside)
label.font = .systemFont(ofSize: 50)
label.backgroundColor = UIColor.gray
label.textAlignment = .center
self.view.addSubview(button)
self.view.addSubview(label)
}
#objc
func buttonAction() {
self.count += 1
self.label.text = "\(count)"
}
}
Output :-
Value of label increases when the button is click.
One day i did button with title label, subtitle label and image in storyboard and got this effect
Image of button in storyboard
but now when i am trying to do this in code programmly i have a problem, i dont see subtitle..
private var firstPaidPack: UIButton = {
let button = UIButton(type: .custom)
let image = UIImage(systemName: "circle")
button.layer.cornerRadius = 15
button.backgroundColor = UIColor(white: 1, alpha: 0.3)
button.setImage(image, for: .normal)
button.setTitle("1.99$", for: .normal)
button.subtitleLabel?.textAlignment = .center
button.subtitleLabel?.text = "subtitle text to check how it..."
button.titleLabel?.font = UIFont(name: "Arial", size: screenWidth / 375 * 19)
return button
}()
You need to create a UIButton.Configuration and set the subtitle on the configuration object:
var config = UIButton.Configuration.tinted()
config.subtitle = "subtitle text to check how it..."
// Set title and all other properties on the configuration object...
let button = UIButton(type: .custom)
button.configuration = config
You can also set the title and all other properties on the configuration object instead.
Try the below tricks will help you to change the subtitle of the button.
if #available(iOS 15.0, *) {
button.configuration?.subtitle = "Your Subtitle"
}
Have a look here for a detailed answer.
let historyButton: UIButton = {
let button = UIButton(type: .system)
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = UIColor(named: "AccentColor")
button.setTitle("History", for: .normal)
button.setTitleColor(.white, for: .normal)
button.layer.cornerRadius = 30 / 2
button.titleLabel?.font = Constants.Fonts.bold(size: 12)
button.addTarget(.none, action: #selector(historyPressed), for: .allTouchEvents)
return button
}()
fileprivate func setupNavBar() {
navigationItem.title = "Introductions"
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: historyButton)
}
the button responds normally except the function "historyPressed" never gets executed
I am trying to create/show a label when a button is pressed, and then delete/hide the same label when the same button is pressed again. I am trying to do this all programmatically in Swift.
I have tried using label.removeFromSuperview() but it doesn't seem to have any effect. However when i try removing the button in the same code location using button.removeFromSuperview()
var label = UILabel()
let labelImage = UIImage(named: "Strike Line.png")
/* to select checkmarked state */
func pressCheck() {
let image = UIImage(named: "Checkmark.png")
button.setBackgroundImage(image, for: UIControlState.normal)
button.addTarget(self, action:#selector(self.pressUnCheck), for: .touchUpInside)
self.view.addSubview(button)
textField1.textColor = UIColor.gray //change textfield to a gray color
label = UILabel(frame: CGRect(x : 31, y : 69, width: 200, height: 2))
label.backgroundColor = UIColor(patternImage: labelImage!)
self.view.addSubview(label)
}
func pressUnCheck()
{
let image = UIImage(named: "To Be Completed Circle.png")
button.setBackgroundImage(image, for: UIControlState.normal)
button.addTarget(self, action:#selector(self.pressCheck), for: .touchUpInside)
self.view.addSubview(button)
label.removeFromSuperview()
textField1.textColor = UIColor.black
}
Here is where i am trying to remove/hide the label.
Since this apparently was the fix I'll drop it in as an answer.
Add button.removeTarget(nil, action: nil, for: .allEvents) before you add any new targets to your button.
If you don't remove the current target it'll have multiple targets and be calling both pressCheck() and pressUnCheck() on each button press.
There's a few ways to handle this... If you just want to hide it you can use
label.isHidden = true - would hide the label.
label.isHidden = false - would show the label.
In the following code I have three buttons that show when an inputField is tapped. Right now the buttons show but all of them are shifted to the left and what I would like to do is have them evenly space and keep them like that regardless of what phone size they are viewed on.
How can I evenly space the buttons in a UIToolbar?
CODE:
override func viewDidLoad() {
super.viewDidLoad()
addButtonsToKeyboard()
}
// Reusable Button
func configurButton(button:UIButton) {
button.backgroundColor = UIColor.whiteColor()
button.layer.cornerRadius = 5
button.layer.borderWidth = 1.0
button.layer.borderColor = UIColor.blueColor().CGColor
button.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
button.setTitleColor(UIColor.blueColor(), forState: .Highlighted)
button.frame = CGRect(x:0, y:0, width:50, height:35)
button.addTarget(self, action: #selector(customKeyPressed), forControlEvents: UIControlEvents.TouchUpInside)
}
func addButtonsToKeyboard(){
// First button
let button1 = UIButton()
button1.setTitle("Btn 1", forState: .Normal)
configureButton(button1)
let barButton1 = UIBarButtonItem()
barButton1.customView = button1
// Second button
let button2 = UIButton()
button2.setTitle("Btn 2", forState: .Normal)
configureButton(button2)
let barButton2 = UIBarButtonItem()
barButton2.customView = button2
// Third button
let button3 = UIButton()
button3.setTitle("Btn 3", forState: .Normal)
configureButton(button3)
let barButton3 = UIBarButtonItem()
barButton3.customView = button3
/**
* UIToolbar.
*/
let toolBar = UIToolbar()
toolBar.tintColor = UIColor.redColor()
toolBar.barTintColor = UIColor.lightGrayColor()
toolBar.items = [barButton1, barButton2, barButton3]
toolBar.sizeToFit()
myInputField.inputAccessoryView = toolBar
}
func customKeyPressed(sender: UIButton){
// do something
}
CURRENT:
AFTER:
Very simple, you just need to add a flexible bar button item between each button. See the following:
let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
toolbar.items = [barButton1, space, barButton2, space, barButton3]