ViewController turns blank when jumping between tabs IOS - ios

I have uiViewControllers embedded in uiNavigationControllers that are driven by a TabBarController. Majority of the time when I move between tabs everythings fine, but at random times when I jump to the first tab the screen goes blank (black). The tabBar is still visible and I'm able to jump to the other two tabs, but the first one remains blank. This has only happened on my iPhone and not in simulator. My appdelegate looks like below code, any ideas?
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
tabBarItem1.title = #"Tab 1";
tabBarItem2.title = #"Tab 2";
tabBarItem3.title = #"Tab 3";
[tabBarItem1 setImage:[[UIImage imageNamed:#"tab1.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem1 setSelectedImage:[[UIImage imageNamed:#"tab1a.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem2 setImage:[[UIImage imageNamed:#"tab2a.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem2 setSelectedImage:[[UIImage imageNamed:#"tab2a.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem3 setImage:[[UIImage imageNamed:#"tab3.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem3 setSelectedImage:[[UIImage imageNamed:#"tab3a.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[[UITabBar appearance] setTintColor:[UIColor colorWithRed:207/255.0 green:83/255.0 blue:0/255.0 alpha:1.0]];
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:207/255.0 green:83/255.0 blue:0/255.0 alpha:1.0]];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[UIColor whiteColor]];
[UINavigationBar appearance].titleTextAttributes = #{NSForegroundColorAttributeName: [UIColor whiteColor]};
UINavigationController *firstVCnav = [[tabBarController viewControllers] objectAtIndex:0];
UINavigationController *secondVCnav = [[tabBarController viewControllers] objectAtIndex:1];
UINavigationController *thirdVCnav = [[tabBarController viewControllers] objectAtIndex:2];
MyFirstVC *oneVC = [[firstVCnav viewControllers] objectAtIndex:0];
oneVC.managedObjectContext = self.managedObjectContext;
MySecondVC *twoVC = [[secondVCnav viewControllers] objectAtIndex:0];
twoVC.managedObjectContext = self.managedObjectContext;
MyThirdVC *thirdVC = [[thirdVCnav viewControllers] objectAtIndex:0];
thirdVC.managedObjectContext = self.managedObjectContext;
return YES;

From another forum I received a tip to check if I'm using a nib where the view controller setup is preconfigured.
I did have a nib reference in a viewcontroller since I removed it the issue hasn't occured, but since random/rare issue I'll monitor and update if issue comes back.

Related

Tabbar item images not loaded first time

