Disable user interaction on tabBar - ios

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;

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.

Change toolbars according to the condition

I am working with xamarin android. I have a layout where initially a toolbar with edit will be visible, when user selects edit the current tool bar should be replaced with another toolbar. I have written code as following.
public override bool OnCreateOptionsMenu(IMenu menu)
{
_localMenu = menu;
var toolbar = _isMultiSelect ? Resource.Menu.MultipleFormsDeleteToolBarMenu : Resource.Menu.FormToolbarMenu;
MenuInflater.Inflate(toolbar, menu);
return base.OnCreateOptionsMenu(menu);
}
And when Edit option in first toolbar is selected, Iam calling OnCreateOptionsMenu
else if (item.ItemId == Resource.Id.edit)
{
_isMultiSelect = true;
OnCreateOptionsMenu(_localMenu);
}
Here the problem is,instead of showing second toolbar... the Edit option in first toolbar is replaced by three dots and while tapping on those three dots Edit and Delete options are dislaying.
How can I change those toolbars acconding to the requirements?

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.

iOS Swift Like Button in UITableView 's Cell

I have a tableView in my app. and in my app users can like a post (best example is instagram). when I like a post my button state change to active and button's color change. but when I scroll the tableView button state back to normal. I know it happen because cell reload again, but what is the best solution for this problem? am I have to overtime that I press like button app send like request and tableview reloadData? (this is not good for server)
EDIT: I have a flag for button, but when I press like button I have to send request and fetch dataArray again. but how can change button state and send like request but not fetch dataArray. it will happen when user manually refresh data.
thanks.
You can set using if and else condition. see below example
if model.data.isFav == true
{
btnFav.selected = true
}
else
{
btnFav.selected = false
}
If you have any flag for button with Posts in your dataArray from which you are loading cells data then you can use that flag to set the color of button. It will take care at the time of scroll.
Set button type to custom in the navigation inspector, then set the background image for default and selected. then use the code below:
// MARK: Set fav button Action
func setFavButtonAction(_ sender : UIButton) {
print(sender.tag)
if sender.isSelected == true {
sender.isSelected = false
}else{
sender.isSelected = true
}
}

I can't figure out how to set the correct button to be unhidden using swift

I'm trying to create a quiz app for a project, I'm having a problem when I try to set another button to be revealed when you select the correct answer. ex.
1+2=?
2
3
5
9
When they select 3, I want the continue button to appear to go to the next page.
#IBAction func Seaturtle(sender: UIButton) {
if sender.tag == 2 {
7.hidden = false
}
}
Go into your Storyboard and find the Continue button you wish to hide/unhide. Use Xcode's split screen view to have both your Storyboard and the associated code file open. Select the button, then Ctrl+Drag from the button into the code, creating an IBOutlet instead of an IBAction. Say you give it the name continueButton. Then, when you want to hide/unhide just call
continueButton.hidden = true
or
continueButton.hidden = false

Resources