Select tab programmatically custom tab bar ios - ios

I have used the following tutorial to implement custom tabs in ios
Its working fine when i tap on any of the item. Now I want to move it Programatically. e.g. I received the notification form Firebase and want to open third tab. But I am getting nil. I have referenced the MainTabbarController instance in appdelegate.
Here is what i have tried
In CustomTabBar
func customTapped(index :Int){
animateTabBarSelection(from: selectedTabBarItemIndex, to: index)
selectedTabBarItemIndex = index
delegate.didSelectViewController(self, atIndex: index)
}
In AppDelegate
mainTabBarController?.customTabBar?.customTapped( index: 2)

From the link that you have provided along with the question, the following are the statements that I found to be responsible for switching the tabs
func barItemTapped(sender : UIButton) {
let index = tabBarButtons.indexOf(sender)!
animateTabBarSelection(from: selectedTabBarItemIndex, to: index)
selectedTabBarItemIndex = index
delegate.didSelectViewController(self, atIndex: index)
}
You can just modify this code as
func barItemTapped(sender : UIButton) {
let index = tabBarButtons.indexOf(sender)!
tabSelectedAt(index)
}
func tabSelectedAt(_ index:Int) {
if selectedTabBarItemIndex != nil {
animateTabBarSelection(from: selectedTabBarItemIndex, to: index)
}
selectedTabBarItemIndex = index
delegate.didSelectViewController(self, atIndex: index)
}
So call the function func tabSelectedAt(_ index:Int) with the index of the tab which you wants to switch.

Related

How to Focus Accessibility On A Particular Segment in A UISegmentedControl

