Toolbar Changes Color During Segue - ios

I am currently working on making the register section of an iOS app in Swift 3.0.2.
Here's a gif
of the problem I am having. Watch the toolbar above the keyboard as the segue occurs. It flashes to a darker shade before it fully loads. This occurs whether or not the keyboard is active throughout the process (ViewController has toolbar as inputAccessoryView).
Here is the toolbar implementation (it's the same for each View).
fileprivate let toolbar = { () -> UIToolbar in
let toolbar = UIToolbar()
toolbar.barStyle = .default
toolbar.isTranslucent = true
toolbar.tintColor = UIColor(netHex: Constant.Color.darkerCrimsonColor)
toolbar.isUserInteractionEnabled = true
toolbar.sizeToFit()
return toolbar
}()
override var inputAccessoryView: UIView? {
return toolbar
}
fileprivate func setupToolbar() {
let otherButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.done, target: self, action: #selector(nextViewController))
toolBar.setItems([otherButton, otherButton, doneButton], animated: true)
nameTextField.inputAccessoryView = toolbar
}
The entire app is made programmatically. I remade the essentials of the project again through Storyboard and ran into the same problem.
My question is: is this intended behavior? If not, what can I do about this? Is there a way to suppress this behavior completely?
Thanks in advance for your time and consideration!

Related

Why does setting a custom backBarButtonItem for a UINavigationItem result in double back buttons?

I have a very simple setup. A UINavigationController with a root UIViewController that modifies its navigation item with a custom back button item on viewDidLoad.
let backButton = UIBarButtonItem(image: backArrowImage,
style: .plain,
target: nil,
action: nil)
navigationItem.backBarButtonItem = backButton
I'm expecting this to completely replace the system back button with title and the default back arrow icon.
However when I push a new view controller on the stack, the navigation bar draws both the new custom back icon and the system back icon.
This is what I'm seeing:
This is what I would expect it to look like:
You can hide the back button
navigationItem.hidesBackButton = true
and use leftBarButtonItem for custom UIBarButtonItem
UPD
import UIKit
final class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.hidesBackButton = true
let backItem = UIBarButtonItem(image: backArrowImage, style: .plain, target: self, action: #selector(backButtonPressed))
navigationItem.leftBarButtonItem = backItem
}
#objc func backButtonPressed() {
navigationController?.popViewController(animated: true)
}
}
let backBarButtonItem: UIBarButtonItem = .init(
image: UIImage(systemName: "chevron.backward"),
style: .plain,
target: target,
action: action
)
navigationBar.topItem?.backBarButtonItem = backBarButtonItem
navigationBar.backIndicatorImage = UIImage()
navigationBar.backIndicatorTransitionMaskImage = UIImage()
This works for me, to setup custom "<" and hide the default one and still keep the backBarButtonItem behaviours
The solution was to set global UINavigationBar appearance.
Apparently this has to be done at app launch.
UINavigationBar.appearance().backIndicatorImage = backArrowImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backArrowImage
With this approach we can preserve title animations and general back button behavior that would not be preserved if supplementing the back button with the leftBarButtonItem.

Pass function argument into selector in Swift

I have five pickers in total. I want to create a done button for each one of them. I want to use a different selector for each one to to do different actions. To set up the done buttons I was trying to use the same method for all of them, but I do not know how to pass a function argument into the selector in Swift.
func createDoneButton(txtField: UITextField, donePressed: /* What do I need here? */) {
let toolbar = UIToolbar() // create toolbar
toolbar.sizeToFit() // toolbar fits the size of the screen
let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed)) // action when the done button was pressed
toolbar.setItems([doneBtn], animated: true)
txtField.inputAccessoryView = toolbar
}
You can find the answer in your question :)
Simply use Selector parameter type, and no need #selector()
func createDoneButton(txtField: UITextField, donePressed: Selector){
let toolbar = UIToolbar() // create toolbar
toolbar.sizeToFit() // toolbar fits the size of the screen
let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: donePressed) // action when the done button was pressed
toolbar.setItems([doneBtn], animated: true)
txtField.inputAccessoryView = toolbar
}

UIBarButtonItem doesn't respond to tap

