UINavigation with UITabbar not working - ios

I'm trying to change views within an app I am editing the code for. But the navigation bar doesn't seem to update in specific instances when presenting a new viewcontroller. Right now the behavior looks like this:
I navigate to a tableviewcontroller by clicking on a tab in the tabbar,
then I navigate to a viewcontroller from this tableviewcontroller using:
settingsController = [SettingsController create];
[self.navigationController pushViewController: settingsController animated: YES];
which I believe calls this code:
+(id)create
{
SettingsController*settings = [[SettingsController alloc] init];
NSBundle*bundle = [NSBundle bundleForClass: [SettingsController class]];
[bundle loadNibNamed: #"SettingsController" owner: settings options: NULL];
settings.view.frame = CGRectMake(0, 0, 320, 411);
settings.title = #"Settings";
return settings;
}
Then from there I navigate to another view controller using:
SearchViewController*searchView;
searchView = [[SearchViewController alloc] initWithNibName:#"SearchView" bundle: [NSBundle mainBundle]];
[self presentViewController:searchView animated:YES completion:nil];
and this is where the behavior starts getting buggy, the navigation bar doesnt update to the change in the view controller. I didn't write this code but it has been giving me a headache trying to clean it up.

If you are using a navigationController then you shouldn't call
[self presentViewController:searchView animated:YES completion:nil];.
You should use
[self.navigationController pushViewController:searchView animated:YES];

Also, it would be better to use the standard in-built function to initialize a new view controller.
SettingsController*settings = [[SettingsController alloc] initWithNibName:#"SettingsController" bundle:nil];
[self.navigationController pushViewController:settings animated:YES];
and then use the default method in the view controller.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Settings";
//other view changes.
}
return self;
}

See this example for the way of initializing tabbarcontroller with navigation controller
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *viewController1, *viewController2,*viewController3;
viewController1 = [[ViewController alloc] init];
viewController2 = [[FormStatusViewController alloc] initWithNibName:#"FormStatusViewController" bundle:nil];
viewController3 = [[DocumentsViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *nav1 =[[UINavigationController alloc] initWithRootViewController:viewController2];
UINavigationController *nav2 =[[UINavigationController alloc] initWithRootViewController:viewController3];
nav.navigationBarHidden=YES;
nav1.navigationBarHidden=YES;
nav2.navigationBarHidden=YES;
NSArray *viewsArray = [[NSArray alloc] initWithObjects:nav,nav1,nav2, nil];
self.formTabBar= [[UITabBarController alloc] init];
[self.formTabBar setViewControllers:viewsArray];
UITabBar *tabBar = self.formTabBar.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
tabBarItem1.title = #"FORM";
tabBarItem2.title = #"STATUS";
tabBarItem3.title = #"DOCUMENTS";
UIImage *tabBackground = [[UIImage imageNamed:#"tab_bg.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UITabBar appearance] setBackgroundImage:tabBackground];
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"form.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"form_h.png"]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:#"status.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"status_h.png"]];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:#"documents.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"documents_h.png"]];
} else {
tabBarItem1.selectedImage = [[UIImage imageNamed:#"form_h.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.image = [[UIImage imageNamed:#"form.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.selectedImage = [[UIImage imageNamed:#"status_h.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.image = [[UIImage imageNamed:#"status.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.selectedImage = [[UIImage imageNamed:#"documents_h.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.image = [[UIImage imageNamed:#"documents.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
}
[[UITabBar appearance] setSelectionIndicatorImage:
[UIImage imageNamed:#"tab_select_indicator.png"]];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
nil] forState:UIControlStateHighlighted];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = self.formTabBar;
[self.window makeKeyAndVisible];
return YES;
}
Now in your view controller suppose the firstviewcontroller in first tab you want to navigate to another view controller in that tab then use below code
SearchViewcontroller *searchView =[[SearchViewcontroller alloc]init];
[self.navigationController pushViewController:searchView animated:YES];

Related

when i am adding in UITabBar background image then height increase automatically

I am adding UITabBar controller, But problem is that when i am set background image its Black space from bottom and when i remove the background image its works fine .
I am adding in app delegate
-(void)gotoLoginStoryBoard
{
UIStoryboard *storyboard ;
storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *login = [storyboard instantiateInitialViewController];
recentsViewController = [[RecentsViewController alloc]
initWithStyle:UITableViewStylePlain];
recentsViewController.phoneCallDelegate = self;
UINavigationController *recentsViewCtrl = [[[UINavigationController alloc]
initWithRootViewController:
recentsViewController]
autorelease];
recentsViewCtrl.navigationBar.barStyle = UIBarStyleBlackOpaque;
[recentsViewController release];
phoneViewController = [[[PhoneViewController alloc]
initWithNibName:nil bundle:nil] autorelease];
phoneViewController.phoneCallDelegate = self;
ContactViewController *contactsViewCtrl = [[[ContactViewController alloc]
init] autorelease];
contactsViewCtrl.phoneCallDelegate = self;
VoicemailController *voicemailController = [[VoicemailController alloc]
initWithStyle:UITableViewStyleGrouped];
voicemailController.phoneCallDelegate = self;
UINavigationController *voicemailNavCtrl = [[[UINavigationController alloc]
initWithRootViewController:
voicemailController]
autorelease];
voicemailNavCtrl.navigationBar.barStyle = UIBarStyleBlackOpaque;
[voicemailController release];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:
/*favoritesViewCtrl,*/ login,
phoneViewController, contactsViewCtrl, nil];
tabBarController.selectedIndex = 0;
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:#"tabbarimage.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:#"fill3Copy.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"pathCopy4.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]setTitle:#"Contact"];
[[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:#"fill3Copy3.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"pathCopy5.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]setTitle:#"Group"];
[[UITabBar appearance] setBackgroundColor:[UIColor blackColor ]];
[self.window setRootViewController:tabBarController];
}
First of All check your imagesize.
from apple documentation the maximum size of a picture on the TabBar
are 30x30 (60x60 for the retina display).
I think it's not possible to take the entire height of the TabBar without strech the image like UIimageview . so, according to me the best solution is to center the image in the TabBar using imageInset.
tabBarItem.imageInsets = UIEdgeInsetsMake(0, -10, -6, -10);
OR
tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
Set Imageinsect till image fit to tabbar controller.
For More Detail Check Apple Document below link.
Icon and Image Sizes
I hope it will helpful for you.
i was missing this line in appellate.
MelpHomeViewController *melpHome = (MelpHomeViewController *)[storyboard instantiateViewControllerWithIdentifier:#"homeController"];

addSubview to rootViewController

I updated my version of XCode and now I get the following error:
*application windows are expected to have a root view controller*
I read that the error is because I have no RootViewController assigned .
The problem is that I have multiple subviews and any is a main view.
Example:
// Add the view controller's view to the window and display.
[window addSubview:infoTabViewController.view];
[window addSubview:shareTabViewController.view];
[window addSubview:tabViewController.view];
[window addSubview:mainTabViewController.view];
How you could assign these subviews to a RootViewController?
Thanks.
Firstly you Write in appdelegate.m to set root view controller
- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.logviewController = [[LoginViewController alloc]init];
self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.logviewController];
[self.window setRootViewController: self.navigationController];
[self.window makeKeyAndVisible];
return YES;
}
after that add tabbar controller and write code in view did load
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.leftBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;
self.view.backgroundColor = [UIColor whiteColor];
self.title = #"Welcome To MyConnect";
_friendviewcontroller = [FriendsViewController new];
_homeviewcontroller = [HomePageViewController new];
_notifviewcontroller = [NotificationViewController new];
_messageviewcontroller = [MessagesViewController new];
self.tabbarcontroller = [[UITabBarController alloc]init];
self.viewControllers = [NSArray arrayWithObjects:_homeviewcontroller,_messageviewcontroller,_friendviewcontroller,_notifviewcontroller, nil];
//UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [self.tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [self.tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [self.tabBar.items objectAtIndex:3];
[item0 setFinishedSelectedImage:[UIImage imageNamed:#"blue-home-icon.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"home_2.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:#"email-icon-env.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"message_2.png"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:#"friendreq_icon.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"friendreq_2.jpg"]];
[item3 setFinishedSelectedImage:[UIImage imageNamed:#"notification_icon.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"bell-512.png"]];
UIImage *img1=[UIImage imageNamed:#"menu-2-128.png"];
CGRect frameimg1 = CGRectMake(340, 0, 35,35);
UIButton *signOut=[[UIButton alloc]initWithFrame:frameimg1];
[signOut setBackgroundImage:img1 forState:UIControlStateNormal];
[signOut addTarget:self action:#selector(collectionmenutwo:)forControlEvents:UIControlEventTouchUpInside];
[signOut setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *barButton=[[UIBarButtonItem alloc]initWithCustomView:signOut];
self.navigationItem.rightBarButtonItem=barButton;
item0.title = #"Home";
item1.title = #"Messages";
item2.title = #"Friends Requests";
item3.title = #"Notification";
}
take a UITabBarController and make this rootViewController.
and add all of your subview on this TabBarController.
code sample
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
UITabBarController *tabBarController = [[UITabBarController alloc]init];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
FirstViewController *firstViewController = [storyboard instantiateViewControllerWithIdentifier:#"FirstViewController"];
SecondViewController *secondViewController = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
tabBarController.viewControllers = #[firstViewController,secondViewController];
[self.window setRootViewController: tabBarController];
[self.window makeKeyAndVisible];
But don't add NavigationController

UINavigationView can not pop, Only navigation pop animation, UIViewController not changed

I use the custom UINavigationController as a rootViewController. The UINavigationController's first viewController is a UITabBarController. Each UITabBarController is a custom UINavigationConller.When the tabBarController is shown, I hide the rootViewController's navigationBar.
Init the UITabbarController
+(RDVTabBarController *)tabBarControllertWithIndex:(NSUInteger)index
{
UIViewController *goodsHomeController = [[ESGoodsHomeViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *goodsHomeNavi = [[ESNavigationController alloc]
initWithRootViewController:goodsHomeController];
........
ESTabBarController *tabBarController = [[ESTabBarController alloc] init]; //对status bar 能定制
[tabBarController setViewControllers:#[goodsHomeNavi,categoryNavi,shoppingCarNavi,userCenterNavi]];
return tabBarController;
}
Set it to a navigationController
self.tabController = [UIHelper tabBarControllertWithIndex:0];
self.tableController.delegate = self;
UINavigationController *tabBarNavigation = [UIHelper navigationControllerViewController:self.tableController];
tabBarNavigation.navigationBarHidden = TRUE;
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
self.window.rootViewController = tabBarNavigation;
In the baseViewController ,I customise the back item in the navigationBar
- (void)setCustomNavigationBackButton
{
UIImage *backBtn = [UIImage imageNamed:#"bar_back"];
UIImage *backDownBtn = [UIImage imageNamed:#"bar_back_down"];
backBtn = [backBtn imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
backDownBtn = [backDownBtn imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.navigationItem.backBarButtonItem.title=#"";
self.navigationController.navigationBar.backIndicatorImage = backBtn;
self.navigationController.navigationBar.backIndicatorTransitionMaskImage = backBtn;
}
In some cases, I change the viewControllers of a navigationController when I push a viewController after the push animation is finished. I use a navigationController+block
- (void)popToViewController:(UIViewController *)viewController animated:(BOOL)animated onCompletion:(void(^)(void))completion
{
[CATransaction begin];
[CATransaction setCompletionBlock:completion];
[self popToViewController:viewController animated:animated];
[CATransaction commit];
}
After finished the block is executed
[UIHelper viewController:self pushViewController:orderDetail completion:^{
NSMutableArray *afterController = [NSMutableArray array]; //将本页面删除
NSArray *viewController = self.navigationController.viewControllers;
[viewController enumerateObjectsUsingBlock:^(UIViewController *obj, NSUInteger idx, BOOL *stop) {
if (![obj isKindOfClass:[ESPurchaseViewController class]]) {
[afterController addObject:obj];
}
}];
self.navigationController.viewControllers = afterController;
}];
In some push, when I come from the UITabBarController push to a secondViewController, I hide navigationBar and show the root navigationBar.
+(void)tabController:(UIViewController *)tabController pushSubController:(UIViewController *)subViewController
{
[tabController.rdv_tabBarController.navigationController pushViewController:subViewController animated:YES];
tabController.rdv_tabBarController.navigationController.navigationBarHidden = FALSE;
}
I can not find the reason why the navigation can not pop. That does not always happen.
Any wrong basic knowledge of iOS about the UINavigationContller,UITabBarController,UIViewController for me to know?
Thank you!
if i think your need is First MainNavigationViewController then tabbar Controller with it and each tab bar's view controller is also having its own nvaigation controller then it will work fine..
listViewController = [[CBListViewController alloc] initWithStyle:UITableViewStylePlain];
bookmarkController = [[CBBookmarksViewController alloc] initWithStyle:UITableViewStylePlain];
settingsController = [[CBActivityViewController alloc] init ];
searchController = [[CBSearchViewController alloc] initWithNibName:#"CBSearchViewController" bundle:nil];
nearbyController = [[CBViewOnMapViewController alloc] init ];
UINavigationController *bNavigationController = [[UINavigationController alloc] initWithRootViewController:bookmarkController];
self.navigationControllerForBookmark = bNavigationController;
UITabBarItem *tab2=[[UITabBarItem alloc]init];
tab2.image = [[UIImage imageNamed:#"bookmark.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tab2.selectedImage = [[UIImage imageNamed:#"bookmark_active.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tab2.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
navigationControllerForBookmark.tabBarItem = tab2;
UINavigationController *cNavigationController = [[UINavigationController alloc] initWithRootViewController:settingsController];
self.navigationControllerForSettings = cNavigationController;
UITabBarItem *tab3=[[UITabBarItem alloc]init];
tab3.image = [[UIImage imageNamed:#"activites.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tab3.selectedImage = [[UIImage imageNamed:#"activites_active.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tab3.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
navigationControllerForSettings.tabBarItem = tab3;
UINavigationController *dNavigationController = [[UINavigationController alloc] initWithRootViewController:searchController];
self.navigationControllerForSearch = dNavigationController;
UITabBarItem *tab4=[[UITabBarItem alloc]init];
tab4.image = [[UIImage imageNamed:#"Search.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tab4.selectedImage = [[UIImage imageNamed:#"Search_active.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.navigationController.tabBarItem = tab4;
tab4.imageInsets=UIEdgeInsetsMake(5, 0, -5, 0);
navigationControllerForSearch.tabBarItem=tab4;
UINavigationController *eNavigationController = [[UINavigationController alloc] initWithRootViewController:nearbyController];
self.navigationControllerForNearby = eNavigationController;
UITabBarItem *tab5=[[UITabBarItem alloc]init];
tab5.image = [[UIImage imageNamed:#"nearby.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tab5.selectedImage = [[UIImage imageNamed:#"nearby_active.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.navigationController.tabBarItem = tab5;
tab5.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
navigationControllerForNearby.tabBarItem = tab5;
[navigationControllerForNearby.navigationBar setBackgroundImage:[UIImage imageNamed:#"headerBar"] forBarMetrics:UIBarMetricsDefault];
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:listViewController];
self.navigationController = aNavigationController;
UITabBarItem *tab1=[[UITabBarItem alloc]init];
tab1.image = [[UIImage imageNamed:#"review.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tab1.selectedImage = [[UIImage imageNamed:#"review_active.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tab1.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);
navigationController.tabBarItem = tab1;
[[UITabBar appearance] setBarTintColor:GRAY_COLOR];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController, navigationControllerForNearby, navigationControllerForSearch, navigationControllerForBookmark, navigationControllerForSettings, nil];
self.window.rootViewController = self.tabBarController;
check if this solves your problem sir.

How to change default ViewController of UITabBarController

I create basic TabBarController with 2 ViewControllers.
It worked but it have bug and i want something about it.
Problem1.
When I run (default ViewController is FirstViewController) name of SecondViewController in TabBar is not appear.
I want following this picture.
Problem2.
In my code default view is FirstViewController.
If I want to set default view is SecondViewController
by Same Tab Bar(FirstView Tab : Left ,and SecondView Tab : Right)
following this picture. How to resolve it.
This is my example code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
FirstViewController *firstVC = [[FirstViewController alloc] init];
UINavigationController *firstNVC = [[UINavigationController alloc] initWithRootViewController:firstVC];
firstNVC.navigationBar.barStyle = UIBarStyleBlack;
SecondViewController *secondVC = [[SecondViewController alloc] init];
UINavigationController *secondNVC = [[UINavigationController alloc] initWithRootViewController:secondVC];
secondNVC.navigationBar.barStyle = UIBarStyleBlack;
UITabBarController *tabController = [[UITabBarController alloc]init];
tabController.navigationItem.hidesBackButton = NO;
tabController.viewControllers = [NSArray arrayWithObjects:firstNVC, secondNVC, nil];
tabController.navigationController.navigationBarHidden = NO;
self.window.rootViewController = tabController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Thank You. ^^
UIViewController *viewController1 = [[[viewController1 alloc] initWithNibName:#"viewController1" bundle:nil] autorelease];
UIViewController *viewController2 = [[[viewController2 alloc] initWithNibName:#"viewController2" bundle:nil] autorelease];
UIViewController *viewController3 = [[[viewController3 alloc] initWithNibName:#"viewController3" bundle:nil] autorelease];
navControl1=[[UINavigationController alloc]initWithRootViewController:viewController1];
navControl2=[[UINavigationController alloc]initWithRootViewController:viewController2];
navControl3=[[UINavigationController alloc]initWithRootViewController:viewController3];
navControl1.navigationBar.tintColor=[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
navControl2.navigationBar.tintColor=[UIColor blackColor];
navControl3.navigationBar.tintColor=[UIColor blackColor];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.delegate=self;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navControl2,navControl1,navControl3, nil ];
[[[[self.tabBarController tabBar] items] objectAtIndex:2] setFinishedSelectedImage:[UIImage imageNamed:#""] withFinishedUnselectedImage:[UIImage imageNamed:#""]];
[[[[self.tabBarController tabBar] items] objectAtIndex:0] setTitle:#"1"];
[[[[self.tabBarController tabBar] items] objectAtIndex:0] setImage:[UIImage imageNamed:#"tab2.png"]];
[[[[self.tabBarController tabBar] items] objectAtIndex:1] setTitle:#"2"];
[[[[self.tabBarController tabBar] items] objectAtIndex:1] setImage:[UIImage imageNamed:#"tab11.png"]];
[[[[self.tabBarController tabBar] items] objectAtIndex:2] setTitle:#"3"];
[[[[self.tabBarController tabBar] items] objectAtIndex:2] setImage:[UIImage imageNamed:#"tab5.png"]];
[self.tabBarController.tabBar setSelectionIndicatorImage:[UIImage imageNamed:#"trans.png"]];
UIImage* tabBarBackground = [UIImage imageNamed:#""];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"trans.png"]];
Problem2. If I want to
set default view is SecondViewController by Same Tab Bar(FirstView Tab
: Left ,and SecondView Tab : Right). How to
resolve it.
Solution:
[self.tabBarController setSelectedIndex:1];
Problem 1:
try to set a title for the view controllers.
Problem 2:
You just set selected index of the tabBarController to 1, that should do it.
Problem1
You haven't set tabbarItem for each navigation controller, I think you should set up a UITabBarItem with title first, then assign it to tabbarItem property to navigation controller.
Problem2
Just as #Kumar says.

iOS custom tabbar

I just started with iOS development and am just playing around atm.
I'm trying to transform the default tabbar buttons into something more custom.
After some looking around I found you can create custom states for every button, so I did:
UIImage *selectedImage0 = [UIImage imageNamed:#"first.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:#"second.png"];
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
However, I can't get rid of the default button, it changes the image, but it doesn't change my entire button.
Is there something else I need to do?
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
UIImage *selectedImage0 = [UIImage imageNamed:#"first.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:#"second.png"];
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
Check the following Links(Most of the customization of the tab-bar controller)
Implement a Custom Tab Bar
Tabbar with custom colors
Custom Tabbar diff. background
How to Save User Customized Tab Order]
RX -Tabbar controller
Here i've created a custom tab bar and images i took are in a constant file. Here you can replace the image with "foo.png" as per your convenience. Here the serivceImg, contactImg etc are UIImageView which is declared in .h file. Also, don't forget to add UITabBarControllerDelegate as a delegate in your .h file.
-(void)setUpTabBar {
tabBar = [[UITabBarController alloc] init];
Services *firstViewController = [[Services alloc]init];
firstViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:firstViewController];
ContactUs *secondViewController = [[ContactUs alloc]init];
secondViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:2];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:secondViewController];
Bookings *thirdViewController = [[Bookings alloc]init];
thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3];
UINavigationController *thirdNavController = [[UINavigationController alloc]initWithRootViewController:thirdViewController];
Reward *fourthViewController = [[Reward alloc]init];
fourthViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:4];
UINavigationController *fourthNavController = [[UINavigationController alloc]initWithRootViewController:fourthViewController];
tabBar.viewControllers = [[NSArray alloc] initWithObjects:firstNavController, secondNavController, thirdNavController, fourthNavController, nil];
tabBar.delegate=self;
tabBar.selectedIndex=0;
[firstNavController release];
[firstViewController release];
[secondNavController release];
[secondViewController release];
[thirdNavController release];
[thirdViewController release];
[fourthNavController release];
[fourthViewController release];
serivceImg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 432, 80, 49)];
serivceImg.image=[UIImage imageNamed:serviceHover];
contactImg=[[UIImageView alloc]initWithFrame:CGRectMake(81, 432,80, 49)];
contactImg.image=[UIImage imageNamed:tabContact];
bookingImg=[[UIImageView alloc]initWithFrame:CGRectMake(162, 432,80, 49)];
bookingImg.image=[UIImage imageNamed:tabBooking];
rewardImg=[[UIImageView alloc]initWithFrame:CGRectMake(243, 432, 80, 49)];
rewardImg.image=[UIImage imageNamed:tabReward];
[tabBar.view addSubview:serivceImg];
[tabBar.view addSubview:contactImg];
[tabBar.view addSubview:bookingImg];
[tabBar.view addSubview:rewardImg];
[[[UIApplication sharedApplication]keyWindow]addSubview:tabBar.view];
[serivceImg release];
[contactImg release];
[bookingImg release];
[rewardImg release];
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController1{
if (viewController1 == [tabBar.viewControllers objectAtIndex:0])
{
serivceImg.image = [UIImage imageNamed:kserviceHover];
contactImg.image=[UIImage imageNamed:ktabContact];
bookingImg.image=[UIImage imageNamed:ktabBooking];
rewardImg.image=[UIImage imageNamed:ktabReward];
}
else if (viewController1 == [tabBar.viewControllers objectAtIndex:1])
{
serivceImg.image = [UIImage imageNamed:ktabService];
contactImg.image=[UIImage imageNamed:kcontactHover];
bookingImg.image=[UIImage imageNamed:ktabBooking];
rewardImg.image=[UIImage imageNamed:ktabReward];
}
else if (viewController1 == [tabBar.viewControllers objectAtIndex:2])
{
serivceImg.image = [UIImage imageNamed:ktabService];
contactImg.image=[UIImage imageNamed:ktabContact];
bookingImg.image=[UIImage imageNamed:kbookingHover];
rewardImg.image=[UIImage imageNamed:ktabReward];
}
else if (viewController1 == [tabBar.viewControllers objectAtIndex:3])
{
serivceImg.image = [UIImage imageNamed:ktabService];
contactImg.image=[UIImage imageNamed:ktabContact];
bookingImg.image=[UIImage imageNamed:ktabBooking];
rewardImg.image=[UIImage imageNamed:krewardHover];
}
}
Try this.This might help you.

Resources