OK. This answer helps a lot. I can select an accessibility item when a screen is shown. I simply add
UIAccessibility.post(notification: .layoutChanged, argument: <a reference to the UI item to receive focus>)
to the end of my viewWillAppear() method, and the item receives focus.
However, in one of my screens, the item I want to receive focus is a UISegmentedControl, and, when focused, it always selects the first item, no matter which one is selected. Since I followed the excellent suggestion here, I have an accessibility label for each item in the control, and I'd like my focus to begin on whichever segment is selected.
Is there a way to do this? As a rule, I try to avoid "hacky" solutions (like the one I just referenced), but I'm willing to consider anything.
Thanks!
UPDATE: Just to add insult to injury, I am also having an issue with the item I want selected being selected, then a second later, the screen jumps the selection to the first item. That's probably a topic for a second question.
I created a blank project as follows to reproduce the problem:
The solution is taking the selectedIndex to display the selected segment and providing the appropriate segment object for the VoiceOver notification: easy, isn't it?
I naively thought that getting the subview in the segmented control subviews array with the selectedIndex would do the job but that's definitely not possible because the subviews can move inside this array as the following snapshot highlights (red framed first element for instance):
The only way to identify a unique segment is its frame, so I pick up the segmented control index and the frame of the selected segment to pass them to the previous view controller.
That will allow to display (index) and read out (frame that identifies the object for the notification) the appropriate selected segment when this screen will appear after the transition.
Hereafter the code snippets for the view controller that contains the 'Next Screen' button:
class SOFSegmentedControl: UIViewController, UpdateSegmentedIndexDelegate {
var segmentIndex = 0
var segmentFrame = CGRect.zero
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let segueName = segue.identifier {
if (segueName == "SegmentSegue") {
if let destVC = segue.destination as? SOFSegmentedControlBis {
destVC.delegate = self
destVC.segmentIndex = segmentIndex
destVC.segmentFrame = segmentFrame
}
}
}
}
#IBAction func buttonAction(_ sender: UIButton) { self.performSegue(withIdentifier: "SegmentSegue", sender: sender) }
func updateSegmentIndex(_ index: Int, withFrame frame: CGRect) {
segmentIndex = index
segmentFrame = frame
}
}
... and for the view controller that displays the segmented control:
protocol UpdateSegmentedIndexDelegate: class {
func updateSegmentIndex(_ index: Int, withFrame frame: CGRect)
}
class SOFSegmentedControlBis: UIViewController {
#IBOutlet weak var mySegmentedControl: UISegmentedControl!
var delegate: UpdateSegmentedIndexDelegate?
var segmentFrame = CGRect.zero
var segmentIndex = 0
var segmentFrames = [Int:CGRect]()
override func viewDidLoad() {
super.viewDidLoad()
mySegmentedControl.addTarget(self,
action: #selector(segmentedControlValueChanged(_:)),
for: .valueChanged)
mySegmentedControl.selectedSegmentIndex = segmentIndex
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print(mySegmentedControl.subviews)
let sortedFrames = mySegmentedControl.subviews.sorted(by: { $0.frame.origin.x < $1.frame.origin.x})
for (index, segment) in sortedFrames.enumerated() { segmentFrames[index] = segment.frame }
if (self.segmentFrame == CGRect.zero) {
UIAccessibility.post(notification: .screenChanged,
argument: mySegmentedControl)
} else {
mySegmentedControl.subviews.forEach({
if ($0.frame == self.segmentFrame) {
UIAccessibility.post(notification: .screenChanged,
argument: $0)
}
})
}
}
#objc func segmentedControlValueChanged(_ notif: NSNotification) {
delegate?.updateSegmentIndex(mySegmentedControl.selectedSegmentIndex,
withFrame: segmentFrames[mySegmentedControl.selectedSegmentIndex]!) }
}
The final result is as follows:
Double tap to go to the next screen.
Select the next element to focus the second segment.
Double tap to select the focused element.
Get back to the previous screen thanks to the Z gesture natively known by iOS with the navigation controller. The delegate passes the index and the frame of the selected segment.
Double tap to go to the next screen.
The segment that was formerly selected is read out by VoiceOver and still selected.
You can now Focus Accessibility On A Particular Segment in A UISegmentedControl following this rationale.
I try to avoid "hacky" solutions (like the one I just referenced), but I'm willing to consider anything.
Unfortunately, this solution is a hacky one... sorry. However, it works and I couldn't find another one anywhere else: see it as a personal fix unless you get a cleaner one to share? ;o)
UPDATE... That's probably a topic for a second question.
I can't reproduce the behavior of your update: if you create a dedicated topic for this problem, please add the most detailed code and context so as to provide the most accurate solution.
i think this works~!
class VC {
let segment = UISegmentedControl()
func fucusSegment(index: Int) {
let item = segment.accessibilityElement(at: index )
UIAccessibility.post(notification: .layoutChanged, argument: item)
}
}

Previous/Next Functionality like iOS mail app?

