swift5 UILabel Clickable - ios

I am learning iOS development. And I want to make UILabel clickable
Here is what I have done but with no result.
#IBOutlet weak var coordinate: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
coordinate.adjustsFontSizeToFitWidth = true
let tap = UIGestureRecognizer(target: self, action: #selector(uiLabelClicked()))
coordinate.addGestureRecognizer(tap)
coordinate.isUserInteractionEnabled = true
Below is a function that is expected to perform an action when label is clicked.
#objc func uiLabelClicked(){
let alert = UIAlertController(title: "Coordinate", message: "I have been clicked...", preferredStyle: .alert)
self.present(alert, animated: true, completion: nil)
debugPrint("clicked")
}
I apologize for my English I am using google translator.

Try this:
#IBOutlet weak var coordinate: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
coordinate.adjustsFontSizeToFitWidth = true
let tap = UITapGestureRecognizer(target: self, action: #selector(self.labelAction(_:)))
coordinate.addGestureRecognizer(tap)
coordinate.isUserInteractionEnabled = true
}
#objc func labelAction(_ sender: UIGestureRecognizer) {
let alert = UIAlertController(title: "Coordinate", message: "I have been clicked...", preferredStyle: .alert)
self.present(alert, animated: true, completion: nil)
debugPrint("clicked")
}

Related

UIButton tint under UIAlertController

I'm trying to show a UIAlertController over a UIButton. The button is of type custom, and I have set the tint color to UIColor.clear, because I don't want any tint color. I've read that setting your UIButton to custom turns off the tint color, but that doesn't work for me. This method works out ok, until I show an alert controller on top.
This is before adding the alert:
This is what it looks like when I show the alert:
There's a clear grey tint color around the UIButton. It does this with any .selected UIButtons. How do I get rid of this tint? I really don't want to make my own button class just to get rid of this.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var starBtn: UIButton!
var selected: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
starBtn.isSelected = selected
starBtn.setImage(UIImage(named: "StarButtonSelected"), for: .selected)
starBtn.setImage(UIImage(named: "StarButtonDeselected"), for: .normal)
starBtn.isHighlighted = false
starBtn.tintColor = UIColor.clear
starBtn.backgroundColor = UIColor.clear
}
#IBAction func starBtnUp(_ sender: Any) {
selected = !selected
starBtn.isSelected = selected
}
#IBAction func showAlertBtnUp(_ sender: Any) {
let shareFileAction = UIAlertAction(title: "Share File", style: .default) { action in
}
let shareWebLink = UIAlertAction(title: "Share Link", style: .default) { action in
}
let alert = UIAlertController(title: "Share", message: "What do you want to share?", preferredStyle: .alert)
alert.addAction(shareFileAction)
alert.addAction(shareWebLink)
self.present(alert, animated: true) {
}
}
}
Edit: Okay, so if I create the button programmatically only, without using IB, it doesn't do the grey tint thing. I really don't want to have to not use IB though. Here's the re-written code that works how I want it to.
class ViewController: UIViewController {
var starBtn: UIButton!
var selected: Bool = true
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
starBtn = UIButton(type: .custom)
self.view.addSubview(starBtn)
starBtn.translatesAutoresizingMaskIntoConstraints = false
starBtn.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
starBtn.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
starBtn.heightAnchor.constraint(equalToConstant: 60).isActive = true
starBtn.widthAnchor.constraint(equalToConstant: 60).isActive = true
starBtn.isSelected = selected
starBtn.tintColor = UIColor.clear
starBtn.setImage(UIImage(named: "StarButtonSelected"), for: .selected)
starBtn.setImage(UIImage(named: "StarButtonDeselected"), for: .normal)
starBtn.isHighlighted = false
starBtn.backgroundColor = UIColor.clear
starBtn.addTarget(self, action: #selector(starBtnUp(_:)), for: .touchUpInside)
}
#IBAction func starBtnUp(_ sender: Any) {
selected = !selected
starBtn.isSelected = selected
}
#IBAction func showAlertBtnUp(_ sender: Any) {
let shareFileAction = UIAlertAction(title: "Share File", style: .default) { action in
}
let shareWebLink = UIAlertAction(title: "Share Link", style: .default) { action in
}
let alert = UIAlertController(title: "Share", message: "What do you want to share?", preferredStyle: .actionSheet)
alert.addAction(shareFileAction)
alert.addAction(shareWebLink)
self.present(alert, animated: true) {
}
}
}

