Disable button2 if button1 is not clicked - ios

I want to disable the button2 if button1 is not clicked, and enable it when button1 is clicked. The problem is, the button2 is disable even I already clicked the button1. I don't receive any error, I'm using Swift4 in Xcode10.
Here is my code:
class ViewController: UIViewController {
#IBOutlet weak var btn1: UIButton!
#IBOutlet weak var btn2: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
intro()
btn1.setImage(UIImage(named: "checked.png"), for: .selected)
btn1.setImage(UIImage(named: "unchecked.png"), for: .normal)
}
#IBAction func button1(_ sender: UIButton) {
UIView.animate(withDuration: 0.5, delay: 0.1, options: .curveLinear, animations: {
sender.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
}) { (success) in
UIView.animate(withDuration: 0.5, delay: 0.1, options: .curveLinear, animations: {
sender.isSelected = !sender.isSelected
sender.transform = .identity
}, completion: nil)
}
}
func intro() {
if btn1.isSelected == true {
btn2.isUserInteractionEnabled = true
} else {
btn2.isUserInteractionEnabled = false
}
}
}
I also tried this code:
func intro() {
if btn1.isTouchInside == true {
btn2.isUserInteractionEnabled = true
} else {
btn2.isUserInteractionEnabled = false
}
}

I think first you want button2 is disable and when you click on the button1 its should be enable.
For this make
override func viewDidLoad() {
super.viewDidLoad()
btn1.setImage(UIImage(named: "checked.png"), for: .selected)
btn1.setImage(UIImage(named: "unchecked.png"), for: .normal)
btn2.isenable = false
}
#IBAction func button1(_ sender: UIButton) {
if sender.isSelected == true
{
btn2.isenable = true
}
else
{
btn2.isenable = false
}
}

Add any of the below code in your viewDidLoad()
btn2.isEnabled = false
or
btn2.isUserInteractionEnabled = false
and change your button action as,
#IBAction func button1(_ sender: UIButton) {
if btn1.isSelected == true {
btn2.isUserInteractionEnabled = true
} else {
btn2.isUserInteractionEnabled = false
}
}

Related

Trouble With UIViewAnimation