I have a viewController that's embedded in a navigationController. From my viewController I programmatically add three UIBarButtonItem and a UISearchBar. Under the navigationbar I have a WKWebView that is covering the whole screen. The issue it that the UIBarButtonItems doesn't respond to any clicks before I've taped or in any way interacted with the WKWebView. After I tap or scroll the WKWebView the UIBarButtonItems work fine until I perform a search in the UISearchBar. The UIBarButtonItems stops responding again until I interact with the WKWebView.
I have tried adding navigationController?.navigationBar.becomeFirstResponder() in DidAppear and in searchBarTextDidEndEditing as suggested on some forum post. It has no effect on the issue.
This is what I do in DidLoad to initiate the navbar:
self.navigationController?.setNavigationBarHidden(false, animated: true)
searchBar.delegate = self
searchBar.sizeToFit()
searchBar.setImage(UIImage(named: "ssl"), for: .search, state: .normal)
searchBar.searchBarStyle = .minimal
searchBar.showsCancelButton = false
let textField = searchBar.value(forKey: "searchField") as? UITextField
textField?.textAlignment = .center
textField?.leftViewMode = UITextField.ViewMode.never
textField?.clearButtonMode = UITextField.ViewMode.whileEditing
textField?.keyboardType = .URL
textField?.autocapitalizationType = .none
textField?.leftView?.contentMode = .scaleAspectFit
let back = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(naviagteBack))
let forward = UIBarButtonItem(image: UIImage(named: "forward"), style: .plain, target: self, action: #selector(naviagteForward))
let reload = UIBarButtonItem(image: UIImage(named: "reload"), style: .plain, target: self, action: #selector(navigateReload))
navigationItem.titleView = searchBar
reload.tintColor = .black
navigationItem.rightBarButtonItem = reload
back.tintColor = .lightGray
forward.tintColor = .lightGray
navigationItem.leftBarButtonItems = [back, forward]
I want the UIBarButtonItems to be active all of the time. Not just after I interact with the WKWebView. And no, it has nothing to do with the capabilities of goBack() e.t.c. I have added print("!") to the button functions and it outputs nothing while in the "unactivated" state.

Swift 4 selectRow for pickerView is not working in viewDidAppear

I am using UIPickerView and it's being triggered by pushing a button like so:
#IBAction func communityButtonPressed(_ sender: Any) {
//Set the picker view delegate to self
pickerView.delegate = self
//Set the picker view data source to self
pickerView.dataSource = self
//Define the tool bar
let toolBar = UIToolbar()
//Set tool bar style
toolBar.barStyle = UIBarStyle.default
//Set tool bar translucent to true
toolBar.isTranslucent = true
//Resizes and moves the tool bar view so it just encloses its subviews.
toolBar.sizeToFit()
//Define done button for tool bar
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: #selector(Dashboard.donePicker))
//Define space inbetween the buttons for tool bar
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
//Define cancel button for tool bar
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItem.Style.plain, target: self, action: #selector(Dashboard.cancelPicker))
//Assign tool bar buttons to tool bar
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
//Set the tool bar to be interactive
toolBar.isUserInteractionEnabled = true
//Add the tool bar to the picker view
pickTextField.inputAccessoryView = toolBar
//Add picker view to view
view.addSubview(pickTextField)
//Assign the picker view to picker text field
pickTextField.inputView = pickerView
//Set picker text field as first responder
pickTextField.becomeFirstResponder()
}
Now I am trying to set a selected row in the viewDidAppear but it's not setting the selected row.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Call method to get the user bundle settings
registerSettingsBundle()
//Apply the users bundle settings
updateDisplayFromDefaults()
//Clear the picker options
pickOption = [String]()
//For each community in the app delegate community array
for item in (appDelegate.communityDescriptionArray?["kCommunityDescriptions"] as! [AnyObject])
{
//Add it to the picker options
pickOption?.append(item as! String)
}
self.selectedIndex = (appDelegate.communityDescriptionArray?["kCommunityDescriptions"]!.index(of: UserDefaults.standard.string(forKey: "community")!))!
self.pickerView.selectRow(3, inComponent: 0, animated: true)
}
I did noticed that my viewDidAppear is loaded first then numberOfRowsInComponent, then titleForRow, is that why the selected row is not taking?

How to customize UItoolbar

Twitter Registration Page
Hi everyone I have some cushions about UItoolBar on swift 3/4
I'm trying to make an singIn design like twitter,
1) i create toolbar with code;
let toolBar = UIToolbar()
toolBar.sizeToFit()
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.doneClicked))
toolBar.setItems([flexibleSpace, doneButton], animated: false)
kullaniciAdi.inputAccessoryView = toolBar
eposta.inputAccessoryView = toolBar
sifre.inputAccessoryView = toolBar
but I can't add custom buttons on that. Like changing the "Done" text in my language.
2) So I created manually using Storyboard a toolBar with inside a button and Flexible Space. But ı need to "attach" that toolbar to the keyboard, so when i start to edit the textfield the toolbar will automatically show up. NOTE: it will be fantastic if the toolbar will show too when not-editing textField. Like that:
My app trying too attach toolbar
THANK YOU ALL
In Code
let sonrakiButton = UIBarButtonItem(title: "Sonraki", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.sendDoneBtnAction))
For more info, read this link

Resources