swift can't click uibutton located inside multiple subviews - ios

I am not getting when I press the arrowButton, not even the print("toggleWhiteMenu pressed") is printing in the console? I have searched the other questions like this one but the don't seem to help.
let card: UIView = {
let tsl = UIView()
tsl.backgroundColor = UIColor.black
tsl.alpha = 0.9
return tsl
}()
let yellowCard: UIView = {
let tsl = UIView()
tsl.backgroundColor = UIColor.yellow
//tsl.alpha = 1
tsl.isUserInteractionEnabled = false
return tsl
}()
lazy var arrowButton: UIButton = {
let button = UIButton(type: .system)
button.setImage(#imageLiteral(resourceName: "down arrow"), for: .normal)
button.tintColor = UIColor.black
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.addTarget(self, action: #selector(self.toggleWhiteMenu), for: .touchUpInside)
return button
}()
func toggleWhiteMenu() {
print("toggleWhiteMenu pressed")
UIView.animate(withDuration: 1, animations: {
self.whiteMenu.transform = CGAffineTransform(scaleX: 11, y: 11)
}) { (true) in
print("really sick")
}
}
let whiteMenu: UIView = {
let tsl = UIView()
tsl.backgroundColor = UIColor.white
tsl.alpha = 1
tsl.isUserInteractionEnabled = false
return tsl
}()
let containerView: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .yellow
v.isUserInteractionEnabled = false
return v
}()
let scrollView: UIScrollView = {
let v = UIScrollView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .white
return v
}()
let backgroundImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "eiffel tower")
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.isUserInteractionEnabled = false
imageView.layer.masksToBounds = true
imageView.contentMode = .scaleAspectFill
return imageView
}()
func setUpViews() {
self.view.addSubview(scrollView)
scrollView.addSubview(containerView)
containerView.addSubview(backgroundImageView)
backgroundImageView.addSubview(card)
backgroundImageView.addSubview(yellowCard)
yellowCard.addSubview(whiteMenu)
yellowCard.addSubview(arrowButton)
///i have left out constraints except for the arrowbutton's constraints as i don't deem them necessary for this question
arrowButton.anchor(top: nil, left: nil, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: view.frame.width/6, height: view.frame.width/6)
arrowButton.clipsToBounds = true
arrowButton.centerXAnchor.constraint(equalTo: whiteMenu.centerXAnchor).isActive = true
arrowButton.centerYAnchor.constraint(equalTo: whiteMenu.centerYAnchor).isActive = true
}

Please add #objc with selector method like below.
#objc func toggleWhiteMenu() {
print("toggleWhiteMenu pressed")
UIView.animate(withDuration: 1, animations: {
self.whiteMenu.transform = CGAffineTransform(scaleX: 11, y: 11)
}) { (true) in
print("really sick")
}
}

You are setting:
.isUserInteractionEnabled = false
on multiple views, and then adding a button as a subview. isUserInteractionEnabled set to false on a parent view will cascade down to all subviews, including buttons and other controls.

Related

How can I change screens layout for landscape and portrait in iOS