I'm getting in trouble on UIView Animation. When I click into one of UITextFields basically everything works fine until I select another UITextField after being selected the other one, the fields move up and down then return to the same place after I select the other UITextField.
What am I doing wrong? What I expected is to move the whole UIStackView containing my fields into up to avoid the keyboard to cover it all. Also, to keep the animation static when I click into the other UITextField, just returning to the default position when the keyboard got dismissed.
class LoginViewController: UIViewController {
var coordinator: MainCoordinator?
override func viewDidLoad() {
super.viewDidLoad()
viewTapped()
setupScreen()
setupViews()
setConstraints()
}
private func setupScreen() {
self.view.backgroundColor = .systemPink
}
private func setConstraints() {
self.textFieldLogin.heightAnchor.constraint(equalToConstant: 50).isActive = true
self.textFieldLogin.widthAnchor.constraint(equalToConstant: 190).isActive = true
//
self.textFieldSenha.heightAnchor.constraint(equalToConstant: 50).isActive = true
self.textFieldSenha.widthAnchor.constraint(equalToConstant: 190).isActive = true
//
self.loginButton.widthAnchor.constraint(equalToConstant: 145).isActive = true
self.loginButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
//
self.stackView.heightAnchor.constraint(equalToConstant: 200).isActive = true
self.stackView.widthAnchor.constraint(equalToConstant: 200).isActive = true
self.stackView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
self.stackView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
}
private func setupViews() {
self.view.addSubview(self.stackView)
self.viewTapped()
}
private lazy var textFieldLogin: UITextField = {
let textFieldLogin = UITextField()
textFieldLogin.tag = 1
textFieldLogin.translatesAutoresizingMaskIntoConstraints = false
textFieldLogin.layer.cornerRadius = 3.7
textFieldLogin.textAlignment = .center
textFieldLogin.placeholder = "Usuário"
textFieldLogin.backgroundColor = .white
textFieldLogin.delegate = self
return textFieldLogin
}()
private lazy var textFieldSenha: UITextField = {
let textFieldSenha = UITextField()
textFieldSenha.tag = 2
textFieldSenha.translatesAutoresizingMaskIntoConstraints = false
textFieldSenha.layer.cornerRadius = 3.7
textFieldSenha.textAlignment = .center
textFieldSenha.placeholder = "Senha"
textFieldSenha.backgroundColor = .white
textFieldSenha.delegate = self
return textFieldSenha
}()
private lazy var loginButton: UIButton = {
let loginButton = UIButton()
loginButton.translatesAutoresizingMaskIntoConstraints = false
loginButton.layer.cornerRadius = 3.8
loginButton.titleLabel?.font = UIFont(name: "Arial", size: 19)
loginButton.setTitle("Entrar", for: .normal)
loginButton.setTitleColor(.systemGreen, for: .normal)
loginButton.backgroundColor = .white
return loginButton
}()
private lazy var stackView: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [self.textFieldLogin, self.textFieldSenha, self.loginButton])
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.backgroundColor = .systemBlue
stackView.axis = .vertical
stackView.distribution = .equalSpacing
return stackView
}()
private func viewTapped() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
self.view.addGestureRecognizer(tapGesture)
}
#objc func handleTap(sender: UITapGestureRecognizer) {
UIView.animate(withDuration: 1.1, delay: 0, usingSpringWithDamping: 5.1, initialSpringVelocity: 5.0, options: .curveEaseIn, animations: {
self.stackView.frame.origin.y = self.stackView.frame.origin.y + 130
self.textFieldLogin.resignFirstResponder()
self.textFieldSenha.resignFirstResponder()
}, completion: nil)
}
}
extension LoginViewController: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
UIView.animate(withDuration: 1.1, delay: 0, usingSpringWithDamping: 5.1, initialSpringVelocity: 5.0, options: .curveEaseIn, animations: {
self.stackView.frame.origin.y = self.stackView.frame.origin.y - 130
}, completion: nil)
}
func textFieldDidEndEditing(_ textField: UITextField) {
UIView.animate(withDuration: 1.1, delay: 0, usingSpringWithDamping: 5.1, initialSpringVelocity: 5.0, options: .curveEaseIn, animations: {
self.stackView.frame.origin.y = self.stackView.frame.origin.y + 130
}, completion: nil)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
I used the following code into textFieldDidBeginEditing and everything works fine! Thank you all.
UIView.animate(withDuration: 0.5) {
self.view.layoutIfNeeded() //Think I forget this method
self.stackView.frame.origin.y = self.stackView.frame.origin.y - 130
}

Pop Up Menu Segue

I am making a pop up menu in Swift on Xcode 8.2.1, and I can't figure out how to make it segue correctly (It is being created programmatically right now so that it will have a animation when you open it).
import UIKit
var clickj = false
var st = String()
class ViewController: UIViewController {
var button = dropDownBtn()
override func viewDidLoad() {
super.viewDidLoad()
if clickj == true {
performSegue(withIdentifier: st, sender: nil)
}
// Do any additional setup after loading the view, typically from a nib.
//Configure the button
button = dropDownBtn.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
button.setTitle("Colors", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
//Add Button to the View Controller
self.view.addSubview(button)
//button Constraints
button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
button.widthAnchor.constraint(equalToConstant: 100).isActive = true
button.heightAnchor.constraint(equalToConstant: 40).isActive = true
//Set the drop down menu's options
button.dropView.dropDownOptions = ["Blue", "Choices"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
protocol dropDownProtocol {
func dropDownPressed(string : String)
}
class dropDownBtn: UIButton, dropDownProtocol {
func dropDownPressed(string: String) {
print(string)
st = string
clickj = true
self.dismissDropDown()
}
var dropView = dropDownView()
var height = NSLayoutConstraint()
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.darkGray
dropView = dropDownView.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
dropView.delegate = self
dropView.translatesAutoresizingMaskIntoConstraints = false
}
override func didMoveToSuperview() {
self.superview?.addSubview(dropView)
self.superview?.bringSubview(toFront: dropView)
dropView.topAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
dropView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
dropView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
height = dropView.heightAnchor.constraint(equalToConstant: 0)
}
var isOpen = false
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if isOpen == false {
isOpen = true
NSLayoutConstraint.deactivate([self.height])
if self.dropView.tableView.contentSize.height > 150 {
self.height.constant = 150
} else {
self.height.constant = self.dropView.tableView.contentSize.height
}
NSLayoutConstraint.activate([self.height])
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
self.dropView.layoutIfNeeded()
self.dropView.center.y += self.dropView.frame.height / 2
}, completion: nil)
} else {
isOpen = false
NSLayoutConstraint.deactivate([self.height])
self.height.constant = 0
NSLayoutConstraint.activate([self.height])
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
self.dropView.center.y -= self.dropView.frame.height / 2
self.dropView.layoutIfNeeded()
}, completion: nil)
}
}
func dismissDropDown() {
isOpen = false
NSLayoutConstraint.deactivate([self.height])
self.height.constant = 0
NSLayoutConstraint.activate([self.height])
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
self.dropView.center.y -= self.dropView.frame.height / 2
self.dropView.layoutIfNeeded()
}, completion: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class dropDownView: UIView, UITableViewDelegate, UITableViewDataSource {
var dropDownOptions = [String]()
var tableView = UITableView()
var delegate : dropDownProtocol!
override init(frame: CGRect) {
super.init(frame: frame)
tableView.backgroundColor = UIColor.darkGray
self.backgroundColor = UIColor.darkGray
tableView.delegate = self
tableView.dataSource = self
tableView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(tableView)
tableView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
tableView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
tableView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dropDownOptions.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = dropDownOptions[indexPath.row]
cell.backgroundColor = UIColor.darkGray
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.delegate.dropDownPressed(string: dropDownOptions[indexPath.row])
self.tableView.deselectRow(at: indexPath, animated: true)
}
}
The problem is I have every thing set up to segue properly but I am still working on where I should place the if statement that will segue when the condition is true (mainly I am looking for some function I could place it in that would be called in the main ViewController when the button is clicked).
You are close... about all you need is to implement another delegate protocol.
Currently, your dropDownView is using dropDownProtocol to tell the button that a row was selected. What you also want is a protocol so the button can tell the view controller that it has dismissed itself, and to pass the selected string from the table.
Here is the code you presented, with very few changes. See the comments in the code. With your start, things should be pretty clear - feel free to ask for clarification if needed:
import UIKit
class ViewController: UIViewController, dropDownCallBackProtocol {
var button = dropDownBtn()
override func viewDidLoad() {
super.viewDidLoad()
//Configure the button
button = dropDownBtn.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
button.setTitle("Colors", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
//Add Button to the View Controller
self.view.addSubview(button)
//button Constraints
button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
button.widthAnchor.constraint(equalToConstant: 100).isActive = true
button.heightAnchor.constraint(equalToConstant: 40).isActive = true
//Set the drop down menu's options
button.dropView.dropDownOptions = ["Blue", "Choices"]
// set the button's "vcCallBackDelegate"
button.vcCallBackDelegate = self
}
func dropDownCompleted(string: String) {
print("Inside View Controller:", string)
// this will be called when the un-show-drop-down animation is finished
// so you can perform your segue here
// performSegue(withIdentifier: string, sender: nil)
}
}
// protocol for the button to "call back" to the view controller (its parent)
protocol dropDownCallBackProtocol {
func dropDownCompleted(string : String)
}
// protocol for the table to "call back" to the button (its parent)
protocol dropDownProtocol {
func dropDownPressed(string : String)
}
class dropDownBtn: UIButton, dropDownProtocol {
var vcCallBackDelegate: dropDownCallBackProtocol?
func dropDownPressed(string: String) {
print("Inside button class:", string)
self.dismissDropDown(string)
}
var dropView = dropDownView()
var height = NSLayoutConstraint()
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.darkGray
dropView = dropDownView.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
dropView.delegate = self
dropView.translatesAutoresizingMaskIntoConstraints = false
}
override func didMoveToSuperview() {
self.superview?.addSubview(dropView)
self.superview?.bringSubview(toFront: dropView)
dropView.topAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
dropView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
dropView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
height = dropView.heightAnchor.constraint(equalToConstant: 0)
}
var isOpen = false
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if isOpen == false {
isOpen = true
NSLayoutConstraint.deactivate([self.height])
if self.dropView.tableView.contentSize.height > 150 {
self.height.constant = 150
} else {
self.height.constant = self.dropView.tableView.contentSize.height
}
NSLayoutConstraint.activate([self.height])
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
self.dropView.layoutIfNeeded()
self.dropView.center.y += self.dropView.frame.height / 2
}, completion: nil)
} else {
isOpen = false
NSLayoutConstraint.deactivate([self.height])
self.height.constant = 0
NSLayoutConstraint.activate([self.height])
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
self.dropView.center.y -= self.dropView.frame.height / 2
self.dropView.layoutIfNeeded()
}, completion: nil)
}
}
func dismissDropDown(_ selectedString: String) {
isOpen = false
NSLayoutConstraint.deactivate([self.height])
self.height.constant = 0
NSLayoutConstraint.activate([self.height])
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
self.dropView.center.y -= self.dropView.frame.height / 2
self.dropView.layoutIfNeeded()
}, completion: {
b in
// tell the delegate the animation is complete, and pass the selected string
self.vcCallBackDelegate?.dropDownCompleted(string: selectedString)
})
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class dropDownView: UIView, UITableViewDelegate, UITableViewDataSource {
var dropDownOptions = [String]()
var tableView = UITableView()
var delegate : dropDownProtocol!
override init(frame: CGRect) {
super.init(frame: frame)
tableView.backgroundColor = UIColor.darkGray
self.backgroundColor = UIColor.darkGray
tableView.delegate = self
tableView.dataSource = self
tableView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(tableView)
tableView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
tableView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
tableView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dropDownOptions.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = dropDownOptions[indexPath.row]
cell.backgroundColor = UIColor.darkGray
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.delegate.dropDownPressed(string: dropDownOptions[indexPath.row])
self.tableView.deselectRow(at: indexPath, animated: true)
}
}