UIImagePickerControllerDelegate not being called, selected image never displayed

I'm stumped. Everything is working except it's not. I can call up the camera interface and take or select a photo yet UIImagePickerControllerDelegate is never being called (I've put two print statements that well, never get printed). Everything is correctly connected in IB, or so I believe it is (I'm fully ready for it so be something totally minor that I missed). Anyone care to take a frtesh set of eyes and go over this to see if I just can't see the forest for the trees?
Thanks in advance.
import UIKit
import Foundation
class afterPhotoViewController: UIViewController {
//MARK: - IBOutlet Properties
#IBOutlet weak var afterImageView: UIImageView!
#IBOutlet weak var aftervwImage: UIView!
#IBOutlet weak var cameraBtnPressed: UIButton!
//MARK: - Useful Variables
let afterImagePicker = UIImagePickerController()
var hud = HKProgressHUD()
var localTextFilePath : URL!
var isImageSetted = false
//MARK: - App Delegates
var isMain = false
var originFrame = CGRect()
override func viewDidLoad() {
super.viewDidLoad()
// Init UI Components
self.initUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: - UI Init
func initUI() {
// Set Navigation Bar
let backButton = UIBarButtonItem(title: "", style: .plain, target: navigationController, action: nil)
navigationItem.leftBarButtonItem = backButton
self.title = "\(h_proposalNumberStr)-\(h_itemIndex)"
// Set ImageView
self.afterImageView.image = #imageLiteral(resourceName: "ic_placeholder")
self.afterImageView.clipsToBounds = false
cameraBtnPressed.layer.cornerRadius = 15
cameraBtnPressed.layer.borderColor = UIColor.lightGray.cgColor
cameraBtnPressed.layer.borderWidth = 1
self.isImageSetted = false
// self.tableView.reloadData()
}
//MARK: - UI Actions
#IBAction func cameraBtnPressed(_ sender: Any) {
//Create the AlertController and add Its action like button in Actionsheet
let actionSheetController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let cancelActionButton = UIAlertAction(title: "Cancel", style: .cancel) { _ in
}
actionSheetController.addAction(cancelActionButton)
let chooseLibraryActionButton = UIAlertAction(title: "Choose from Library", style: .default)
{ _ in
self.isMain = true
self.afterImagePicker.allowsEditing = true
self.afterImagePicker.sourceType = .photoLibrary
self.afterImagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
self.present(self.afterImagePicker, animated: true, completion: nil)
}
actionSheetController.addAction(chooseLibraryActionButton)
let takePhotoActionButton = UIAlertAction(title: "Take a Photo", style: .default)
{ _ in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
self.isMain = true
self.afterImagePicker.allowsEditing = false
self.afterImagePicker.sourceType = .camera
self.afterImagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .camera)!
self.afterImagePicker.showsCameraControls = true
self.present(self.afterImagePicker, animated: true, completion: nil)
} else {
Helper.showMessage(vc: self, title: CONSTANTS.APPINFO.APP_NAME, bodyStr: "No camera available.")
}
}
actionSheetController.addAction(takePhotoActionButton)
self.present(actionSheetController, animated: true, completion: nil)
}
//MARK: - UIImagePickerControllerDelegate
extension afterPhotoViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate {
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
print("called")
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
print("called2")
//getting actual image
var image = info[UIImagePickerControllerEditedImage] as? UIImage
if image == nil {
image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
afterImageView.image = image
// Set Imageview Corner Radius
afterImageView.layer.cornerRadius = 5
afterImageView.clipsToBounds = true
self.isImageSetted = true
picker.dismiss(animated: true, completion: nil)
}
}
#raja Kishan caught my stupid simple error of forgetting to set the delegate.
Now, where's the facepalm emoji.
You have to specify that the UIImagePickerController's delegate is self, so that the events are triggered.
In your case:
afterImagePicker.delegate = self

How can I disable a UIButton until multiple TextFields are filled in with data (numbers)?