I'm really close to figuring this out. So in the iOS mail app when you click on the two arrow keys it takes you to the previous/next mail. Its on the top right
I've managed to pass the indexPath value to my second viewcontroller and print in in the console. I can also increase and decrease from it.
if segue.identifier == "DetailVC" {
let detailVC = segue.destination as! DetailVC
let indexPath = self.collectionViewIBO.indexPathsForSelectedItems?.last!
detailVC.index = indexPath
}
EDIT
This is where I'm pulling the data from. It reads the values from my model. I cannot assign an indexPath to it however. I can only do that from the previous view controller
var monster: Monsters!
I've attempted to implement the "previous" functionality using this code. My view styling are in the displayDataForIndexPath() function and the function is called from my view will appear
#IBAction func monsPreviousIBO(_ sender: Any) {
self.index = IndexPath(row: self.index.row - 1, section: self.index.section)
displayDataForIndexPath()
}
But all it does is decrease the IndexPath. For some reason the data doesn't actually reload with my function. I'm missing some important puzzle piece here to achieving the same functionality.
EDIT The code in my displayDataForIndex is as follows
func displayDataForIndexPath() {
if index.row == 0 {
self.monsPreviousIBO.removeFromSuperview()
}
var monsterName = (String(format: "%03d", monster.speciesId!))
self.navigationItem.title = monster.name!
let gif = UIImage(gifName: monsterName)
self.gifIBO.setGifImage(gif, manager: gifManager)
gifIBO.contentMode = .center
guard monster.legendary! != true else {
// Value requirements not met, do something
monsterStatusLegend()
return
}
guard monster.subLegend! != true else {
// Value requirements not met, do something
monsterStatusSub()
return
}
guard monster.isMega! != true else {
// Value requirements not met, do something
monsterStatusMega()
return
}
}
you display all data depending on monster but you never change the monster depending which indexPath you used.
add some code to populate the monster from indexPath
monster = getMonster(index.row)
or in your case
#IBAction func monsPreviousIBO(_ sender: Any) {
self.index = IndexPath(row: self.index.row - 1, section: self.index.section)
monster = mons[self.index.row]
displayDataForIndexPath()
}
or better in the displayDataForIndexPath() add this line:
func displayDataForIndexPath() {
if index.row == 0 {
self.monsPreviousIBO.removeFromSuperview()
}
monster = mons[self.index.row]
//....
NOTE some suggestions:
i would change the line for the button then it is enabled if the indexpath gets >0:
self.monsPreviousIBO.isEnabled = (index.row != 0)
just save the row as monsterIndex = indexPath.row and then deal only with the index and not indexPath.
you don't need to save the current monster as monster if you use the monster only in displayDataForIndexPath - then you can get the current monster just there and have it a local variable in this function:
var monster = mons[self.index.row]

UISegmentedControl reload certain page

I'm using a third party library for my UISegmentedControl. The pages are initialised as following:
func carbonTabSwipeNavigation(carbonTabSwipeNavigation: CarbonTabSwipeNavigation, viewControllerAtIndex index: UInt) -> UIViewController {
switch index {
case 0:
return self.storyboard!.instantiateViewControllerWithIdentifier("FolderOverviewController") as! FolderOverviewController
case 1:
return self.storyboard!.instantiateViewControllerWithIdentifier("TopFoldersTab") as! TopFoldersTab
case 2:
return self.storyboard!.instantiateViewControllerWithIdentifier("CategoriesFolderTab") as! CategoriesFolderTab
default:
return self.storyboard!.instantiateViewControllerWithIdentifier("CategoriesFolderTab") as! CategoriesFolderTab
}
}
When I press the third segment, the user can go further down to see more details (via subviews on the same page). I would like the page to reload, every time I select the third segment again. (go back to the original CategoriesFolderTab page) . Currently I'm doing this with a ViewDidLoad(), but this is slowing down the app when you do it multiple times.
Is there a more correct way to do this? Thanks in advance
I think that calling viewDidLoad() is not the right approach for achieving this, instead, implement a new function that should contains code responsible for loading data in the UI components, for example:
override func viewDidLoad() {
super.viewDidLoad()
reloadUI()
}
func reloadUI() {
// filling UI components with desired data, such as:
// myLabel.text = "Hello World"
}
And somewhere in your code (where you want to reload), instead of calling viewDidLoad(), you should call reloadUI() method.
Hope this helped.
try this
Let folderOverVC = self.storyboard!.instantiateViewControllerWithIdentifier("FolderOverviewController") as! FolderOverviewController
Let topFoldersTab = self.storyboard!.instantiateViewControllerWithIdentifier("TopFoldersTab") as! TopFoldersTab
Let categoriesFolderTab = self.storyboard!.instantiateViewControllerWithIdentifier("CategoriesFolderTab") as! CategoriesFolderTab
func carbonTabSwipeNavigation(carbonTabSwipeNavigation: CarbonTabSwipeNavigation, viewControllerAtIndex index: UInt) -> UIViewController {
switch index {
case 0:
return folderOverVC
case 1:
return topFoldersTab
case 2:
return categoriesFolderTab
default:
return categoriesFolderTab
}
}
this initializes the view controllers once, so their respective viewDidLoad methods get called once initialized, not every time you tap a tab item

Programmatically turns the page, indicators does not change

I try the following approach found here
extension UIPageViewController {
func goToNextPage(){
guard let currentViewController = self.viewControllers?.first else { return }
guard let nextViewController = dataSource?.pageViewController( self, viewControllerAfter: currentViewController ) else { return }
setViewControllers([nextViewController], direction: .forward, animated: true, completion: nil)
}
}
It works, but there is one issue:
When the page is turned programatically, the indicator does not move. It seems that they move only when user turns. page with swipe
that's how indicators should look like after programmatic turn is performed:
instead they remain unchanged
Which leads to issue that hierarchy shown by indicators is rather [2,0,1] instead of [0,1,2]
This is how I implement indicators:
func presentationCount(for PageViewController:UIPageViewController) -> Int {
return 3
}
func presentationIndex(for PageViewController:UIPageViewController) -> Int {
return 0
}
How to make dots indicators move when the page is turned programatically?
Unfortunately you can't update UIPageControl embedded in UIPageViewController. However, you can have your own UIPageControl in UIPageViewController in order to get full control. Then you can update UIPageControl property programmatically upon updating your Page. Have a look at this article.
There is a workaround get the subviews of UIPageViewController, set the value to currentPage.
for subView in self.view.subviews{
if let pageControl = subView as? UIPageControl,
pageControl.numberOfPages > currentIndex{
pageControl.currentPage = currentIndex
}
}
You can, all you need to do is maintain the page index in a variable, let's call it currentPageIndex and use the following method:
// Part of UIPageViewControllerDelegate
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return currentPageIndex;
}
// In your Button action, set this variable
#IBAction func nextPage(_ sender: Any) {
var index = (pageViewController?.viewControllers?.last as! SinglePageViewController).pageIndex
currentPageIndex = index
}
That's it!! Your page indicator should work now.
I was stuck in a same situation as yours.

