Mark mobile app menu button when there are new items - ios

There are few ViewControllers in my app and all of them have menu button. When this button is pressed - menu ViewController is opened.
I want to mark menu button with red dot showing that some new content is available and user need to press menu button to see which one of menu item is marked with this dot.
As all my buttons are independent of each other - I thought that I need to solve it this way
Add red dot image on each menu button
Make this dot hidden by default
When each ViewController is opened - i should check - are there any new items available and switch isHidden property of this red dot image to false.
But maybe there is some more elegant way?

use NotificationCenter to notify the ui when new content available
in the menu viewcontroller class:
//put this in viewDidLoad
NotificationCenter.default.addObserver(self.selector : #selector(menuviewcontroller.refresh(_:)),name:NSNotification.Name(rawValue:"showRedBtn"),object : nill)
//create function refresh
func refresh(_ notification : Notification)
{
//make the red dot visible
}
create class that listen if any content added and call the delegate in case of add by this line of code
NotificationCenter.default.post(name : Notification.Name("showRedBtn"),object : nil , userInfo : nil)
hope it's will help you

Related

How can I disable some of the tabs of the moreNavigationController of my UITabBarController?

I'm using XCode 13.0, Swift 5.5 and Storyboard. This is a mobile app for iPhone with iOS 15.
I have altogether 7 tabs, all of them with icons. I have 4 tabs and a "More" tab in the tab bar of my app.
3 additional tabs show up after the user clicks on the "More" tab. I'd like the first 2 of these additional tabs to be disabled. They need to be grey coloured and when the user clicks on any of these 2, I'd like to app to do nothing (not to show any page, not to navigate anywhere). I'm using the original UITabBarController and moreNavigationController defined by Apple, I didn't subclass any of them.
On my storyboard I added 2 UIViewControllers to these 2 disabled tabs, but I set them disabled this way:
This didn't work at all. When the user clicks on the 2 disabled tabs, they show 2 empty ViewControllers. I'd expect them not to show anything.
I also tried accessing the moreNavigationController's tabs from the page my UITabBarController first opens. I tried setting these 2 tabs disabled programmatically. However I didn't manage to access these 2 tabs, I only manage to access the main tabs that show up in the tab bar (I don't need to access these). How can I disable and colour grey the tabs that show up after clicking on the More tab?
let moreControllerItems = tabBarController?.moreNavigationController.toolbarItems
if let tabArray = moreControllerItems {
let tabBarItem1 = tabArray[0]
let tabBarItem2 = tabArray[1]
tabBarItem1.isEnabled = false
tabBarItem2.isEnabled = false
}`
This code doesn't work, the moreControllerItems variable is nil. The tabBarController variable isn't nil, I can access my UITabBarController from here, but it doesn't help me much.
I googled this issue lots of different ways but I couldn't find the solution. Any help would be appreciated. I really need to solve this, please write a comment if you have any idea how to solve this. Thank you!
You can disable tabBar items from TabBarController.
class TabVC: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
}
func disableTabBar(itemNo: Int) {
if let items = tabBar.items, itemNo < items.count {
items[itemNo].isEnabled = false
}
}
And you can access this function from
any child Viewcontroller attached to TabBarController
class MoreVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let tabBarVC = tabBarController as? TabVC {
tabBarVC.disableTabBar(itemNo: 1)
}
}
In short: You cannot disable it. If you want to disable the item in the More Viewcontroller, you have to use a custom Viewcontroller in the fifth Tab
I am explaining here why we cannot do it and what limitations we have.
Apple will show the More tab if items are more than five in the UITabBarController. And more tab holds the UINavigationController which is attached to a kind of UIMoreListController. you can check by
print moreNavigationController.viewControllers[0]
UIMoreListController is not accessible. In your case, the remaining three items are listed in the UIMoreListController. Although we can still access the Viewcontroller's tabBarItem and we can disable it. You can check by clicking the edit option.
But still, users can click on it as long as it is in the UIMoreListController. Because we are not interacting with the tabBar instead we are interacting with the items on the list. Users can't click once you move the item to any of the first four positions using the edit option in the MoreViewcontroller.

Custom Raised Button action not navigates to Tabbarİtem

İ have created a custom raised button in tabbarviewcontroller. Already added a custom action on button for selectedindex = 2.
Problem I am facing first time button clicked is working fine shows second tabbar item and perform action but if I re-clicked button again it won’t show tabbar item view. To check button status I use print command and its printing that button clicked.
Tabbarviewcontroller —> tabbarİtem 2 —> webview
As it stays on webview controller but On re-click of button İ need it come backs(navigates) to tabbarİtem 2.(refresh view as navigatecontroller performs back button). Behind the custom button - tabbarbar item for item2 (link able) is working but not on button.
#objc func buttonAction(sender: UIButton!) {
print("clicked button")
selectedIndex = 2 //// button click to open tabbar item 2
}

UI testing a tab bar controller

I have built a simple tab bar with 3 tabs.
I want to run a UI test to make sure that if the user clicks a tab bar item, the correct view controller shows. How would I go about doing that? Below is the code I would start with, just don't know how to write my assertion.
func testTabBarMyProfileButton() {
let tabBarsQuery = XCUIApplication().tabBars
tabBarsQuery.buttons["My Profile"].tap()
}
func testTabBarGraphsButton() {
let tabBarsQuery = XCUIApplication().tabBars
tabBarsQuery.buttons["Graphs"].tap()
}
func testTabBarAboutButton() {
let tabBarsQuery = XCUIApplication().tabBars
tabBarsQuery.buttons["About"].tap()
}
You can access the tabbar button by its position:
app.tabBars.buttons.element(boundBy: 2).tap()
If you have different controls in each view controller shown on each tab bar, you can make assertions if they exist or not (what is expected).
For example if the first tab bar has UILabel named "First name" you can assert if it exists by writing
Let theLabel = app.staticTexts["myValue"]
XCTAssert(theLabel.exists).to(beTrue)
And on the other screens do the same thing for the different controls.
If anyone finds this looking to UI test the contents of another app, I just found a solution..
The tab bar item is a lazy variable and needs to be touched before you can reference a tab bar button by value. Add this line:
tabBarItem.accessibilityIdentifier = "my-snazzy-identifier"
to the viewDidLoad method and you should be able to do this in your UI tests:
app.tabBars.buttons["Button Title"].tap()
You can test the title of the navigation bar.
XCTAssert(app.navigationBars["Graphs"].exists)
See my GitHub repo for a more detailed UI Testing example.

What is triggered when "Done" is pressed after changing from "Edit" in UINavigationItem bar for UITableViewController?

Short: In Swift/iOS, when does the "Done" (which was previously "Edit") nav bar button trigger when getting out of the UITableViewController "Edit" mode? When the user presses "Done" I'd like to enable a "+" button in my UINavigationItem bar so the user can once-again add rows by migrating to another view controller.
Longer: When a UITableViewController is showing below a UINavigationItem nav bar, there is an "Edit" button which turns into "Done" after it's clicked to enable deletes & move/drags. Works great when this button is enabled via uncommenting code in viewDidLoad() generated as part of the UITableViewController class:
self.navigationItem.leftBarButtonItem = self.editButtonItem
I've got my move/drags & deletes working fine, but I want to appropriately disable my "+" button (addBarButton, used for navigating to another view controller to add a new row) while the user is in the Edit mode. I'd then like to re-enable addBarButton after the user has clicked "Done" (which turns back into "Edit").
It looks like disabling addBarButton during the func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) is correct. If I read Apple's docs right, this is triggered when user presses Edit in the nav bar. What I don't know is what is triggered when the user presses "Done" (the button formerly labeled "Edit"). If I enable my addBarButton "+" button after the func tableView with moveRowAt, this enables addBarButton before the user has pressed "Done".
The Apple doc I'm referencing is at:
https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/TableView_iPhone/ManageReorderRow/ManageReorderRow.html#//apple_ref/doc/uid/TP40007451-CH11-SW1
Apologies if I'm missing something obvious. Thx
The answer is right in the description for the UIViewController editButtonItem documentation:
If one of the custom views of the navigationItem property is set to the returned object, the associated navigation bar displays an Edit button if isEditing is false and a Done button if isEditing is true. The default button action invokes the setEditing(_:animated:) method.
The last sentence is the key. You should override the setEditing(_:animated:) method in your table view controller subclass. Be sure you call the super implementation and then perform whatever custom action you want based on whether the controller is entering or exiting editing mode.
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
if (editing) {
// User tapped the Edit button, do what you need
} else {
// User tapped the Done button, do what you need
}
}

Disable user interaction on tabBar

I am displaying an image right after the app didFinishLaunchingWithOptions, the app consists in a tab bar, and in the first view i have some buttons.
The user can only continue to use the app after he press the button in that first image, the problem is, some users can interact with the tab bar, and the buttons in the first view even with the image above all.
How i can completely disable the user interaction on those buttons and in the tabBar, and enable then only when the button is pressed and the image disapear?
To disable:
UITabBarController.tabBar.userInteractionEnabled = NO;
To enable:
UITabBarController.tabBar.userInteractionEnabled = YES;
if let items = self.tabBarController?.tabBar.items {
for i in 0 ..< items.count {
let itemToDisable = items[i]
itemToDisable.isEnabled = false
}
}
somehow i didn't fully understand what you described,but have you tried disabling your buttons using following code?
button1.Enabled=false;
also you can disable images click function using
image1.Enabled=false;

Resources