I have 3 UITextFields in my app and I need a user to fill in before he/she can see the UIButton which will calculate the result. I have tried a lot of different methods to resolve the issue but they don't seem to work and I can't quite understand some of them. I am sure there is a simple way to do that. P.S.: I don't use a storyboard
I have already tried to include UITextFieldDelegate, then writing a function, also used other ways, etc...
import UIKit
let Label: UILabel = {
let f = UILabel()
f.text = "Label"
return f
}()
let textFiled1: UITextField = {
let fw = UITextField()
fw.keyboardType = UIKeyboardType.decimalPad
return fw
}()
let textField2: UITextField = {
let fh = UITextField()
fh.keyboardType = UIKeyboardType.decimalPad
return fh
}()
let textField3: UITextField = {
let fa = UITextField()
fa.keyboardType = UIKeyboardType.numberPad
return fa
}()
class ViewController: UIViewController {
let calculateButton: UIButton = {
let c = UIButton()
c.setTitle("Calculate", for: .normal)
return c
}()
override func viewDidLoad() {
super.viewDidLoad()
setupLabel()
setupTextFieldComponents()
setupCalculateButton()
}
fileprivate func setupLabel() {
view.addSubview(fLabel)
}
fileprivate func setupTextFieldComponents() {
setupTextField1()
setupTextField2()
setupTexrField3()
}
fileprivate func setupTextField1() {
view.addSubview(textField1)
}
fileprivate func setupTextField2() {
view.addSubview(textField2)
}
fileprivate func setupTextField3() {
view.addSubview(textField3)
}
fileprivate func setupCalculateButton() {
view.addSubview(calculateButton)
}
#objc func calculateButtonPressed(sender: UIButton){
let textfield1 = Float(textField1.text!)
let textfield2 = Float(textField2.text!)
let textfield3 = Float(textField3.text!)
femaleMetaLabel.text = String(111 + (1.1 * textfield1!) + (1.1 * textfield2!) - (1.1 * textfield3!))
}
}
User is supposed to enter numeric data in all three text fields in order to calculate the result. Is there a way to make an ERROR message pop out without an app crashing?
When I am doing validation on text input that enables a button, I usually do something like this:
#IBOutlet weak var usernameField: UITextField!
#IBOutlet weak var passwordField: UITextField!
#IBOutlet weak var submitButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
submitButton.isEnabled = false
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self,
selector: #selector(validateInput(_:)),
name: UITextField.textDidChangeNotification,
object: usernameField)
NotificationCenter.default.addObserver(self,
selector: #selector(validateInput(_:)),
name: UITextField.textDidChangeNotification,
object: passwordField)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
#objc func validateInput(_ sender: Any?) {
submitButton.isEnabled = false
if let username = usernameField.text,
let password = passwordField.text {
// your validation rules will be more complex than this :D
if username.count > 0 && password.count > 0 {
submitButton.isEnabled = true
}
}
}
If what you need to do is pop an alert when the inputs are incorrect, simply substitute a call to a method like the one below, where you vaildate the user's input.
func showAlert() {
let alert = UIAlertController(title: "Nope",
message: "Your inputs were incorrect.",
preferredStyle: .alert)
alert.addAction( UIAlertAction(title: "Try again",
style: .default,
handler: nil) )
self.present(alert, animated: true, completion: nil)
}
#Maxim you are almost there. Let me add a simple delegate in your code to achieve your goal.
Add code in your textFieldDidEndEditing delegate -
func textFieldDidEndEditing(textField: UITextField) {
if (textField1.text?.count)! > 0 && (textField2.text?.count)! > 0 && (textField3.text?.count)! > 0 {
calculateButton.isEnabled = true
}else{
calculateButton.isEnabled = false
}
}
Now time to calculate the result:
#objc func calculateButtonPressed(sender: UIButton){
if let inputOne = Float(textField1.text!), let inputTwo = Float(textField2.text!), let inputThree = Float(textField3.text!) {
femaleMetaLabel.text = String(111+(1.1*inputOne)+(1.1*inputTwo)-(1.1*inputThree))
}else{
let alert = UIAlertController(title: "", message: "You entered invalid inputs", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
self.present(alert, animated: true)
}
}
Hope it will help you. Let me know if you are still having any issue.

UIButton not working or gets unrecognized selector sent to instance