Remove TabBar item in Swift

I currently try to find a way to remove while run the app a TabBar Item, i found a way to enable or disable it but not to complete remove it.
For disable it i do:
In ViewDidLoad
if let tabBarItem = self.tabBarController?.tabBar.items?[3] as? UITabBarItem {
tabBarItem.enabled = false
}
This works well but still the user can see the TabBar item and i ll simply complete remove it, is there a way?
I want to trigger the TabBarItem via Parse, if i set the Parse Data to true it should show other way it should not.
You want to set the viewControllers property of your tabBarController with an array where you excluded the particular viewController that you don't want to have anymore.
if let tabBarController = self.tabBarController {
let indexToRemove = 3
if indexToRemove < tabBarController.viewControllers?.count {
var viewControllers = tabBarController.viewControllers
viewControllers?.remove(at: indexToRemove)
tabBarController.viewControllers = viewControllers
}
}
For those who just want to disable one item. Use this code from #Daniele's solution. and place it in your UITabBarController class
viewDidLoad() {
let index = 0 //0 to 5
viewControllers?.remove(at: index)
}
Swift 5: For removing only one index in Tab Bar Controller(you can use this method in viewDidLoad and viewDidAppear both of them)
override func viewDidAppear(_ animated: Bool) {
}
override func viewDidLoad() {
super.viewDidLoad()
}
tabBarController.viewControllers?.remove(at:0) // for 0 index
tabBarController.viewControllers?.remove(at:1) // for 1 index
tabBarController.viewControllers?.remove(at:2) // for 2 index
if you have 4 index in Tab Bar and you want to remove the last 2 index
tabBarController.viewControllers?.remove(at:2)
tabBarController.viewControllers?.remove(at:2)
first line will remove the index 3rd one and you will remaining 3 from 4 and again when you remove the 2nd index it will remove again 3rd index and then you will have remain 2 index in last.
Another Way
//MARK: - Function Call
removeTab(at: 4)
//MARK: - Method
func removeTab(at index: Int) {
if self.viewControllers?.count ?? 0 >= index {
self.viewControllers?.remove(at: index)
}
}
Swift 4.1
For removing More items Use Array
let index = [2,0]
index.forEach{viewControllers?.remove(at: $0)}
the point in the array is You Should Use Descending Order of indexes to remove to get the desired Result.
better way is to use only text instead of image. choose 'space' as the text then disable that. then the user will not be able to see it. i haven't tested it but I sure it will work.

Resources