iOS Tab Bar Image Size Increasing On Tap - ios

I have a tab bar on iOS and I set the tab bar images using SVGs. The SVGs are set via the XamSvg plugin.
I want the central tabbar item image to be larger than the ones on the other tabs. To do so, I use this code in
In AwakeFromNib:
CenterTab.Image = SvgFactory.FromBundle(ViewModel.Images.CenterTabSvg, 24);
CenterTab.ImageInsets = new UIEdgeInsets(-5, -5, -5, -5);
It works but every time I tap on the center tab, its image size keeps increasing.
I also tried this with no success:
var centerImage = SvgFactory.FromBundle(ViewModel.Images.CenterTab, 40).ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
CenterTab.SetFinishedImages(centerImage,centerImage);
centerImage.ImageInsets = new UIEdgeInsets(3,-13,-5,-5);

Try This :
UITabBarItem *centerTab = [self.tabBarController.tabBar.items objectAtIndex:2];
[centerTab setImage:[[UIImage imageNamed:#"iconGray.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];//Your Image
[centerTab setSelectedImage:[[UIImage imageNamed:#"iconBlue.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];//Your Image

Related

UITabBarController background image is not set correctly

I am trying to add UITabBarController programmatically. Everything is working fine but I am having two issues.
I am setting tabbar background image but it shows a different image which I dont even have in resources.
I am using this image as tabbar background image with a green line above :
But it shows another green line at bottom like this:
Here is the code I am using for this:
[self.myTabBarController.tabBar setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"tabbar.png" ofType:nil]]];
Another issue is, I am setting tabbar item images using this code:
MyViewController *myController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
UINavigationController *myNavController = [[UINavigationController alloc] initWithRootViewController: myController];
[myNavController.tabBarItem initWithTitle:#"" image:[UIImage imageNamed:#"ads_inactive.png"] selectedImage:[UIImage imageNamed:#"ads_active.png"]];
Images are set but when i try to add title in MyViewController's viewDidLoad using this:
self.title = #"My Ads";
It shows same title on tabbar item too but I dont want any title there.
How I can fix this issue?
Thanks
The image size is less than the frame size of tab bar , so to cover to frame area background image show two times.
You can change it in two ways
1.)Change the image size that will be not the best option
2.) set the content insets of tabBar like (0,0,0,0)
Example:
UITabBar *tabBar = self.tabBarController.tabBar;
iterm0.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
UITabBarItem *iterm0 = [tabBar.items objectAtIndex:0];
[iterm0 setImage:[[UIImage imageNamed:#"tab1_normal"]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[iterm0 setSelectedImage:[[UIImage imageNamed:#"tab1_selected"]
[(UITabBarItem *)[self.tabBarController.tabBar.items objectAtIndex:0] setTitle:#""];

How to resize the UITabBarItem?

My code here:
for (NSInteger i = 1; i<= 4 ; i ++) {
UIViewController *VC = self.viewControllers[i-1];
UIImage *itemImage = [[UIImage imageNamed:[self getTabBarItemImageNameFromIndex:i selected:NO]] imageWithRenderingMode:UIImageRenderingModeAutomatic];
UIImage *itemImageSelected = [[UIImage imageNamed:[self getTabBarItemImageNameFromIndex:i selected:YES]]imageWithRenderingMode:UIImageRenderingModeAutomatic];
UITabBarItem *item = [[UITabBarItem alloc]initWithTitle:[self getTabBarItemTitleWithIndex:i] image:itemImage selectedImage:itemImageSelected];
item.tag = i-1;
VC.tabBarItem = item;
}
First I run :
I want to shrink the UITabBarItem size, the original size of picture is 36 × 36 pixels.
Then I add these code:
item.imageInsets = UIEdgeInsetsMake(3, 3, 3, 3);
It does well,but when I press the TabBarItem the size of item was changed
I know the issue is about the setting of imageInsets, but I don't know how to resize the TabBarItem without to set imageInsets.
I don't think that the standard UITabBar control is very customizable. I would recommend you create your own custom tab bar which fits your need, or use something like UISegmentedControl which is quite customizable.
Have a look at these threads for reference:
how to resize iphone tab bar in xcode?
How do I resize the tab bar items in Xcode 4.3.1?

Show stack of images one on top of the other in iOS

I'm trying to develop a simple app that shows a stack of images: each image on top of the other. And when you swipe or click a button, the first image will disappear and the image below it will have the same size as the one that was on the top (as in the image below, but without the rotation effect)
I've tried to fetch the images and create the frame of the images at each for loop iteration:
UIImageView*picture = [[UIImageView alloc] initWithFrame:CGRectMake(40+10*i, 101-10*i, 240-20*i, 200)];
UIImage *image = [UIImage imageWithData:imageData];
picture.image = image;
[dView addSubview:picture];
and it worked. But I still can't find how to make the second image in the same size as the one that was on top of it.
CGrect nextImageFrame = nextImage.frame;
nextImageFrame.size.width = 240;
nextImageFrame.size.height = 200;
nextImage.frame = nextImageFrame;
assuming that nextImage is the image you want to show next

how to programmatically change the tabbarItem's image

I'm developing a shopping cart tab. Originally I just use the default badge value to show how many items in the cart on the bottom tabbar. Now the designer wants to be fancy, he wants to show different image based on how many items in the cart. For example, if there's one, show cartTab-1.png, if 2, show cartTab-2.png...
I tried to change the tabaritem (UITabBarItem)'s image but it didn't work for me. Is it feasible? I discussed with my colleague, he said I may have to draw the image on top of the tabbarItem by myself. Do you have any suggestion? Thanks
more details:
I created the tabItem using InterfaceBuilder, and set the image and title over there
I need to support ios4. So I can't use the setSelectedImage...
In my case it's a KVO, if the cart count changes, it notifies the method to update the image. not in the initialize step.
does anyone know why [self.tabBarItem setImage:[UIImage imageNamed:#"cartxxx.png"]] doesn't work? When I debug, the property do changed, but the UI remains same
Update
the below code works. Thanks everyone!
UIImage* cartTabImage = [UIImage imageNamed:cartTabImageName];
[[self.tabBarController.tabBar.items objectAtIndex:3] setImage:cartTabImage];
Swift 3.0 version for 2 tabs,
self.tabBar.items?[0].image = UIImage(named: "inactive_image_0")?.withRenderingMode(.alwaysOriginal)
self.tabBar.items?[0].selectedImage = UIImage(named: "active_image_0")?.withRenderingMode(.alwaysOriginal)
self.tabBar.items?[1].image = UIImage(named: "inactive_image_1")?.withRenderingMode(.alwaysOriginal)
self.tabBar.items?[1].selectedImage = UIImage(named: "active_image_1")?.withRenderingMode(.alwaysOriginal)
UITabBarItem *tabBarItem0 = [self.tabBarController.tabBar.items objectAtIndex:0];
[tabBarItem0 setImage:[[UIImage imageNamed:#"iconGray.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem0 setSelectedImage:[[UIImage imageNamed:#"iconBlue.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
The simplest way I found was
self.tabBarItem.image=[UIImage imageNamed:#"myImage.png"];
Try this:
int numItems = 0; // count of items in your shopping cart
NSString *imageName = [NSString stringWithFormat:#"cartTab-%d",numItems];
// change your image
[[self.tabBar.items objectAtIndex:myIndex] setImage:[UIImage imageNamed:imageName]];
// or, if you want to set it when initializing the tabBar
UITabBarItem *item = [[[UITabBarItem alloc] initWithTitle:myTitle image:[UIImage imageNamed:imageName] tag:someTag];
This Answer May be Help You
UITabBarItem *i5=[[UITabBarItem alloc]initWithTitle:#"Profile" image:[UIImage imageNamed:#"profile.png"] tag:5];
- (void)setFinishedSelectedImage:(UIImage *)selectedImage withFinishedUnselectedImage:(UIImage *)unselectedImage
selectedImage is displayed when the user has selected the tab. unselectedImage is displayed when the user has selected a different tab.
in your viewDidLoad: do
UIImage *c1 = [UIImage imageNamed:#"cart1.png"];
UIImage *c2 = [UIImage imageNamed:#"cart1unselected.png"];
[[self tabBarItem] setFinishedSelectedImage:c1 withFinishedUnselectedImage:c2];
As mentioned on the updated question and other answers, in most cases the UITabBarItem needs to be accessed via the UITabBarController directly.
It seems that iOS creates a copy of the UITabBarItem instance, which explains why updating the self.tabBarItem property does not reflect the changes in the user interface:
My guess is that this happens when the Tab Bar items are created programmatically, instead of by the storyboard, but this is just a guess.
The solution is then, as pointed out, accessing the array of Tab Bar items via the Tab Bar controller. This solution is bad in that it depends on the knowledge of the tab bar item index:
UITabBarItem *tabBarItem = [self.tabBarController.tabBar.items objectAtIndex:0];
[tabBarItem setImage:image];
[tabBarItem setSelectedImage:image];
Don't forget to update both images for default and selected states.
let favorites = UITabBarItem(title: nil, image:UIImage(named: "Screen Shot 2018-12-13 at 11.00.42 AM") , tag: 0)

Adding a background image to UINavigationBar covers the title

I have successfully added a background image to my UINavigationBar with the following code:
UIImageView *barView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"subHeader_lg.png"]];
[calculatorBar addSubview:barView];
[barView release];
However, with this method, the title of the view is covered by the image.
If I navigate further into the app, and then back, the title appears on top of the background image like so:
Any ideas how I can get the title to appear on top from the beginning?
I have tried pushing the barView to the back, but that makes it hidden behind everything else.
EDIT:
It seems that the custom draw function is the accepted answer, but I am unable to get the draw function to be called. I have this code at the bottom of my appdelegate.m file
#implementation UINavigationBar (UINavigationBarCustomDraw)
- (void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed: #"subHeader_lg.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
When you call "addSubview" it adds it above any view that have already been added, thus covering the title.
What you want is
[calculatorBar insertSubview:barView atIndex:0];
However, this won't make it "stick" on subsequent pushes, so use the methods described at http://www.developers-life.com/custom-uinavigationbar-with-image-and-back-button.html for a better solution.
Also in iOS 5, Apple has added a built in way to customize the nav bar see http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIAppearance_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40010906
To set the background image on your navigation bar, use the following.
On iOS 5.x, use
[calculatorBar setBackgroundImage: barView.image forBarMetrics: 0];
On iOS 4.x use
calculatorBar.layer.contents = (id)barView.image.CGImage;

Resources