How to place tab bar on top of the view - iphone

I'm developing an iPhone app where I want to place tab bar on top.
Here is my code
tabBarController = [[UITabBarController alloc]init];
tabBarController.view.frame= CGRectMake(0, 0, 320, 40);
NSArray *arr = [[NSArray alloc]initWithObjects:[[DemoView alloc] init], [[DemoView alloc] init], nil];
tabBarController.viewControllers = arr;
[tabBarController.view setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:tabBarController.tabBar];
But I cant see tab bar. I don't what the problem is.
Thanks

you dont have to take another view to add tabBar. You can directly add to your window

This code worked for me:
UITabBarController *tabC = [[UITabBarController alloc]init];
tabC.tabBar.frame = CGRectMake(0, 0, 320, 70);
NSArray *arr = [[NSArray alloc]initWithObjects:firstObj,secObj, nil];
tabC.viewControllers = arr;
[self.window addSubview:tabC.view];

Related

How to remove space below the UITabBarItem?

I have created a tabBar and set image in it, but it leaves too much space below the tabBarItem. How can I remove that?
This is my tabBar than displaying right now
And I want to display it like this
To display Tabbar
firstVC = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
secondVC = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
thirdVC = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
forthVC = [[ForthViewController alloc] initWithNibName:#"ForthViewController" bundle:nil];
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
NSArray *viewControllersArray = [[NSArray alloc] initWithObjects:firstVC,secondVC,thirdVC,forthVC, nil];
self.tabController = [[UITabBarController alloc] init];
[self.tabController setViewControllers:viewControllersArray animated:NO];
[self.window addSubview:self.tabController.view];
//self.tabController.selectedIndex = 1;
self.tabController.delegate = self;
self.window.rootViewController = self.tabController;
[self.window makeKeyAndVisible];
For tabBar background image i have used this code
UIImage* tabBarBackground = [UIImage imageNamed:#"tabbarimg1.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
And for set the imge at item i have used this code
//used to set the tabBarItem images
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"home_tab"] withFinishedUnselectedImage:[UIImage imageNamed:#"home_tab"]];
//Set the badge on tabBarItem
[self.tabBarItem setBadgeValue:#"15"];
As Vytis says in their answer here:
There is a property on UIBarItem (UIBarButton item inherits from this
class) imageInsets.
To use full height images (49px) for finishedSelectedImage and
finishedUnselectedImage you need to set these image insets:
tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
you have to write the following lines of code
item0.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
item1.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
item2.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
item2.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
I think you should use image insect in the xib for this like ,
for your requirement top should be some positive value and bottom should be some same negative value . If you have added the tab bar programmatically then you can refer the Wills answer

UIBarButtonItems of a UIToolbar hiding when presenting UIActivityViewController

This looks like a bug to me, but maybe someone can think of a workaround?
Basically if you have a custom UIToolbar, its button items will automatically hide when you present a UIActivityViewController, and reappear when you dismiss it. This is only the case on the iPhone. Being that UIActivityViewController doesn't hide the entire screen it just looks weird that buttons disappear behind the dimmed screen.
To replicate, simply start a single view project and use the following code on the view controller:
- (void)viewDidLoad {
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(didTapAction)];
toolbar.items = [NSArray arrayWithObject:button];
[self.view addSubview:toolbar];
}
- (void)didTapAction {
NSArray *items = [NSArray arrayWithObjects:#"Text", nil];
UIActivityViewController *sharing = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
[self presentViewController:sharing animated:YES completion:nil];
}
Found a workaround. Simply get rid of all the items before presenting, and add them back right after.
- (void)didTapAction {
NSArray *items = [NSArray arrayWithObjects:#"Text", nil];
UIActivityViewController *sharing = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
NSArray *barItems = toolbar.items;
toolbar.items = nil;
[self.navigationController presentViewController:sharing animated:YES completion:nil];
toolbar.items = barItems;
}
know it is quite old thread for but those who look this page for solution, here you go :
With iOS7, you can use this approach to show/hide your toolbar button :
if(// your code Condition)
{ self.toolbarBtn1.enabled = YES;
self.toolbarBtn1.tintColor = nil; }
else
{ self.toolbarBtn1.enabled = NO;
self.toolbarBtn1.tintColor = [UIColor clearColor]; }

How to set Tabbar integration in iPhone?

I am working on an iPhone app, using UITabBar to develop it. Only the second tabbar title visible in tabbar, but first tabbar title not show in tabbar, how to fix this? please help me,
Thanks in Advance.
below is the source code for your reference.
NSMutableArray *items = [[NSMutableArray alloc]init];
tab =[[UITabBar alloc]initWithFrame:CGRectMake(0, 300, 320, 44)];
[self.tabBarItem initWithTitle:#"sample1" image:nil tag:111];
[items addObject:self.tabBarItem];
[self.tabBarItem initWithTitle:#"sample2" image:nil tag:101];
[items addObject:self.tabBarItem];
[tab setItems:items animated:YES];
[self.view addSubview:tab];
Dont navigate the view controllers, because then your controller which contains the TabBar will get popped out.
When we use UITabBar we dont use navigation in this way.
Just addSubView the new controllers with such frame so that your TabBar will alwaz be visible.
You have to add that tabbarItem in UITabBar.
NSArray *items = [[NSArray alloc]init];
[items addObject:self.tabBarItem]; //add all your tabBarItems in an array
[tab setItems:items animated:YES]; //then set all your tabBarItems in UITabBar like this
Actually you are using same instance of tabBarItem thats'y you are getting only second button.
Do this way, and Please if this helps you then accept the answer.
UITabBar *tabBar = [[UITabBar alloc]initWithFrame:CGRectMake(0, 725, 768, 49)];
NSMutableArray *items = [[NSMutableArray alloc]init];
UITabBarItem *item1 = [[UITabBarItem alloc]initWithTitle:#"first" image:nil tag:1];
UITabBarItem *item2 = [[UITabBarItem alloc]initWithTitle:#"second" image:nil tag:2];
UITabBarItem *item3 = [[UITabBarItem alloc]initWithTitle:#"third" image:nil tag:3];
UITabBarItem *item4 = [[UITabBarItem alloc]initWithTitle:#"fourth" image:nil tag:4];
[items addObject:item1];
[items addObject:item2];
[items addObject:item3];
[items addObject:item4];
[tabBar setItems:items];
[self.view addSubview:tabBar];
That's not the way you do to create and add a TabBar programmatically.
You'd rather use this other method: https://stackoverflow.com/a/3844365/655221
Good luck.

Show iAdBanner on top of nav bar

i want to show iAd Banner on top of Nav Bar.
I tried with the following code
Devanagari *viewController = [[Devanagari alloc]init];
viewController.delegate = self;
// _adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, 0, 320,40)];
_adView.frame = CGRectOffset(_adView.frame, 0, -50);
_adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
_adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
_adView.delegate=self;
self.bannerIsVisible=YES;
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:viewController];
navigationController.navigationBar.tintColor = [UIColor brownColor];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
didn't worked.
Is there any way that i can show iAdBanner View on top of the view and then right below nav bar.
Thanks for help.

How to Give action to the tab bar item

This is my code for my tab bar
- (void)viewDidLoad {
[super viewDidLoad];
self.title = NSLocalizedString(#"Crops ", #"Articles");
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 376, 320, 44)];
item1 = [[UITabBarItem alloc] initWithTitle:#"TypesToTry" image:[UIImage imageNamed:#"crops.png"] tag:0];
item2 = [[UITabBarItem alloc] initWithTitle:#"WhenToPlant" image:[UIImage imageNamed:#"crops.png"] tag:1];
NSArray *items = [NSArray arrayWithObjects:item1,item2,item3,item4,item5, nil];
[tabBar setItems:items animated:YES];
[tabBar setSelectedItem:nil];
tabBar.delegate=self;
[self.view addSubview:tabBar];
}
what i want is whenever i tap on the 1st tab i want to go to that particular description ,so where to right this action.Please help me in this.
The questions may be simple,but for me they are difficult :),Please help this new developer
Thanks
Hi i think you can add following line of code to make your tab bar item workable
[item1 performSelectorOnMainThread:#selector(Yourfunction) withObject:nil waitUntilDone:NO];
[item2 performSelectorOnMainThread:#selector(Yourfunction) withObject:nil waitUntilDone:NO];
put this line at end of -(void) didLoad, i hope it will work for you