i am using tabbarcontroller in my project. Here is my storyboard
In all view controllers tabbar item, I set images like below
I make a class of tabbarcontroller and do coding below
UITabBar *tabBar = self.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
UITabBarItem *tabBarItem5 = [tabBar.items objectAtIndex:4];
// [tabBarItem1 setImage:[[UIImage imageNamed:#"ic_home_tab_home_sel"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
// [tabBarItem1 setSelectedImage:[[UIImage imageNamed:#"ic_home_tab_home_sel"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"ic_home_tab_home_sel"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_home_tab_home"]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:#"ic_home_tab_profile_sel"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_home_tab_profile"]];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:#"ic_home_create_post_sel"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_home_create_post"]];
[tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:#"ic_home_tab_chat_sel"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_home_tab_chat"]];
[tabBarItem5 setFinishedSelectedImage:[UIImage imageNamed:#"ic_home_tab_setting_sel"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_home_tab_setting"]];
But When I run it, First time all images are mixed, But when I clicked them one by one like in Video then all images sets.I dont know why this happend? Please help meThank you
I also faced same problem then I used below code in appdelegate
UINavigationController *navigationVC1 = [[UINavigationController alloc]initWithRootViewController:viewController1];
navigationVC1.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"My Trips"
image:[UIImage imageNamed:#"cab.png"]
tag:1];
UINavigationController *navigationVC2 = [[UINavigationController alloc]initWithRootViewController:viewController2];
navigationVC2.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"My Children"
image:[UIImage imageNamed:#"kids-couple.png"]
tag:2];
try like this.

Setting TabBar Item title and Image on Selection in XCode 6.4

I am using xcode 6.4 and using storyboard to set up Tab bar controller Item and its Images.
Issue 1- In storyboard on tab bar item selection, there are already option for Bar Item and Image selection. i have assigned Images on both field but Images are not changing at time of selection.Only default(black) image exists.
I have referred to this Link . Then also default (black) exists
Issue 2- In storyboard i have assigned all images on Bar Item and Image Selection and used given code then title is not visible in tab bar item. only image is working fine on selection.
Code-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Assign tab bar item with titles
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
UITabBarItem *tabBarItem5 = [tabBar.items objectAtIndex:4];
tabBarItem1.title = #"Home";
tabBarItem2.title = #"Search";
tabBarItem3.title = #"Cart";
tabBarItem4.title = #"Account";
tabBarItem5.title = #"More";
[tabBarItem1 setSelectedImage:[[UIImage imageNamed:#"HomeSelected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem2 setSelectedImage:[[UIImage imageNamed:#"SearchOpened.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem3 setSelectedImage:[[UIImage imageNamed:#"CartOpened.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem4 setSelectedImage:[[UIImage imageNamed:#"ProfileOpened.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem5 setSelectedImage:[[UIImage imageNamed:#"MoreTabOpened.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
// Change the tab bar background
UIImage* tabBarBackground = [UIImage imageNamed:#"tabbar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"tabbar_selected.png"]];
// Change the title color of tab bar items
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
UIColor *titleHighlightedColor = [UIColor colorWithRed:255.0/255.0 green:109.0/255.0 blue:0.0/255.0 alpha:1.0];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
titleHighlightedColor, NSForegroundColorAttributeName,
nil] forState:UIControlStateHighlighted];
return YES;
}
I've seen gone through iOS Docs, but could not find any useful update on these issues. please update me on it.
You should create your custom tabbar
Useful link : http://swiftiostutorials.com/tutorial-custom-tabbar-storyboard/

How to set image for tabbar item in ios 6 and ios 7

How to set image for tabbar item in ios 6 and ios 7. My tabbar only run in ios 7 because imageWithRenderingMode method is not run in ios 6. How to set tab bar item run in ios 6 and 7. Thank in advance. This is my code.
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
tabBarItem1.title = #"Phim mới";
tabBarItem2.title = #"Xem phim";
tabBarItem3.title = #"GS Xoay";
tabBarItem4.title = #"Về chúng tôi";
[tabBarItem1 setImage:[[UIImage imageNamed:#"ic1.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem2 setImage:[[UIImage imageNamed:#"ic2.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem3 setImage:[[UIImage imageNamed:#"ic3.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem4 setImage:[[UIImage imageNamed:#"ic4.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
return YES;
You need to check to see if the method exists at runtime:
UIImage *image1 = [UIImage imageNamed:#"ic1.jpg"];
if ([image1 respondsToSelector:#selector(imageWithRenderingMode:)]) {
[tabBarItem1 setImage:[image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tabBarItem2 setImage:[[UIImage imageNamed:#"ic2.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem3 setImage:[[UIImage imageNamed:#"ic3.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem4 setImage:[[UIImage imageNamed:#"ic4.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
} else {
[tabBarItem1 setImage:image1];
[tabBarItem2 setImage:[UIImage imageNamed:#"ic2.jpg"]];
[tabBarItem3 setImage:[UIImage imageNamed:#"ic3.jpg"]];
[tabBarItem4 setImage:[UIImage imageNamed:#"ic4.jpg"]];
}
I suggest you read the "SDK Compatibility Guide" in the documentation.

placing a Tabbar controller on top of viewcontroller

How do i place the TABBAR control on top of view control.by default it is at bottom how do i put it up..?
Used following code to do:
UIImage* tabBarBackground = [UIImage imageNamed:#"footer-bg.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance]setSelectionIndicatorImage:[UIImage imageNamed:#"footer-hover-bg.png"]];
if(IS_IOS_7)
{
[[appDelegate.tabBarController tabBar] setSelectionIndicatorImage:[UIImage imageNamed:#"footer-hover-bg.png"]];
[[UITabBar appearance] setTintColor:[UIColor blackColor]];
}
appDelegate.tabBarController.tabBar.frame = CGRectMake(0, 20, 320, 50);
appDelegate.tabBarController.delegate=self;
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, -20, 320, 600)];
viewControllers = [[NSMutableArray alloc] init];
MainViewController *view1 = [[MainViewController alloc] init];
UINavigationController *nav1=[[UINavigationController alloc]initWithRootViewController:view1];
[viewControllers addObject:nav1];
ViewController1 *view2 = [[ViewController1 alloc] init];
UINavigationController *nav2=[[UINavigationController alloc]initWithRootViewController:view2];
[viewControllers addObject:nav2];
[appDelegate.tabBarController setViewControllers:viewControllers];
appDelegate.tabBarController.tabBarController.view.frame=CGRectMake(0, 0, 320, 480);
[view addSubview:appDelegate.tabBarController.view];
[self.view addSubview:view];
UITabBarItem *tabBarItem1 = [appDelegate.tabBarController.tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [appDelegate.tabBarController.tabBar.items objectAtIndex:1];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:[arraySelectedImages objectAtIndex:0]] withFinishedUnselectedImage:[UIImage imageNamed:[arrayUnselectedImages objectAtIndex:0]]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:[arraySelectedImages objectAtIndex:1]] withFinishedUnselectedImage:[UIImage imageNamed:[arrayUnselectedImages objectAtIndex:1]]];
How do i do the same using storyboards?
First add a UIViewController then add the UITabBar and add contraint if you use autolayout.
Don't forgot to connect delegate.
Hope that will help.

Creating custom tabs in IOS?

I am trying to create custom-tabs in my IOS app. I have followed this simple guide http://kurrytran.blogspot.se/2011/10/ios-5-tutorial-creating-custom-tab-bar.html Each tab should have 2 background pictures, one when being selected and one being unselected. My porblem is, in my real project I also have navigation controllers between my Tab bar controller and view controllers which seems to mess everything up. Simply nothing happens, all my tabs are black without any content in them. What shall I do to make it work ?
This is put in firstviewCotroller.m
- (void)viewDidLoad
{
UIImage *selectedImage0 = [UIImage imageNamed:#"HomeDB.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:#"HomeLB.png"];
UIImage *selectedImage1 = [UIImage imageNamed:#"ScheduleDB.png"];
UIImage *unselectedImage1 = [UIImage imageNamed:#"ScheduleLB.png"];
UIImage *selectedImage2 = [UIImage imageNamed:#"BuildingsDB.png"];
UIImage *unselectedImage2 = [UIImage imageNamed:#"BuildingsLB.png"];
UIImage *selectedImage3 = [UIImage imageNamed:#"InformationDB.png"];
UIImage *unselectedImage3 = [UIImage imageNamed:#"InformationLB.png"];
UIImage *selectedImage4 = [UIImage imageNamed:#"MoreDB.png"];
UIImage *unselectedImage4 = [UIImage imageNamed:#"MoreLB.png"];
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
UITabBarItem *item4 = [tabBar.items objectAtIndex:4];
[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
[item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
[item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:unselectedImage2];
[item3 setFinishedSelectedImage:selectedImage3 withFinishedUnselectedImage:unselectedImage3];
[item4 setFinishedSelectedImage:selectedImage4 withFinishedUnselectedImage:unselectedImage4];
[super viewDidLoad];
}
Would it not be better to put the UINavigationControllers in each tab separately within your MainWindow.xib then in your App delegate use your above code to change the tab bar items in your App Delegate.
I'm doing the following in the "didFinishLaunchingWithOptions" method in my App Delegate.
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
[item0 setFinishedSelectedImage:[UIImage imageNamed:#"tab-1-selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab-1.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:#"tab-2.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab-2.png"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:#"tab-3-selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab-3.png"]];
Thanx for your answer AndyDev! This is my code below....
My AppleDelegate.h file:
#interface AppDelegate : UIResponder <UIApplicationDelegate>{
UITabBarController *tabBarController;
}
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
My AppleDelegate.m file:
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize tabBarController;
#synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
UITabBarItem *item4 = [tabBar.items objectAtIndex:4];
[item0 setFinishedSelectedImage:[UIImage imageNamed:#"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"BuildingsLB.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:#"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"BuildingsLB.png"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:#"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"BuildingsLB.png"]];
[item3 setFinishedSelectedImage:[UIImage imageNamed:#"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"BuildingsLB.png"]];
[item4 setFinishedSelectedImage:[UIImage imageNamed:#"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"BuildingsLB.png"]];
return YES;
}
I am probably doing something wrong. I never tried to customized anything before in IOS and this is my first project I am developing in IOS. Do I need any code in each ViewController.m file aswell? Nothing happens atleast with this code....

Resources