Swift 4 selectRow for pickerView is not working in viewDidAppear - ios

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?

Related

swipe back hides some navigation item in swift

I have a problem regarding swipe back gestures in swift. After I push the new uicollectionviewcontroller when certain cell is pressed at the parent uicollectionviewcontroller, I swipe back to the parent viewcontroller. But only a few navigationitems appear on the navigation bar. However, when I go back by pressing the "back" button, all the navigation items appear. Here's my code:
override func viewDidLoad() {
setupNavBarButtons()}
func setupNavBarButtons(){
let flexible = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: self, action: nil)
flexible.isEnabled = false
let logoImg = UIImage(named: "wee")?.withRenderingMode(.alwaysOriginal)
let homeBarButtonItem = UIBarButtonItem(image:logoImg, style: .plain, target: self, action: #selector(handleHome))
homeBarButtonItem.isEnabled = false
let cameraImg = UIImage(named: "nav bar_circles")?.withRenderingMode(.alwaysOriginal)
let cameraButtonItem = UIBarButtonItem(image:cameraImg, style: .plain, target: self, action: nil)
cameraButtonItem.isEnabled = false
navigationItem.rightBarButtonItems = [flexible, cameraButtonItem, flexible, cameraButtonItem, flexible, cameraButtonItem]
navigationItem.leftBarButtonItems = [flexible, cameraButtonItem, flexible, cameraButtonItem, flexible, cameraButtonItem, flexible, homeBarButtonItem]
navigationItem.accessibilityElementsHidden = false
navigationController?.hidesBarsOnSwipe = false
navigationController?.isNavigationBarHidden = false
}
func showAppDetailForApp(pht: UIImage){
let layout = UICollectionViewFlowLayout()
let detailLauncher = ShowPhoto(collectionViewLayout: layout)
detailLauncher.info = pht
navigationController?.navigationBar.barTintColor = UIColor.white
navigationController?.pushViewController(detailLauncher, animated: true)
}
Thanks in advance.
Please Set below code line in your pushed view controller's viewDidLoad() method.
self.navigationController.interactivePopGestureRecognizer?.isEnabled = false

Toolbar Changes Color During Segue

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!

IOS Swift Programmatically created navigationController TOOLBAR (bottom bar) item actions not working

