How to remove space below the UITabBarItem? - iphone

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

Related

How to place tab bar on top of the view

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];

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.

Why don't my TabBar buttons autoresize on the iPad?

I'm building a universal iOS app and the iPad version uses a SplitViewController. In the popover view, I have a UITabBarController with two buttons. When it runs on the iPhone, the TabBar buttons correctly stretch to fill the entire width of the view...
...but on the iPad, in the popover view, the buttons don't stretch to fill the entire width...
I'm creating the UITabBarController programmatically...
InspectionTabBarViewController *inspectionTabBarVC;
InspectionListViewController *inspectionListVC;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
inspectionListVC = [[InspectionListViewController alloc] initWithSunday:NO];
inspectionListVC.managedObjectContext = self.managedObjectContext;
UINavigationController *calendarNavVC = [[UINavigationController alloc] initWithRootViewController:inspectionListVC];
calendarNavVC.title = #"Calendar";
InspectionMapViewController *mapViewVC = [[InspectionMapViewController alloc] initWithNibName:#"InspectionMapView_iPhone" bundle:nil];
UINavigationController *mapdNavVC = [[UINavigationController alloc] initWithRootViewController:mapViewVC];
mapdNavVC.title = #"Map";
inspectionTabBarVC = [[InspectionTabBarViewController alloc] init];
[inspectionTabBarVC addChildViewController:calendarNavVC];
[inspectionTabBarVC addChildViewController:mapdNavVC];
self.window.rootViewController = inspectionTabBarVC;
}
else
{
inspectionListVC = [[InspectionListViewController alloc] initWithSunday:NO];
UINavigationController *calendarNavVC = [[UINavigationController alloc] initWithRootViewController:inspectionListVC];
calendarNavVC.title = #"Calendar";
InspectionMapViewController *mapViewVC = [[InspectionMapViewController alloc] initWithNibName:#"InspectionMapView_iPad" bundle:nil];
UINavigationController *mapdNavVC = [[UINavigationController alloc] initWithRootViewController:mapViewVC];
mapdNavVC.title = #"Map";
inspectionTabBarVC = [[InspectionTabBarViewController alloc] init];
[inspectionTabBarVC addChildViewController:calendarNavVC];
[inspectionTabBarVC addChildViewController:mapdNavVC];
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController_iPad" bundle:nil];
UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:detailViewController];
self.splitViewController = [[UISplitViewController alloc] init];
self.splitViewController.delegate = detailViewController;
self.splitViewController.viewControllers = [NSArray arrayWithObjects:inspectionTabBarVC, detailNavigationController, nil];
self.window.rootViewController = self.splitViewController;
inspectionListVC.detailViewController = detailViewController;
inspectionListVC.managedObjectContext = self.managedObjectContext;
detailViewController.detailViewControllerDelegate = inspectionListVC;
}
[self.window makeKeyAndVisible];
I also tried setting the autoResizeMask inside the InspectionTabBarViewController's loadView method using the following statement...
self.tabBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
...but that didn't work either. How can I get the UITabBar buttons to fill the entire width of of the view?
Thanks so much in advance for your help!
Change UITabBar property itemPositioning to UITabBarItemPositioningFill:
self.tabBar.itemPositioning = UITabBarItemPositioningFill;
Swift version:
tabBar.itemPositioning = .fill
UITabBar itemPositioning reference
This can actually be done by setting the setSelectionIndicatorImage on the tab bar. The buttons will resize to your image's width, regardless of iPhone or iPad.
The problem with this is that it's not dynamic, and every time you add or change the number of items in the tab bar, you'll have to create another image.
A solution is to create the image in code and calculate the width by ({tab bar width}/{# of tabbaritems}).
For example:
Where you create the tab bar:
[[self.tabBarController tabBar] setSelectionIndicatorImage:[self selectionIndicatorImage]];
The image method:
- (UIImage *)selectionIndicatorImage
{
NSUInteger count = [[self.tabBarController viewControllers] count];
CGSize tabBarSize = [[self.tabBarController tabBar] frame].size;
NSUInteger padding = 2;
CGSize buttonSize = CGSizeMake( tabBarSize.width / count, tabBarSize.height );
UIGraphicsBeginImageContext( buttonSize );
CGContextRef c = UIGraphicsGetCurrentContext();
[[UIColor colorWithWhite:0.9 alpha:0.1] setFill];
UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:CGRectMake( padding, padding * 2, buttonSize.width - (padding * 2) , buttonSize.height - ( padding * 2 ) ) cornerRadius:4.0];
[roundedRect fillWithBlendMode: kCGBlendModeNormal alpha:1.0f];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGContextRelease( c );
return image;
}
We can not set size for the UITabbarItem dynamically.
In iPhone it sets (Devices Width/no of Tabbaritems)
In iPad it sets with specific width
If you want to create such kind of view, then go with custom buttons which looks like tab bar buttons and add functionality by your code for the same.
I found itemWidth(property of UITabBar) in iOS7.
UITabBar reference

UIPopoverController without arrows?

I would like to know to make an UIPopoverController without arrows
In fact I would like to simulate something like this:
See that
There is no arrows
There is a title that is somehow inside of a expanded top border of the UIPopoverController and not inside of it like in the normal UIPopoverController.
I assume this is not really an UIPopoverController object but I would appreciate advices on how can I make the same effect (using CoreGraphics? -> specially the translucent degrade effect of the 3D outstanding border) and/or links to some sources if anyone has done this before.
Thanks in advance.
Ignacio
EDIT:
I am still looking for this stuff and realized that even in third party apps is being used
an example is: twitterrific for iPad as seen in this picture.
Anyone please? Putting the title inside the popovercontroller is just ugly.
The below method works fine for me (include iOS7)
[popoverController presentPopoverFromRect:CGRectMake(0, 0, 20, 20)
inView:self.view
permittedArrowDirections:NULL
animated:YES];
Pass 0 to permittedArrowDirections attribute.
[popoverController presentPopoverFromRect:YOUR_RECT
inView:self.view
permittedArrowDirections:0
animated:YES];
While there is some question about whether Apple will approve apps that create a popover without an arrow, you might want to check out this post regarding arrows and this post regarding modal views.
To create a popover with a title you need to create a separate view like you would make a separate window and then load that view in the popover.
The top border is produced by placing a navigation controller between the popover and the presented view controller.
In other words, the popover presents a navigation controller and the navigation controller's root view controller is set to your view controller. This produces the title bar and allows you to set the title with [self setTitle:#"My Title"] and add navigation buttons.
You can add a title by using a UINavigationController, and adding UIViewControllers to the navigation controller. Set the 'title' attribute of the UIViewController to make the title appear.
Setting the arrow direction to NULL, as some have suggested, can result in unpredictable behavior, since the method uses this variable to figure out how to orient the popup relative to your bar button item or rectangle.
It is better to subclass UIPopoverBackgroundView, and set the various arrow return methods to return 0 for the arrows (iOS5 and up only). See this example for how to subclass this properly:
http://blog.teamtreehouse.com/customizing-the-design-of-uipopovercontroller
Simple implementation example (MyCustomPopoverBGView is the subclass of UIPopoverBackgroundView in this example):
UIViewController *vCtrlr = [[UIViewController alloc] initWithNibName:nil bundle:nil];
vCtrlr.title = #"My Title";
self.navCtrlr = [[UINavigationController alloc] initWithRootViewController:vCtrlr];
self.popCtrlr = [[UIPopoverController alloc] initWithContentViewController:_navCtrlr];
_popCtrlr.popoverBackgroundViewClass = [MyCustomPopoverBGView class];
[_popCtrlr presentPopoverFromRect:CGRectMake(0,
0,
320,
150)
inView:self permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
Just copy & Paste the below code
UIViewController *popovercontroller=[[UIViewController alloc] init];
UIView *popoverView=[[UIView alloc] initWithFrame:CGRectMake(312,390, 400, 344)];
popoverView.backgroundColor=[UIColor whiteColor];
popovercontroller.contentSizeForViewInPopover=CGSizeMake(400, 300);
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 400, 0)];
[pickerView setTintColor:[UIColor blackColor]];
[pickerView addTarget:self action:#selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
pickerView.datePickerMode = UIDatePickerModeDate;
pickerView.hidden = NO;
NSString *bs ; //= [NSString alloc];
// //NSDate *newDate = [NSData alloc];
bs = CurrentSelectedDate;
if (bs.length >= 1) {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init] ;
// //[dateFormatter setDateStyle:NSDateFormatterLongStyle];
// [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:#"dd-MMM-yyyy"];
// NSDate *myDate = [dateFormatter dateFromString: txtText.text];
pickerView.date = [dateFormatter dateFromString: CurrentSelectedDate];
}
else
{
pickerView.date = [NSDate date];
}
[popoverView addSubview:pickerView];
// pickerView.date = [dateFormatter dateFromString:txtText.text];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
pickerToolbar.barStyle = UIBarStyleDefault;
pickerToolbar.barTintColor=[UIColor colorWithRed:150.0f/255.0f green:91.0f/255.0f blue:129.0f/255.0f alpha:1.0f];
[pickerToolbar sizeToFit];
self.navigationController.toolbar.barTintColor = [UIColor colorWithRed:150.0f/255.0f green:91.0f/255.0f blue:129.0f/255.0f alpha:1.0f];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)];
doneBtn.tintColor=[UIColor whiteColor];
[barItems addObject:doneBtn];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonPressed:)];
cancelBtn.tintColor=[UIColor whiteColor];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[popoverView addSubview:pickerToolbar];
popovercontroller.view=popoverView;
pickerViewPopup = [[UIPopoverController alloc] initWithContentViewController:popovercontroller];
[pickerViewPopup presentPopoverFromRect:CGRectMake(312, 212, 400, 344) inView:self.view permittedArrowDirections:0 animated:YES];

Change pop-over contentsize using navigationcontroller

I want to show a popover with a custom contentsize.
I can do it doing like so
UINavigationController* popoverContent = [[UINavigationController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 800, 800)];
popoverView.backgroundColor = [UIColor blueColor];
popoverContent.view = popoverView;
popoverContent.contentSizeForViewInPopover = CGSizeMake(55, 55);
UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[pop presentPopoverFromRect:CGRectMake(80.0, 210.0, 160.0, 40.0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
but if I change the content to :
popoverContent.viewControllers = [NSArray arrayWithObject:myViewController];
the
popoverContent.contentSizeForViewInPopover = CGSizeMake(55, 55);
the size doesn't change at all.
How can I change the content size while using the navigation controller?
Thanks
Explicitly changing the popovers size works:
[self.popoverController setPopoverContentSize:myView.size animated:YES];
But I agree that contentSizeForViewInPopover should work. It doesn't for me anyway.