I have totally different layouts for landscape and portrait.
In portrait the screen does not have the menu view of full height and half width on left and in landscape the screen contains the menu view on left side.
How to do that in iOS?
If you only want to show menu on landscape.
Check the device orientation by:
UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight
Then show the menu by using menu.isHidden = false to show the menu.
You can set the menu constraints either using storyboard or programmatically.
You can do it programmatically, declare your menuContainer view under your class controller like that:
let menuContainer: UIView = {
let v = UIView()
v.backgroundColor = .lightGray
v.alpha = 0
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
Now in viewDidLoad set constraints:
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .darkGray
view.addSubview(menuContainer)
menuContainer.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
menuContainer.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
menuContainer.trailingAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
menuContainer.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
after that write the detect func (I add animation but you can't do it):
fileprivate func detectRotation() {
if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {
print("landscape left")
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {
self.menuContainer.alpha = 1
self.view.layoutIfNeeded()
}, completion: nil)
} else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {
print("landscape right")
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {
self.menuContainer.alpha = 1
self.view.layoutIfNeeded()
}, completion: nil)
} else {
print("porrait")
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {
self.menuContainer.alpha = 0
self.view.layoutIfNeeded()
}, completion: nil)
}
}
override viewWillTransition func and call detectRotation func:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
detectRotation()
}
This is the result:
portrait
Landscape
To show in landscape and portrait:
let myButton: UIButton = {
let b = UIButton()
b.backgroundColor = .white
b.setTitle("Section 1", for: .normal)
b.setTitleColor(.black, for: .normal)
b.titleLabel?.font = .systemFont(ofSize: 17, weight: .regular)
b.titleLabel?.adjustsFontSizeToFitWidth = true
b.titleLabel?.minimumScaleFactor = 0.5
b.translatesAutoresizingMaskIntoConstraints = false
return b
}()
let myButton2: UIButton = {
let b = UIButton()
b.backgroundColor = .white
b.setTitle("Section 2", for: .normal)
b.setTitleColor(.black, for: .normal)
b.titleLabel?.font = .systemFont(ofSize: 17, weight: .regular)
b.titleLabel?.adjustsFontSizeToFitWidth = true
b.titleLabel?.minimumScaleFactor = 0.5
b.translatesAutoresizingMaskIntoConstraints = false
return b
}()
let myButton3: UIButton = {
let b = UIButton()
b.backgroundColor = .white
b.setTitle("Section 3", for: .normal)
b.setTitleColor(.black, for: .normal)
b.titleLabel?.font = .systemFont(ofSize: 17, weight: .regular)
b.titleLabel?.adjustsFontSizeToFitWidth = true
b.titleLabel?.minimumScaleFactor = 0.5
b.titleLabel?.textAlignment = .left
b.translatesAutoresizingMaskIntoConstraints = false
return b
}()
let myButton4: UIButton = {
let b = UIButton()
b.backgroundColor = .white
b.setTitle("Section 4", for: .normal)
b.setTitleColor(.black, for: .normal)
b.titleLabel?.font = .systemFont(ofSize: 17, weight: .regular)
b.titleLabel?.adjustsFontSizeToFitWidth = true
b.titleLabel?.minimumScaleFactor = 0.5
b.titleLabel?.textAlignment = .left
b.translatesAutoresizingMaskIntoConstraints = false
return b
}()
let menuContainer: UIView = {
let v = UIView()
v.backgroundColor = .lightGray
v.alpha = 1
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .darkGray
view.addSubview(menuContainer)
menuContainer.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
menuContainer.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
menuContainer.trailingAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
menuContainer.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
let stackViewMenu = UIStackView(arrangedSubviews: [myButton, myButton2, myButton3, myButton4])
stackViewMenu.axis = .vertical
stackViewMenu.distribution = .fillEqually
stackViewMenu.spacing = 2
stackViewMenu.translatesAutoresizingMaskIntoConstraints = false
menuContainer.addSubview(stackViewMenu)
stackViewMenu.topAnchor.constraint(equalTo: menuContainer.safeAreaLayoutGuide.topAnchor).isActive = true
stackViewMenu.leadingAnchor.constraint(equalTo: menuContainer.leadingAnchor).isActive = true
stackViewMenu.trailingAnchor.constraint(equalTo: menuContainer.trailingAnchor).isActive = true
stackViewMenu.heightAnchor.constraint(equalTo: menuContainer.heightAnchor, multiplier: 0.5).isActive = true
}
The result:

How can I get a button in my subview to enable the rightbarbuttonitem in my view controller