I have an odd (at least to me) issue in that I used the pod Localize_Swift to localize my app.
I have two issues that I cannot figure out as nearly all documentation about localizing only covers text on the same vc as the localize menu or button. In my case, I have localize strings set up and across the entire app, they are in use with a .localize suffix.
First issue:
The button either does not work. The print() function never gets triggered or if it does work, appears to be randomly, I get the unrecognized selector sent to instance that lists the func changeLanguage as the cause. I commented out all of the code within the function, but still received the error.
I have also deleted the func and recreated it, but still no go.
Second issue:
How to initialize the localization across the entire app with the strings?
SettingsViewController.swift
import UIKit
import Localize_Swift
class SettingsViewController: UITableViewController
{
var availableLanguages = Localize.availableLanguages()
var actionSheet: UIAlertController!
#IBOutlet weak var switchlangButton: UIButton!
#IBOutlet weak var flagImageView: UIImageView!
override func viewDidLoad()
{
super.viewDidLoad()
let backButton:UIBarButtonItem = UIBarButtonItem(title: "Back".localized, style: UIBarButtonItemStyle.Done, target: self, action: #selector(SettingsViewController.backBtnAction(_:)))
self.navigationItem.leftBarButtonItem = backButton
configureView()
availableLanguages.removeAtIndex(0)
print(availableLanguages)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SettingsViewController.setTranslatedText), name: LCLLanguageChangeNotification, object: nil)
}
func setTranslatedText(){
switchlangButton.setTitle("Switch Language".localized(), forState: UIControlState.Normal)
flagImageView.image = UIImage(named: "flag-en".localized() + ".png")
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
NSNotificationCenter.defaultCenter().removeObserver(self)
}
#IBAction func changeLanguage(sender: AnyObject) {
print ("changeLanguageButton pressed")
actionSheet = UIAlertController(title: nil, message: "Switch Language".localized(), preferredStyle: UIAlertControllerStyle.ActionSheet)
for language in availableLanguages {
let displayName = Localize.displayNameForLanguage(language)
let languageAction = UIAlertAction(title: displayName, style: .Default, handler: {
(alert: UIAlertAction!) -> Void in
Localize.setCurrentLanguage(language)
})
actionSheet.addAction(languageAction)
}
let cancelAction = UIAlertAction(title: "Cancel".localized(), style: UIAlertActionStyle.Cancel, handler: {
(alert: UIAlertAction) -> Void in
})
actionSheet.addAction(cancelAction)
self.presentViewController(actionSheet, animated: true, completion: nil)
}
func configureView(){
switchlangButton.setTitle("Switch Language".localized(), forState: UIControlState.Normal)
flagImageView.image = UIImage(named: "flag-en".localized() + ".png")
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
#IBAction func backBtnAction(sender:UIBarButtonItem)
{
navigationController?.popViewControllerAnimated(true)
}
}
Screenshot of the Settings scene
Just an FYI for troubleshooting, the image does not work either.

Adding UIGestureRecognizer To Subview Programmatically in Swift

I currently have a subview that is created and added to the UIView in ViewDidLoad(). I am attempting to user UIGestureRecognizers to detect a tap and unhide a particular button. My current code:
override func viewDidLoad() {
super.viewDidLoad()
architectView = CustomClass(frame: self.view.bounds)
self.view.addSubview(architectView)
let gestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
gestureRecognizer.delegate = self
architectView.addGestureRecognizer(gestureRecognizer)
}
func handleTap(gestureRecognizer: UIGestureRecognizer) {
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
The handleTap() function is a simple test to see if the taps are being recognized. This code does not trigger the UIAlert when it is pressed? What am I missing?
I tested your code here and it does work. However, I think you might be missing to add the UIGestureRecognizerDelegate protocol to your View Controller. See below:
class ViewController: UIViewController, UIGestureRecognizerDelegate {
var architectView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
architectView = UIView(frame: self.view.bounds)
self.view.addSubview(architectView)
let gestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
gestureRecognizer.delegate = self
architectView.addGestureRecognizer(gestureRecognizer)
}
func handleTap(gestureRecognizer: UIGestureRecognizer) {
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Swift 3 version code based on Raphael Silva answer:
import UIKit
class ViewController: UIViewController, UIGestureRecognizerDelegate {
var architectView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
architectView = UIView(frame: self.view.bounds)
self.view.addSubview(architectView)
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap(gestureRecognizer:)))
gestureRecognizer.delegate = self
architectView.addGestureRecognizer(gestureRecognizer)
}
func handleTap(gestureRecognizer: UIGestureRecognizer) {
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Resources