Subview loads but none of buttons work ios swift

I have a custom keyboard project with a Storyboard containing a collectionView and a scrollView. I also have a textField for a search bar that pulls up a nib containing a keyboard layout that I can use to search within the keyboard. When the subview containing the keyboard becomes active, I cannot press any of the buttons. I have tried bringing the subView to front upon becoming active and have tried sending the main view to the back when hiding the collectionView. What can I do to use the buttons in the subview?
Before viewDidLoad
let filename = "buttonClick"
let ext = "m4a"
var soundId: SystemSoundID = 0
#objc func typeKey(sender : UIButton)
{
AudioServicesPlaySystemSound(soundId)
self.key.txtSearch.text = "\(self.key.txtSearch.text!)\((sender.titleLabel?.text!)!)"
if(self.key.txtSearch.text != "")
{
isCaps = false
updateKeyBoard()
}
}
#objc func typeSpaceKey(sender : UIButton)
{
AudioServicesPlaySystemSound(soundId)
self.key.txtSearch.text = "\(self.key.txtSearch.text!) "
}
#objc func typeDoneKey(sender : UIButton)
{
AudioServicesPlaySystemSound(soundId)
hideTextField()
}
#objc func typeNumKey(sender : UIButton)
{
AudioServicesPlaySystemSound(soundId)
isNum = !isNum
updateKeyBoard()
// self.key.txtSearch.text = "\(self.key.txtSearch.text!)\((sender.titleLabel?.text!)!)"
}
#objc func typeBackSpaceKey(sender : UIButton)
{
AudioServicesPlaySystemSound(soundId)
self.key.txtSearch.text = "\(self.key.txtSearch.text!.dropLast())"
if(self.key.txtSearch.text == "")
{
isCaps = true
updateKeyBoard()
}
}
#objc func typeCapsKey(sender : UIButton)
{
AudioServicesPlaySystemSound(soundId)
if(isNum)
{
isFirst = !isFirst
}
else
{
isCaps = !isCaps
}
updateKeyBoard()
}
#objc func updateKeyBoard()
{
if(isCaps)
{
self.key.btnCap.setImage(#imageLiteral(resourceName: "cap_F"), for: .normal)
}
else
{
self.key.btnCap.setImage(#imageLiteral(resourceName: "caps"), for: .normal)
}
var count = 0
for btn in buttons
{
if(!isNum)
{
if(isCaps)
{
btn.setTitle("\(arrCapOn[count])", for: .normal)
}
else
{
btn.setTitle("\(arrCapOff[count])", for: .normal)
}
}
else
{
if(isFirst)
{
btn.setTitle("\(arrNumCapOn[count])", for: .normal)
}
else
{
btn.setTitle("\(arrNumCapOff[count])", for: .normal)
}
}
count = count + 1
}
}
#objc func activeTextField()
{
self.key.btnBack.isHidden = false
self.key.txtSearch.becomeFirstResponder()
self.key.constLeftAchor.constant = 40
self.key.constSideKeyboard.constant = 8
self.key.constLeftAchorView.constant = 32
UIView.animate(withDuration: 0.2) {
self.key.viewKeyboard.transform = CGAffineTransform(translationX: 0, y: 0)
self.key.btnBack.alpha = 1.0
self.key.layoutIfNeeded()
}
self.key.btnTextFieldSelect.isHidden = true
}
#objc func hideTextField()
{
self.key.btnBack.isHidden = true
self.key.txtSearch.resignFirstResponder()
self.key.btnTextFieldSelect.isHidden = false
self.key.constSideKeyboard.constant = 400
self.key.constLeftAchor.constant = 24
self.key.constLeftAchorView.constant = 16
UIView.animate(withDuration: 0.2) {
self.key.viewKeyboard.transform = CGAffineTransform(translationX: self.view.frame.width, y: 0)
self.key.btnBack.alpha = 0.0
self.key.layoutIfNeeded()
}
}
#objc func hideArticles(){
self.categoriesScrollView.isHidden = true
self.collectionview.isHidden = true
//self.collectionview.isUserInteractionEnabled = false
//self.view.bringSubview(toFront: key)
//self.key.isUserInteractionEnabled = true
}
#objc func showArticles(){
self.categoriesScrollView.isHidden = false
self.collectionview.isHidden = false
}
#objc func handleTap(_ sender: UITapGestureRecognizer) {
self.key.txtSearch.text = "Hello"
self.inputView?.resignFirstResponder()
print("Hello World")
}
func textFieldDidBeginEditing(_ textField: UITextField) {
}
override func textWillChange(_ textInput: UITextInput?) {
// The app is about to change the document's contents. Perform any preparation here.
}
override func textDidChange(_ textInput: UITextInput?) {
// The app has just changed the document's contents, the document context has been updated.
var textColor: UIColor
let proxy = self.textDocumentProxy
if proxy.keyboardAppearance == UIKeyboardAppearance.dark {
textColor = UIColor.white
} else {
textColor = UIColor.black
}
// self.nextKeyboardButton.setTitleColor(textColor, for: [])
}
after viewDidLoad
if let soundUrl = Bundle.main.url(forResource: filename, withExtension: ext) {
AudioServicesCreateSystemSoundID(soundUrl as CFURL, &soundId)
}
self.key = Bundle.main.loadNibNamed("keyboard", owner: nil, options: nil)![0] as! keyboard
self.key.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 160)
//self.key.txtSearch.isUserInteractionEnabled = false
self.key.constLeftAchor.constant = 24
// self.key.viewKeyboard.transform = CGAffineTransform(translationX: self.view.frame.width, y: 0)
self.key.btnBack.isHidden = true
self.key.btnBack.alpha = 0.0
self.key.btnTextFieldSelect.addTarget(self, action: #selector(activeTextField), for: .touchUpInside)
self.key.btnTextFieldSelect.addTarget(self, action: #selector(hideArticles), for: .touchUpInside)
self.key.btnBack.addTarget(self, action: #selector(hideTextField), for: .touchUpInside)
self.key.btnBack.addTarget(self, action: #selector(showArticles), for: .touchUpInside)
buttons.append(self.key.btnQ)
buttons.append(self.key.btnW)
buttons.append(self.key.btnE)
buttons.append(self.key.btnR)
buttons.append(self.key.btnT)
buttons.append(self.key.btnY)
buttons.append(self.key.btnU)
buttons.append(self.key.btnI)
buttons.append(self.key.btnO)
buttons.append(self.key.btnP)
buttons.append(self.key.btnA)
buttons.append(self.key.btnS)
buttons.append(self.key.btnD)
buttons.append(self.key.btnF)
buttons.append(self.key.btnG)
buttons.append(self.key.btnH)
buttons.append(self.key.btnJ)
buttons.append(self.key.btnK)
buttons.append(self.key.btnL)
buttons.append(self.key.btnZ)
buttons.append(self.key.btnX)
buttons.append(self.key.btnC)
buttons.append(self.key.btnV)
buttons.append(self.key.btnB)
buttons.append(self.key.btnN)
buttons.append(self.key.btnM)
for btn in buttons
{
btn.addTarget(self, action: #selector(typeKey), for: .touchUpInside)
}
self.key.btnCap.addTarget(self, action: #selector(typeCapsKey), for: .touchUpInside)
self.key.btnBackSpace.addTarget(self, action: #selector(typeBackSpaceKey), for: .touchUpInside)
self.key.btnSpace.addTarget(self, action: #selector(typeSpaceKey), for: .touchUpInside)
self.key.btnDone.addTarget(self, action: #selector(typeDoneKey), for: .touchUpInside)
//self.key.btnDone.addTarget(self, action: #selector(fetchSearch), for: .touchUpInside)
self.key.btnNum.addTarget(self, action: #selector(typeNumKey), for: .touchUpInside)
view.addSubview(key)
activeTextField()
hideTextField()
Please make sure the added subviews are userinteration enabled and it is not send to back in superview.

How to enable leftBarButtonItem from other ViewController?

When I click the left bar button on MessageController I make it isEnabled to false. Then I present PopUpViewController and on button click the function removeAnimate() runs. And in that function I would like to set MessageController left bar button to isEnabled to true. I have tried but it does not work. Can someone help ?
class PopUpViewController: UIViewController {
...
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.8)
self.showAnimate()
}
func showAnimate()
{
view.backgroundColor = UIColor.black.withAlphaComponent(0.8)
self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.view.alpha = 0.0;
UIView.animate(withDuration: 0.25, animations: {
self.view.alpha = 1.0
self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
});
}
func removeAnimate()
{
let messagView = MessageController()
UIView.animate(withDuration: 0.25, animations: {
self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.view.alpha = 0.0;
}, completion:{(finished : Bool) in
if (finished)
{
self.view.removeFromSuperview()
messagView.navigationItem.leftBarButtonItem?.isEnabled = true
}
});
}
lazy var cancleButton: UIButton = {
let button = UIButton(type: .system)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Cancel", for: UIControlState())
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
button.tintColor = .white
button.addTarget(self, action: #selector(removeAnimate), for: .touchUpInside)
return button
}()
}
MessageViewController
class MessageController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(preformSettings))
}
func preformSettings(){
let popViewController = PopUpViewController()
self.addChildViewController(popViewController)
popViewController.view.frame = self.view.frame
self.view.addSubview(popViewController.view)
popViewController.didMove(toParentViewController: self)
navigationItem.leftBarButtonItem?.isEnabled = false
}
}
You may use protocols. Write a protocol
protocol PopViewControllerDelegate: class {
func enableBackButton()
}
In PopUpViewController make an object of this protocol.
class PopUpViewController: UIViewController {
var delegate: PopViewControllerDelegate?
....
func removeAnimate() {
...
delegate?.enableBackButton()
}
}
In the MessageController implement this protocol.
class MessageController: UITableViewController {
func preformSettings(){
let popViewController = PopUpViewController()
popViewController.delegate = self
self.addChildViewController(popViewController)
....
}
}
extension MessageController: PopViewControllerDelegate {
func enableBackButton() {
navigationItem.leftBarButtonItem?.isEnabled = true
}
}

Swift PresentViewController Dismisses Keyboard

I'm creating a Sign Up wizard with multiple UIViewControllers.
I currently have it setup so a user enter's his email, clicks the "Go" button on the keyboard and the next UIViewController slides in from the right where the user will enter his name. The problem though is that when I call presentViewController to bring the next UIViewController in, it dismisses the keyboard.
I'd like the keyboard to stay open the entire time while switching ViewControllers. If you look at Facebook's iOS app, they do what I'm trying to do with their signup page.
Any help or suggestions will be greatly appreciated. I read something about using an overlay window, but am not sure how to go about it since I will have multiple UIViewController's in my Sign Up wizard.
Here's my initial controller in the Sign Up Wizard:
class SignUpEmailViewController: UIViewController {
var titleLabel = UILabel.newAutoLayoutView()
var emailField = SignUpTextField(placeholder: "Enter your email address")
var emailLabel = UILabel.newAutoLayoutView()
var continueButton = SignUpContinueButton.newAutoLayoutView()
var footerView = SignUpFooterView.newAutoLayoutView()
let presentAnimationController = PushInFromLeftAnimationController()
let dismissAnimationController = PushInFromRightAnimationController()
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
setupGestures()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
emailField.becomeFirstResponder()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setupViews() {
view.backgroundColor = UIColor.colorFromCode(0xe9eaed)
titleLabel.text = "Get Started"
titleLabel.font = UIFont(name: "AvenirNextLTPro-Demi", size: 18)
titleLabel.textColor = Application.greenColor
emailField.enablesReturnKeyAutomatically = true
emailField.returnKeyType = .Go
emailField.delegate = self
emailLabel.text = "You'll use this email when you log in and if you ever need to reset your password."
emailLabel.font = UIFont(name: "AvenirNextLTPro-Regular", size: 13)
emailLabel.textColor = .colorFromCode(0x4e5665)
emailLabel.numberOfLines = 0
emailLabel.textAlignment = .Center
continueButton.addTarget(self, action: "continueButtonPressed", forControlEvents: .TouchUpInside)
continueButton.hidden = true
view.addSubview(titleLabel)
view.addSubview(emailField)
view.addSubview(emailLabel)
view.addSubview(continueButton)
view.addSubview(footerView)
setupConstraints()
}
func setupGestures() {
let gestureRecognizer = UISwipeGestureRecognizer(target: self, action: "swipeHandler")
gestureRecognizer.direction = .Down
view.addGestureRecognizer(gestureRecognizer)
let tapGesture = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tapGesture)
}
func setupConstraints() {
titleLabel.autoAlignAxisToSuperviewAxis(.Vertical)
titleLabel.autoPinEdgeToSuperviewEdge(.Top, withInset: screenSize.height * 0.2)
emailField.autoAlignAxisToSuperviewAxis(.Vertical)
emailField.autoPinEdge(.Top, toEdge: .Bottom, ofView: titleLabel, withOffset: 15)
emailField.autoSetDimensionsToSize(CGSize(width: screenSize.width * 0.85, height: 40))
emailLabel.autoAlignAxisToSuperviewAxis(.Vertical)
emailLabel.autoPinEdge(.Top, toEdge: .Bottom, ofView: emailField, withOffset: 10)
emailLabel.autoSetDimension(.Width, toSize: screenSize.width * 0.85)
continueButton.autoAlignAxisToSuperviewAxis(.Vertical)
continueButton.autoPinEdge(.Top, toEdge: .Bottom, ofView: emailField, withOffset: 10)
continueButton.autoSetDimensionsToSize(CGSize(width: screenSize.width * 0.85, height: 30))
footerView.autoSetDimension(.Height, toSize: 44)
footerView.autoPinEdgeToSuperviewEdge(.Bottom)
footerView.autoPinEdgeToSuperviewEdge(.Leading)
footerView.autoPinEdgeToSuperviewEdge(.Trailing)
}
override func prefersStatusBarHidden() -> Bool {
return true
}
func swipeHandler() {
dismissViewControllerAnimated(true, completion: nil)
}
func continueButtonPressed() {
presentNextViewController()
}
func dismissKeyboard() {
view.endEditing(true)
}
func presentNextViewController() {
let toViewController = SignUpNameViewController()
toViewController.transitioningDelegate = self
toViewController.firstNameField.becomeFirstResponder()
presentViewController(toViewController, animated: true, completion: nil)
}
}
extension SignUpEmailViewController: UIViewControllerTransitioningDelegate {
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return presentAnimationController
}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return dismissAnimationController
}
}
extension SignUpEmailViewController: UITextFieldDelegate {
func textFieldShouldReturn(textField: UITextField) -> Bool {
presentNextViewController()
return true
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
continueButton.hidden = true
emailLabel.hidden = false
return true
}
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
continueButton.hidden = false
emailLabel.hidden = true
return true
}
}
And here's the controller that I'm trying to present:
class SignUpNameViewController: UIViewController, UIViewControllerTransitioningDelegate {
var titleLabel = UILabel.newAutoLayoutView()
var textFieldContainer = UIView.newAutoLayoutView()
var firstNameField = SignUpTextField(placeholder: "First name")
var lastNameField = SignUpTextField(placeholder: "Last name")
var continueButton = SignUpContinueButton.newAutoLayoutView()
var footerView = SignUpFooterView.newAutoLayoutView()
let presentAnimationController = PushInFromLeftAnimationController()
let dismissAnimationController = PushInFromRightAnimationController()
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
firstNameField.becomeFirstResponder()
}
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
setupGestures()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setupViews() {
view.backgroundColor = UIColor.colorFromCode(0xe9eaed)
titleLabel.text = "What's your name?"
titleLabel.font = UIFont(name: "AvenirNextLTPro-Demi", size: 18)
titleLabel.textColor = Application.greenColor
firstNameField.returnKeyType = .Next
firstNameField.enablesReturnKeyAutomatically = true
lastNameField.returnKeyType = .Next
lastNameField.enablesReturnKeyAutomatically = true
continueButton.addTarget(self, action: "continueButtonPressed", forControlEvents: .TouchUpInside)
view.addSubview(titleLabel)
view.addSubview(textFieldContainer)
textFieldContainer.addSubview(firstNameField)
textFieldContainer.addSubview(lastNameField)
view.addSubview(continueButton)
view.addSubview(footerView)
setupConstraints()
}
func setupGestures() {
let gestureRecognizer = UISwipeGestureRecognizer(target: self, action: "swipeHandler")
gestureRecognizer.direction = .Right
view.addGestureRecognizer(gestureRecognizer)
let tapGesture = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tapGesture)
}
func setupConstraints() {
titleLabel.autoAlignAxisToSuperviewAxis(.Vertical)
titleLabel.autoPinEdgeToSuperviewEdge(.Top, withInset: screenSize.height * 0.2)
textFieldContainer.autoPinEdge(.Top, toEdge: .Bottom, ofView: titleLabel, withOffset: 15)
textFieldContainer.autoAlignAxisToSuperviewAxis(.Vertical)
textFieldContainer.autoSetDimensionsToSize(CGSize(width: screenSize.width * 0.8, height: 40))
let spaceBetweenTextFields: CGFloat = 5
let textFieldSize = ((screenSize.width * 0.8) - spaceBetweenTextFields) / 2
let textFields: NSArray = [firstNameField, lastNameField]
textFields.autoDistributeViewsAlongAxis(.Horizontal, alignedTo: .Horizontal, withFixedSize: textFieldSize, insetSpacing: false)
firstNameField.autoPinEdgeToSuperviewEdge(.Top)
firstNameField.autoPinEdgeToSuperviewEdge(.Bottom)
lastNameField.autoPinEdgeToSuperviewEdge(.Top)
lastNameField.autoPinEdgeToSuperviewEdge(.Bottom)
continueButton.autoAlignAxisToSuperviewAxis(.Vertical)
continueButton.autoPinEdge(.Top, toEdge: .Bottom, ofView: textFieldContainer, withOffset: 10)
continueButton.autoSetDimensionsToSize(CGSize(width: screenSize.width * 0.8, height: 30))
footerView.autoSetDimension(.Height, toSize: 44)
footerView.autoPinEdgeToSuperviewEdge(.Bottom)
footerView.autoPinEdgeToSuperviewEdge(.Leading)
footerView.autoPinEdgeToSuperviewEdge(.Trailing)
}
override func prefersStatusBarHidden() -> Bool {
return true
}
func dismissKeyboard() {
view.endEditing(true)
}
func swipeHandler() {
dismissViewControllerAnimated(true, completion: nil)
}
func continueButtonPressed() {
let toViewController = SignUpPasswordViewController()
toViewController.transitioningDelegate = self
presentViewController(toViewController, animated: true, completion: {})
}
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return presentAnimationController
}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return dismissAnimationController
}
}
And here is my custom transition:
class PushInFromLeftAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.35
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
let finalFrameForVC = transitionContext.finalFrameForViewController(toViewController)
let containerView = transitionContext.containerView()
let bounds = UIScreen.mainScreen().bounds
toViewController.view.frame = CGRectOffset(finalFrameForVC, bounds.size.width, 0)
containerView!.addSubview(toViewController.view)
UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: {
fromViewController.view.frame = CGRectOffset(finalFrameForVC, -bounds.size.width, 0)
toViewController.view.frame = finalFrameForVC
}, completion: {
finished in
transitionContext.completeTransition(true)
})
}
}
I think the keyboard is dismissed because the corresponding UIResponder will resign at the same time the ViewController will disappear.
Have you tried setting the next UITextField (in the next ViewController) as the first responder then? thus the keyboard will be linked to a new UIResponder before the previous one would end editing...

Resources