I have a UIView with a textfield and a button, this UIView is a subview of my view controller. How can I have the button in my subview enable the view controller's navigation rightbarbuttonitem when it is pressed?
class CreateActivityView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
lazy var addButton: UIButton = {
let button = UIButton(type: UIButton.ButtonType.system)
button.setImage(UIImage(named: "add_dark.png"), for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 24.0, height: 24.0)
button.addTarget(self, action: #selector(addButtonTapped), for: .touchUpInside)
button.isEnabled = false
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
lazy var activityNameTextField: DataEntryTextField = {
let textField = DataEntryTextField()
textField.frame = CGRect(x: 0, y: 0, width: 250.0, height: 30.0)
textField.placeholder = "Add an activity"
textField.font = UIFont(name: "Avenir", size: 17)
textField.keyboardType = UIKeyboardType.default
textField.returnKeyType = UIReturnKeyType.done
textField.translatesAutoresizingMaskIntoConstraints = false
return textField
}()
lazy var activityNameLabel: UILabel = {
let label = UILabel()
label.frame = CGRect(x: 0, y: 0, width: 0, height: 30.0)
label.font = UIFont(name: "Avenir", size: 17)
label.translatesAutoresizingMaskIntoConstraints = false
label.isHidden = true
return label
}()
func setupView() {
backgroundColor = UIColor.lightGray
addSubview(addButton)
addButton.rightAnchor.constraint(equalTo: self.safeAreaLayoutGuide.rightAnchor, constant: -16.0).isActive = true
addButton.centerYAnchor.constraint(equalTo: self.safeAreaLayoutGuide.centerYAnchor).isActive = true
addButton.heightAnchor.constraint(equalToConstant: 24.0).isActive = true
addButton.widthAnchor.constraint(equalToConstant: 24.0).isActive = true
addSubview(activityNameTextField)
activityNameTextField.leftAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leftAnchor, constant: 16.0).isActive = true
activityNameTextField.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 10.0).isActive = true
activityNameTextField.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor, constant: -10.0).isActive = true
activityNameTextField.rightAnchor.constraint(equalTo: addButton.safeAreaLayoutGuide.leftAnchor, constant: -20.0).isActive = true
addSubview(activityNameLabel)
activityNameLabel.leftAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leftAnchor, constant: 16.0).isActive = true
activityNameLabel.rightAnchor.constraint(equalTo: self.safeAreaLayoutGuide.rightAnchor, constant: -16.0).isActive = true
activityNameLabel.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 10.0).isActive = true
activityNameLabel.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor, constant: -10.0).isActive = true
}
#objc func addButtonTapped() {
guard let validatedText = activityNameTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines), !validatedText.isEmpty else {
return
}
displayActivityNameLabel(with: validatedText)
}
func displayActivityNameLabel(with text: String) {
activityNameLabel.text = text
activityNameLabel.isHidden = false
activityNameTextField.isHidden = true
print(activityNameLabel.text)
}
}
class QuickLogViewController: UIViewController {
let screenSize = UIScreen.main.bounds.size
var createActivityView: CreateActivityView!
override func viewDidLoad() {
super.viewDidLoad()
title = "Quick Log"
setupView()
}
func setupView() {
initializeCreateActivityView()
self.view.addSubview(createActivityView)
createActivityView.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor).isActive = true
createActivityView.rightAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.rightAnchor).isActive = true
createActivityView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
createActivityView.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
}
func initializeCreateActivityView() {
createActivityView = CreateActivityView()
createActivityView.frame = CGRect(x: 0, y: 0, width: screenSize.width, height: 50.0)
createActivityView.translatesAutoresizingMaskIntoConstraints = false
createActivityView.activityNameTextField.delegate = self
}
func enableCancelButton() {
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelButtonPressed))
}
}
I added my CreateActivityView to my view controller. The button target is handled by my createActvitiyView. So how can I access the rightbarbuttonitem from the subview?
You can try to use delegate
class CreateActivityView: UIView {
weak var delegate:VCName?
and
createActivityView = CreateActivityView()
createActivityView.delegate = self
after that inside any action do
delegate?.methodInsideTheVC()
Or write button action inside the vc
createActivityView = CreateActivityView()
createActivityView.button.addTarget(self, action: #selector(addButtonTapped), for: .touchUpInside)
and place
#objc func addButtonTapped() {

iOS CustomView With AutoLayout in navigationItem not receiving clicks

I created a custom view for navigationItem but somehow it is not receiving any click events:
The code for customView is below
class CustomNavigationView: UIView {
let backButton: UIButton = {
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "icon_back", in: Bundle.main, compatibleWith: nil), for: .normal)
button.isUserInteractionEnabled = true
return button
}()
var profileImage: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "icon_back", in: Bundle.main, compatibleWith: nil)
return imageView
}()
var profileName: UILabel = {
let label = UILabel()
label.text = "No Name"
label.font = UIFont(name: "HelveticaNeue", size: 16) ?? UIFont.systemFont(ofSize: 16)
label.textColor = UIColor(red: 96, green: 94, blue: 94)
return label
}()
var onlineStatusIcon: UIView = {
let view = UIView()
view.backgroundColor = UIColor(28, green: 222, blue: 20)
return view
}()
var onlineStatusText: UILabel = {
let label = UILabel()
label.text = "Online"
label.font = UIFont(name: "HelveticaNeue", size: 12) ?? UIFont.systemFont(ofSize: 12)
label.textColor = UIColor(red: 113, green: 110, blue: 110)
return label
}()
lazy var profileView: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [self.profileName, self.onlineStatusText])
stackView.alignment = .fill
stackView.axis = .vertical
stackView.distribution = .fillEqually
stackView.spacing = 2
return stackView
}()
#objc func backButtonClicked(_ sender: UIButton) {
print("Back Button click successfully")
}
private func setupConstraints() {
self.addViewsForAutolayout(views: [backButton, profileImage, onlineStatusIcon, profileView])
//Setup constraints
backButton.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 5).isActive = true
backButton.topAnchor.constraint(equalTo: self.topAnchor, constant: 10).isActive = true
backButton.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -10).isActive = true
backButton.widthAnchor.constraint(equalToConstant: 20).isActive = true
profileImage.leadingAnchor.constraint(equalTo: backButton.trailingAnchor, constant: 20).isActive = true
profileImage.topAnchor.constraint(equalTo: self.topAnchor, constant: 5).isActive = true
profileImage.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -5).isActive = true
profileImage.widthAnchor.constraint(equalToConstant: 35).isActive = true
profileImage.layer.cornerRadius = 18
profileImage.clipsToBounds = true
onlineStatusIcon.bottomAnchor.constraint(equalTo: profileImage.bottomAnchor, constant: 0).isActive = true
onlineStatusIcon.leadingAnchor.constraint(equalTo: profileImage.trailingAnchor, constant: -8).isActive = true
onlineStatusIcon.widthAnchor.constraint(equalToConstant: 10).isActive = true
onlineStatusIcon.heightAnchor.constraint(equalToConstant: 10).isActive = true
onlineStatusIcon.layer.cornerRadius = 5
onlineStatusIcon.clipsToBounds = true
profileView.leadingAnchor.constraint(equalTo: profileImage.trailingAnchor, constant: 5).isActive = true
profileView.topAnchor.constraint(equalTo: profileImage.topAnchor).isActive = true
profileView.bottomAnchor.constraint(equalTo: profileImage.bottomAnchor).isActive = true
}
required init() {
super.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 40))
setupConstraints()
addButtonTarget()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addButtonTarget() {
// Setup button callback
backButton.addTarget(self, action: #selector(backButtonClicked(_:)), for: .touchUpInside)
print("Target added")
}
}
And I am setting this view as NavigationbarLeft button Item in my view Controller:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let customView = CustomNavigationView()
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: customView)
}
}
The view is displaying correctly but the clicks are not working at all.
I used view debugging to check if some other layer is on top of this which might be causing problem but nothing of that sort is present.
I also checked backButton frame when adding the target using debug points.
Is there any solution for this problem. Does autolayout not work with custom view in navigation item? Or is there something that I am missing.
You can run the above piece of code and see that the clicks are not working.
This somehow appears to be related to auto layout. If I hardcode the frame position then clicks are working.
class CustomNavigationView: UIView {
let backButton: UIButton = {
let button = UIButton(frame: CGRect(x: 5, y: 5, width: 30, height: 30))
button.setImage(UIImage(named: "icon_back", in: Bundle.kommunicate, compatibleWith: nil), for: .normal)
button.isUserInteractionEnabled = true
return button
}()
var profileImage: UIImageView = {
let imageView = UIImageView(frame: CGRect(x: 40, y: 5, width: 30, height: 30))
imageView.image = UIImage(named: "icon_back", in: Bundle.kommunicate, compatibleWith: nil)
return imageView
}()
var profileName: UILabel = {
let label = UILabel(frame: CGRect(x: 80, y: 5, width: 50, height: 15))
label.text = "No Name"
label.font = UIFont(name: "HelveticaNeue", size: 16) ?? UIFont.systemFont(ofSize: 16)
label.textColor = UIColor(red: 96, green: 94, blue: 94)
return label
}()
var onlineStatusIcon: UIView = {
let view = UIView(frame: CGRect(x: 65, y: 30, width: 10, height: 10))
view.backgroundColor = UIColor(28, green: 222, blue: 20)
return view
}()
var onlineStatusText: UILabel = {
let label = UILabel(frame: CGRect(x: 80, y: 25, width: 50, height: 10))
label.text = "Online"
label.font = UIFont(name: "HelveticaNeue", size: 12) ?? UIFont.systemFont(ofSize: 12)
label.textColor = UIColor(red: 113, green: 110, blue: 110)
return label
}()
lazy var profileView: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [self.profileName, self.onlineStatusText])
stackView.alignment = .fill
stackView.axis = .vertical
stackView.distribution = .fillEqually
stackView.spacing = 2
return stackView
}()
#objc func backButtonClicked(_ sender: UIButton) {
print("Back button is successfully called")
}
private func setupConstraints() {
self.addSubview(backButton)
self.addSubview(profileImage)
self.addSubview(onlineStatusIcon)
self.addSubview(profileView)
}
required init() {
super.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 40))
setupConstraints()
addButtonTarget()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addButtonTarget() {
// Setup button callback
backButton.addTarget(self, action: #selector(backButtonClicked(_:)), for: .touchUpInside)
print("Target added")
}
}
The problem is with the manually added constraints that you added.
Using the view debugger the width of CustomNavigationView after it is added to the bar is 0.
In order to force the container to expand, add the following constraint in setupConstraints():
profileView.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
Now that the container expands to match it's contents, the touch events should be propagated to the button as expected.