I am having trouble creating a UINavigationController toolbar programmatically. I have previously used storyboards to do this successfully but would like to try doing it all in code.
I have created the UIBarButtonItems programmatically, but the actual functions or actions they are supposed to perform when pressed are not working.
To clarify, I am NOT attempting to add UIBarButtonItems to the top bar. I've seen dozens of questions all asking the same thing. I am referring to the toolbar at the bottom that comes with the UINavigationController. This means no "rightBarButtonItem" or "leftBarButtonItem"
Here is the code:
class ProgOneViewController: UIViewController {
var chatRoomsButton: UIBarButtonItem = {
var button = UIBarButtonItem(title: "Chats", style: .plain, target: self, action: #selector(segueToChatRoomController(_:)))
return button
}()
var exploreButton: UIBarButtonItem = {
var button = UIBarButtonItem(title: "Explore", style: .plain, target: self, action: #selector(segueToExploreController(_:)))
return button
}()
var profileButton: UIBarButtonItem = {
var button = UIBarButtonItem(title: "Profile", style: .plain, target: self, action: #selector(segueToProfileController(_:)))
return button
}()
// Flexible spaces that are added in between each button.
var flexibleSpace1: UIBarButtonItem = {
var flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
return flexibleSpace
}()
var flexibleSpace2: UIBarButtonItem = {
var flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
return flexibleSpace
}()
var flexibleSpace3: UIBarButtonItem = {
var flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
return flexibleSpace
}()
var flexibleSpace4: UIBarButtonItem = {
var flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
return flexibleSpace
}()
// These are the functions that are not being called for some mysterious reason.
func segueToChatRoomController(_ sender: Any) {
print("segueing to chat rooms controller")
let chatRoomsController = ChatRoomsViewController()
let navController = UINavigationController(rootViewController: chatRoomsController)
self.present(navController, animated: false, completion: nil)
}
func segueToExploreController(_ sender: Any) {
print("segueing to explore controller")
let exploreController = ExploreCollectionViewController()
let navController = UINavigationController(rootViewController: exploreController)
self.present(navController, animated: false, completion: nil)
}
func segueToProfileController(_ sender: Any) {
print("segueing to profile controller")
let profileController = ProfileTableViewController()
let navController = UINavigationController(rootViewController: profileController)
self.present(navController, animated: false, completion: nil)
}
func setUpToolbar() {
print("setting up toolbar")
self.navigationController?.setToolbarHidden(false, animated: false)
self.navigationController?.toolbar.isUserInteractionEnabled = true
let toolBarItems = [flexibleSpace1, chatRoomsButton, flexibleSpace2, exploreButton, flexibleSpace3, profileButton, flexibleSpace4]
self.setToolbarItems(toolBarItems, animated: true)
// For some reason, these two methods leave the toolbar empty.
//self.navigationController?.setToolbarItems(toolBarItems, animated: true)
//self.navigationController?.toolbar.items = toolBarItems
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
setUpToolbar()
}
}
EDIT*
Here is the code used to instantiate the ProgOneViewController inside a UINavigationController
func pushToTestProgrammaticallyCreatedViews() {
let progOneViewController = ProgOneViewController()
let navController = UINavigationController(rootViewController: progOneViewController)
//navController.isToolbarHidden = false
//progOneViewController.setUpToolbar()
self.present(navController, animated: false, completion: nil)
}
I called this function upon clicking a button in my storyboard-created view controller. The function signature was long in order to specify which viewControllers were tests (created programmatically) :)
first of all you have to make sure that your ProgOneViewController is really included in a UINavigationController. if that is the case the toolbar as well as the barbuttonitems are shown (tried your code in my project).
except of that you have to change all of your barbuttonitem declarations to lazy var so that the references to self within the declarations point to the viewcontroller and target-action works.
feel free to ask if there are any questions :)

How to add title/button to a UINavigationController added inside a UIViewController?

For some reason, I have to add a UINavagationController() inside a UIViewController(), so I did the following in the view controller class:
class someViewController: UIViewController {
private let myNav = UINavigationController()
override func viewDidLoad() {
self.addChildViewController(myNav)
self.view.addSubview(myNav.view)
myNav.view.translatesAutoresizingMaskIntoConstraints = false
myNav.view.topAnchor.constraintEqualToAnchor(self.topLayoutGuide.bottomAnchor).active = true
myNav.view.leftAnchor.constraintEqualToAnchor(view.leftAnchor).active = true
myNav.view.rightAnchor.constraintEqualToAnchor(view.rightAnchor).active = true
myNav.view.heightAnchor.constraintEqualToAnchor(nil, constant: 44).active = true
myNav.didMoveToParentViewController(self)
}
}
The navigation controller is added & showing up correctly. Then I try to add a title (or Done button) to the navigation bar, however the items just don't show up. I tried a few things like this:
self.navigationItem.title = "some title"
myNav.navigationItem.title = "some title"
myNav.navigationItem.rightBarButtonItem = btnDone
What's the right way to do it in this case?
Why don't you use this simple code to add a navigation bar :
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 66)) // Offset by 20 pixels vertically to take the status bar into account
// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "Title"
// Create left and right button for navigation item
let leftButton = UIBarButtonItem(title: "Save", style: UIBarButtonItemStyle.Plain, target: self, action: "btn_clicked:")
let rightButton = UIBarButtonItem(title: "Right", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
// Create two buttons for the navigation item
navigationItem.leftBarButtonItem = leftButton
navigationItem.rightBarButtonItem = rightButton
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)

Add searchBar in navigationBar after viewDidLoad?

In my application, I want to have a search button in the navigation bar and when the button is pressed searchbar should appear in the Navigation Bar. I've seen many posts on the subject, but somehow it does not work for me.
So, I placed a SearchDisplayController right below nav bar. then in the viewDidLoad I assign an action to the search button and remove searchbar from superview. Once button is clicked, I am calling mapSearchDisplayController.displaysSearchBarInNavigationBar = true, but nothing happends. Code extracts below:
override func viewDidLoad() {
super.viewDidLoad()
banner.delegate = self
// Add button to navbar
var filterButton : UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Organize, target: self, action: nil)
self.navigationItem.leftBarButtonItem = filterButton
var aboutButton : UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "aboutAction")
var searchButton : UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search, target: self, action: "searchAction")
var rButtons : [UIBarButtonItem] = [aboutButton, searchButton]
self.navigationItem.rightBarButtonItems = rButtons
// Deal with Search Display Controller
self.mapSearchDisplayController.delegate = self.mapSearchDisplayController;
self.mapSearchDisplayController.searchResultsDataSource = self.mapSearchDisplayController;
self.mapSearchDisplayController.searchResultsDelegate = self.mapSearchDisplayController;
self.mapSearchDisplayController.searchBar.removeFromSuperview();
}
func searchAction(){
println("Search action")
mapSearchDisplayController.displaysSearchBarInNavigationBar = true
}
If I call displaysSearchBarInNavigationBar = true in viewDidLoad then searchBar correctly appears in the Navigation Bar.
How do I make appear in Nav bar on the button press?
Any help is appreaciated.
I created a search bar using
let searchBar = UISearchBar(frame : CGRect(0, 0, self.navigationController?.navigationBar.frame.size.width)! * 0.9, 0)
self.searchBar.placeholder = "Search"
self.searchBar.delegate = self
And then added it to the navigation bar as follows:
self.navigationItem.titleView = self.searchBar
Further styling etc can also be done but I just created and added it to the navbar on button pressed and removed it from superview when it needed to be hidden.

Resources