swift can't call function after clicking button

I have made a container view with a image view and a button inside for some reason when running the app in simulator i can't click button and call the function taking me to the next screen , i have encountered this kind of problem before and it was as simple as changing the button to a lazy var though that hasn't work on this occasion?
lazy var nameLabelButton = UIButton()
func setupNavBarWithUser() {
guard let displayName = user?.DisplayName else { return }
let titleView = UIView()
titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
titleView.backgroundColor = UIColor.red
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
titleView.addSubview(containerView)
let profileImageView = UIImageView()
profileImageView.translatesAutoresizingMaskIntoConstraints = false
profileImageView.contentMode = .scaleAspectFill
profileImageView.layer.cornerRadius = 20
profileImageView.clipsToBounds = true
if let profileImageUrl = user?.profileImageURL {
profileImageView.loadImageUsingCacheWithUrlString(urlString:profileImageUrl)
}
containerView.addSubview(profileImageView)
//ios 9 constraint anchors
//need x,y,width,height anchors
profileImageView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
profileImageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
profileImageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 40).isActive = true
containerView.addSubview(nameLabelButton)
nameLabelButton.setTitle("\(displayName)", for: .normal)
nameLabelButton.setTitleColor(.black, for: .normal)
nameLabelButton.translatesAutoresizingMaskIntoConstraints = false
//need x,y,width,height anchors
nameLabelButton.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8).isActive = true
nameLabelButton.centerYAnchor.constraint(equalTo: profileImageView.centerYAnchor).isActive = true
nameLabelButton.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
nameLabelButton.heightAnchor.constraint(equalTo: profileImageView.heightAnchor).isActive = true
containerView.centerXAnchor.constraint(equalTo: titleView.centerXAnchor).isActive = true
containerView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true
nameLabelButton.addTarget(self, action: #selector(self.openUsersProfileController), for: .touchUpInside)
self.navigationItem.titleView = titleView
}
func openUsersProfileController(){
print("asdasdadsad")
let openUsersProfileController = UserProfileController(collectionViewLayout: UICollectionViewFlowLayout())
openUsersProfileController.user = self.user
navigationController?.pushViewController(openUsersProfileController, animated: true)
}
well, code looks fine you just need to add #objc
#objc func openUsersProfileController(){
print("asdasdadsad")
let openUsersProfileController = UserProfileController(collectionViewLayout: UICollectionViewFlowLayout())
openUsersProfileController.user = self.user
navigationController?.pushViewController(openUsersProfileController, animated: true)
}
Reason:
From Swift 4, we manually need to add #objc before function.

Use of unresolved identifier

I am building a barcode scanner. the operations are simple
scan a barcode,
if the scanned barcode is in my firebase database perform func updateProductInfo() if the barcode is not in my firebase database perform func enterNewProduct().
The one problem that I am having now is how to define metadataObj so that it is accessible by all and any other function that I will define later. I have attempt to define it right below the ScanController class but I couldn't figure it out. My code is below
import UIKit
import AVFoundation
import Firebase
class ScanController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {
var captureSession: AVCaptureSession?
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
var qrCodeFrameView: UIView?
let itemDB = FIRDatabase.database().reference().child("Items")
let supportedCodeTypes = [AVMetadataObjectTypeUPCECode,
AVMetadataObjectTypeCode39Code,
AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeCode93Code,
AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypeEAN8Code,
AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeAztecCode,
AVMetadataObjectTypePDF417Code,
AVMetadataObjectTypeQRCode]
let messageLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
// label.center = CGPoint(x: 160, y: 285)
label.center = CGPoint(x: 160, y: 285)
label.textAlignment = .center
return label
}()
let productDescriptionTextField: UITextField = {
let tf = UITextField()
tf.placeholder = "Product Description"
tf.translatesAutoresizingMaskIntoConstraints = false
return tf
}()
let descriptionSeparatorView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(r: 220, g: 220, b: 220)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let priceTextField: UITextField = {
let tf = UITextField()
tf.placeholder = "Price"
tf.translatesAutoresizingMaskIntoConstraints = false
return tf
}()
let priceSeparatorView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(r: 220, g: 220, b: 220)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let productLocationTextField: UITextField = {
let tf = UITextField()
tf.placeholder = "Product Location"
tf.translatesAutoresizingMaskIntoConstraints = false
return tf
}()
let productImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "XXXXXXX")
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
return imageView
}()
let exitScanButton: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = UIColor(r: 80, g: 101, b: 161)
button.setTitle("Exit", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget( nil, action: #selector(exitScan), for:.touchUpInside)
return button
}()
let newProductEntry: UIView = {
let view = UIView()
view.backgroundColor = UIColor.white
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = 5
view.layer.masksToBounds = true
return view
}()
let productSummary: UIView = {
let view = UIView()
view.backgroundColor = UIColor.white
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = 5
view.layer.masksToBounds = true
return view
}()
let productDescriptionLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
// label.center = CGPoint(x: 160, y: 285)
label.center = CGPoint(x: 160, y: 285)
label.textAlignment = .center
return label
}()
let storeNameLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
// label.center = CGPoint(x: 160, y: 285)
label.center = CGPoint(x: 160, y: 285)
label.textAlignment = .center
return label
}()
let verifyProductInfoButton: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = UIColor(r: 80, g: 101, b: 161)
button.setTitle("Checked", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget( nil, action: #selector(verifyNewProduct), for:.touchUpInside)
return button
}()
let updateProductInfoButton: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = UIColor(r: 80, g: 101, b: 161)
button.setTitle("Update", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget( nil, action: #selector(updateProductInfo), for:.touchUpInside)
return button
}()
let enterNewProductButton: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = UIColor(r: 80, g: 101, b: 161)
button.setTitle("Enter", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget( nil, action: #selector(enterNewProduct), for:.touchUpInside)
return button
}()
func updateProductInfo() {
guard let price = priceTextField.text,
let location = productLocationTextField.text
else{
print("Please update price and product location")
return
}
}
func verifyNewProduct() {
// need to add a counter that counts how many people verified product information
self.dismiss(animated: true, completion: nil)
}
func enterNewProduct() {
let itemID = metadataObj.stringValue
guard let Description = productDescriptionTextField.text,
let price = priceTextField.text,
let location = productLocationTextField.text
else{
print("Fill basic product information")
return
}
let ref = FIRDatabase.database().reference(fromURL: "")
// creating an item child node
let values = ["Item Description": Description, "Image": price, "Location": location, "Price": price ]
let items = ref.child("Items").child(itemID!)
items.updateChildValues(values, withCompletionBlock: { (err, ref) in
if err != nil {
print(err)
return
}
})
self.dismiss(animated: true, completion: nil)
}
func exitScan() {
//Go back to ViewController
self.dismiss(animated: true, completion: nil)
}
func setupUpdateProductInfo() {
productSummary.addSubview(productDescriptionLabel)
productSummary.addSubview(storeNameLabel)
productSummary.addSubview(priceTextField)
productSummary.addSubview(productLocationTextField)
productSummary.addSubview(verifyProductInfoButton)
productSummary.addSubview(updateProductInfoButton)
productSummary.addSubview(productImageView)
// need x, y, width, height constraints for product image
productImageView.leftAnchor.constraint(equalTo: productSummary.leftAnchor, constant: 12).isActive = true
productImageView.topAnchor.constraint(equalTo: productSummary.topAnchor).isActive = true
productImageView.widthAnchor.constraint(equalTo: productSummary.widthAnchor, constant: 100).isActive = true
productImageView.heightAnchor.constraint(equalToConstant: 100).isActive = true
// need x, y, width, height constraints for store logo
storeNameLabel.leftAnchor.constraint(equalTo: productDescriptionLabel.leftAnchor, constant: 12).isActive = true
storeNameLabel.topAnchor.constraint(equalTo: productSummary.topAnchor).isActive = true
storeNameLabel.widthAnchor.constraint(equalTo: productSummary.widthAnchor).isActive = true
// need x, y, width, height constraints for description label
productDescriptionLabel.leftAnchor.constraint(equalTo: productImageView.leftAnchor, constant: 12).isActive = true
productDescriptionLabel.topAnchor.constraint(equalTo: productSummary.topAnchor).isActive = true
productDescriptionLabel.widthAnchor.constraint(equalTo: productSummary.widthAnchor).isActive = true
productDescriptionLabel.rightAnchor.constraint(equalTo: storeNameLabel.leftAnchor,constant: 12).isActive = true
// need x, y, width, height constraints for price textfield
priceTextField.leftAnchor.constraint(equalTo: productImageView.rightAnchor, constant: 12).isActive = true
priceTextField.topAnchor.constraint(equalTo: productDescriptionLabel.bottomAnchor).isActive = true
priceTextField.widthAnchor.constraint(equalToConstant: 50).isActive = true
// need x, y, width, height constraints for location textfield
productLocationTextField.leftAnchor.constraint(equalTo: priceTextField.rightAnchor, constant: 12).isActive = true
productLocationTextField.topAnchor.constraint(equalTo: productDescriptionLabel.bottomAnchor).isActive = true
productLocationTextField.widthAnchor.constraint(equalToConstant: 50).isActive = true
}
func setupNewProductEntry() {
newProductEntry.addSubview(productImageView)
newProductEntry.addSubview(productLocationTextField)
newProductEntry.addSubview(productDescriptionTextField)
newProductEntry.addSubview(priceTextField)
newProductEntry.addSubview(enterNewProductButton)
newProductEntry.addSubview(descriptionSeparatorView)
newProductEntry.addSubview(priceSeparatorView)
// need x, y, width, height constraints
newProductEntry.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
newProductEntry.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
newProductEntry.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -24).isActive = true
newProductEntry.heightAnchor.constraint(equalToConstant: 150).isActive = true
// need x, y, width, height constraints for name productDescriptionTextField
productDescriptionTextField.leftAnchor.constraint(equalTo: newProductEntry.leftAnchor, constant: 12).isActive = true
productDescriptionTextField.topAnchor.constraint(equalTo: newProductEntry.topAnchor).isActive = true
productDescriptionTextField.widthAnchor.constraint(equalTo: newProductEntry.widthAnchor).isActive = true
// need x, y, width, height constraints for description separator line
descriptionSeparatorView.leftAnchor.constraint(equalTo: newProductEntry.leftAnchor).isActive = true
descriptionSeparatorView.topAnchor.constraint(equalTo: productDescriptionTextField.bottomAnchor).isActive = true
descriptionSeparatorView.widthAnchor.constraint(equalTo: newProductEntry.widthAnchor).isActive = true
descriptionSeparatorView.heightAnchor.constraint(equalToConstant: 1).isActive = true
// need x, y, width, height constraints for name pricetextfield
priceTextField.leftAnchor.constraint(equalTo: newProductEntry.leftAnchor, constant: 12).isActive = true
priceTextField.topAnchor.constraint(equalTo: productDescriptionTextField.bottomAnchor).isActive = true
priceTextField.widthAnchor.constraint(equalTo: newProductEntry.widthAnchor).isActive = true
// need x, y, width, height constraints for price separator line
priceSeparatorView.leftAnchor.constraint(equalTo: newProductEntry.leftAnchor).isActive = true
priceSeparatorView.topAnchor.constraint(equalTo: priceTextField.bottomAnchor).isActive = true
priceSeparatorView.widthAnchor.constraint(equalTo: newProductEntry.widthAnchor).isActive = true
priceSeparatorView.heightAnchor.constraint(equalToConstant: 1).isActive = true
// need x, y, width, height constraints for name LocationTextField
productLocationTextField.leftAnchor.constraint(equalTo: newProductEntry.leftAnchor, constant: 12).isActive = true
productLocationTextField.topAnchor.constraint(equalTo: priceTextField.bottomAnchor).isActive = true
productLocationTextField.widthAnchor.constraint(equalTo: newProductEntry.widthAnchor).isActive = true
}
func setupenterNewProductButton(){
// need x, y, width, height constraints
enterNewProductButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
enterNewProductButton.topAnchor.constraint(equalTo: newProductEntry.bottomAnchor, constant: 12).isActive = true
enterNewProductButton.widthAnchor.constraint(equalTo: newProductEntry.widthAnchor).isActive = true
enterNewProductButton.heightAnchor.constraint(equalToConstant: (50)).isActive = true
}
func setupupdateProductInfoButton(){
// need x, y, width, height constraints
updateProductInfoButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
updateProductInfoButton.topAnchor.constraint(equalTo: productSummary.bottomAnchor, constant: 12).isActive = true
updateProductInfoButton.widthAnchor.constraint(equalTo: productSummary.widthAnchor).isActive = true
updateProductInfoButton.heightAnchor.constraint(equalToConstant: (50)).isActive = true
}
func setupverifyProductInfoButton(){
// need x, y, width, height constraints
verifyProductInfoButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
verifyProductInfoButton.topAnchor.constraint(equalTo: productSummary.bottomAnchor, constant: 12).isActive = true
verifyProductInfoButton.widthAnchor.constraint(equalTo: productSummary.widthAnchor).isActive = true
verifyProductInfoButton.heightAnchor.constraint(equalToConstant: (50)).isActive = true
verifyProductInfoButton.leftAnchor.constraint(equalTo: enterNewProductButton.rightAnchor).isActive = true
}
override func viewDidLoad() {
super.viewDidLoad()
//Get an instance of the AVCaptureDevice class a device object and provide the video as the media type parameter
let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
do {
// Get an instance of the AVCaptureDeviceInput class using the previous device object.
let input = try AVCaptureDeviceInput(device: captureDevice)
// Initialize the captureSession object.
captureSession = AVCaptureSession()
// Set the input device on the capture session.
captureSession?.addInput(input)
let captureMetadataOutput = AVCaptureMetadataOutput()
captureSession?.addOutput(captureMetadataOutput)
// Set delegate and use the default dispatch queue to execute the call back
captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
captureMetadataOutput.metadataObjectTypes = supportedCodeTypes
// Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer?.frame = view.layer.bounds
view.layer.addSublayer(videoPreviewLayer!)
// Start video capture.
captureSession?.startRunning()
// Add the message label
self.view.addSubview(messageLabel)
self.view.addSubview(exitScanButton)
setupexitScanButton()
//initialize QR Code Frame to highlight the QR Code
qrCodeFrameView = UIView()
if let qrCodeFrameView = qrCodeFrameView {
qrCodeFrameView.layer.borderColor = UIColor.green.cgColor
qrCodeFrameView.layer.borderWidth = 2
view.addSubview(qrCodeFrameView)
}
} catch {
// If any error occurs, simply print it out and don't continue any more.
print(error)
return
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
// Check if the metadataObjects array is not nil and it contains at least one object.
if metadataObjects == nil || metadataObjects.count == 0 {
qrCodeFrameView?.frame = CGRect.zero
messageLabel.text = "No QR/barcode is detected"
return
}
//Get metadata object
let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
let itemID = metadataObj.stringValue
if supportedCodeTypes.contains(metadataObj.type) {
//if the found metadata is equal to the QR code metadata then update the status label's text and set the the bounds
let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
qrCodeFrameView?.frame = barCodeObject!.bounds
if metadataObj.stringValue != nil {
messageLabel.text = metadataObj.stringValue
//Searches firebase for existing barcode
let itemToSearchFor = metadataObj.stringValue
FIRDatabase.database().reference().child("Items").child(itemToSearchFor!).observeSingleEvent(of: .value, with:{(snap) in
print(snap)
}) } else {
setupNewProductEntry()
}
}
}
func setupexitScanButton() {
// need x, y, width, height constraints
exitScanButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
exitScanButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 12).isActive = true
exitScanButton.widthAnchor.constraint(equalToConstant: 60).isActive = true
exitScanButton.heightAnchor.constraint(equalToConstant: (50)).isActive = true
}
}
My error is currently at let itemID = metadataObj.stringValue.
Define it at the top under qrcodeframeview to make it "global" in your class

Resources