How To change Tab Bar Seleced Image? | Swift - swift

While it was pretty easy to set it up in Objective C,
I find it difficult with Swift.
I'm having a Page View Controller which leads me to a Tab Controller (kind of tutorial).
I'm trying to change the selected and unselected to my two images I've got in the Tab Bar, when selected It will chose "imageSelected.png" when not selected it will go with "imageNotSelected.png" (check the snippet code below)
Trying this snippet of code not doing anything
tabBarItem.selectedImage = UIImage(named: "someImage.png")!.imageWithRenderingMode(.AlwaysOriginal)
Any help would be appreciated.
Edit: This is the Objective C I used in my older project which do work.
UITabBarController *tabBarController=(UITabBarController*)(self.window.rootViewController);
tabBarController.selectedIndex = 1; // SELTECT INDEX OF TAB BAR
UITabBar *tabBar=tabBarController.tabBar;
UITabBarItem *tabBarItem1=[[tabBar items] objectAtIndex:0]; //first tab bar
UITabBarItem *tabBarItem2=[[tabBar items] objectAtIndex:1];
UIImage *tabrBarImageOne = [[UIImage imageNamed:#“imageSelectedOne.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *tabrBarImageOneOff = [[UIImage imageNamed:#“imageNotSelectedOne.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *tabBarImageTwo = [[UIImage imageNamed:#“imageSelectedTwo.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *tabBarImageTwooff = [[UIImage imageNamed:#“imageNotSelectedTwo.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem1 = [tabBarItem1 initWithTitle:#“title1” image:tabrBarImageOneOff selectedImage:tabrBarImageOne];
tabBarItem2 = [tabBarItem2 initWithTitle:#“title2” image:tabBarImageTwooff selectedImage:tabBarImageTwo];
Note: I've used it in the Objective C code in app delegate which now it's different since I need to load the code differently in each of the the VC I load in the ViewWillAppear.

You'd want to use .AlwaysTemplate instead if you want the tintColor to take effect.

Related

Twitters iPhone NavBar Logo

This is more like a design question but does anyone know how they made the Logo in the UINavigationBar look that good in the native iPhone app?
Does anyone know a tutorial?
Thanks in advance!
A UIViewController's -navigationItem property has itself a property called -titleView.
Set that titleView property of the currently displayed UIViewController's navigationItem to an embossed, semi-transparent icon like Twitter has done, and you're set.
You will load an image, and then put it in the navigationItem.titleView property on the view controller that will be inside the navigation controller.
UIImage *image = [UIImage imageNamed: #"header_logo.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
viewController.navigationItem.titleView = imageView;
Basically both answers were correct.
I'm just typing the code I've used. I like it better than #stevenhepting 's code :)
Just put this code in the viewDidLoad: method
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourPicture"]];
The perfect size for your image (in my opinion) is 80x40 pixels (width=80 / height=40).

UITabBarItem title position

On searching the web on how to adjust the position of a UITabBarItem title position i ran over this similar post and still wondering how to do that.
Is it even possible to adjust the title position a bit from the bottom up? (for example 5px) I need this because i have custom images and now the position of the title is not perfect.
If you want to move up simply set vertical offset to negative value
UITabBarItem* it = [[self.tabController.tabBar items] objectAtIndex:0];
it.titlePositionAdjustment = UIOffsetMake(0.0, -2.0);
You don't have to use proxy as it is done here UITabBarItem title in centre of title, not at bottom.You can define offset per item.
Swift version for the lazy ones :)
UITabBarItem.appearance().titlePositionAdjustment = UIOffsetMake(0.0, -4.0)
If you want to update all of them:
tabBar.items?.forEach({ $0.titlePositionAdjustment = UIOffset(horizontal: 0.0, vertical: -2.0) })
Why don't you just have an empty title property for your view controller and add the title to your custom images for the tab?
You can do this (in iOS 5.0):
UIImage* iconSelected = [UIImage imageNamed:#"tabIconSelected.png"];
UIImage* iconNotSelected = [UIImage imageNamed:#"tabIconNotSelected.png"];
UITabBarItem *updatesListItem = [[UITabBarItem alloc] initWithTitle:#"" image:iconSelected tag:0];
[updatesListItem setFinishedSelectedImage:iconSelected withFinishedUnselectedImage:iconNotSelected];
[navigationController setTabBarItem:updatesListItem];
where tabIconSelected.png and tabIconNotSelected.png both contain the title text for the tab.
I have written a brief article "Add some colour to your UITabBar icons" which explains how to use custom images with tabs.
Hope this helps.
Global adjustment in objective-C:
[[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, -4)];

How to customize UITabBar text labels?

I have customized a Tab Bar using the methods from Ray Wenderlich and iOS Blog but because I'm using a light color as background the text labels look awful and I want to change them to match my style. Is it possible and how?
If its not possible or if its too complex I would like to ask how to move the icon image down so its centered on the Tab Bar? This way I could "write" the labels on the .png files.
I'm planning on developing this app for iOS5+ so there is no worry about making it compatible with iOS4 (although if anyone finds a easy way of doing this I would really appreciate).
Here's what I got so far and here's what I want.
The code for my tabBarBackground:
UIImage* tabBarBackground = [UIImage imageNamed:#"tabbar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"selection-tab.png"]];
And for the Icons:
//Custom TabBar Items
UIImage *selectedImage = [UIImage imageNamed:#"home-icon-selected.png"];
UIImage *unselectedImage = [UIImage imageNamed:#"home-icon.png"];
UITabBar *tabBar = rootTabBarController.tabBar;
//Icons for the 1st Tab
UITabBarItem *item1 = [tabBar.items objectAtIndex:0];
[item1 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
You can do this by accessing the tab-bar item object of the tabbar for each view controller.
Check out some documentation here for more examples.
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/CombiningViewControllers/CombiningViewControllers.html
Use this to customize UITabBarItem text:
[[UITabBarItem appearance] setTitleTextAttributes: aNSDictionaryOfTextAttributes];
with a dictionary of attributes similar to those shown in the answer here.

Accessing/setting UIImageView from array

Having issues with this, I'm certain that I'm not grasping something.
Let's say I've created properties for three image views and linked those views to three views in Interface Builder. I synthesize them, link them all in IB (double checked). I've also created a property for a NSMutableArray and synthesized it.
When the view loads (viewdidload), I put all of the aforementioned image views into the array. For example:
[imageArray initWithObjects: img1, img2, img3, nil];
How do I directly access/set/change/whatever the views directly from the array?
For instance, if I wanted to change what img1 is displaying, I've been trying things like:
[imageArray objectAtIndex:0].image = [UIImage imageWithName:#"someimage.png"];
But it gives me an error. If I replace img1 in the array, will it display in IB?
QueueOverflow's solution is correct. In addition to that, u can also do like,
((UIImageView *)[imageArray objectAtIndex:0]).image = [UIImage imageNamed:#"someimage.png"];
Try this
UIImageView *selectedImageView = (UIImageView *)[imageArray objectAtIndex:0];
selectedImageView.image = [UIImage imageWithName:#"someimage.png"];

ImageView Scrolling along with the tableview

In my application,i created an imageview on the above the tabbar.I want to display some images here for displaying some adds there.
What my actual problem is...i added this on my table view and whenever i am scrolling the table view ,my imageview is also scrolling.Please help me in this
Thanks in advance.Here is my code
UIImageView *currentLocationImageView = [[UIImageView alloc] init];
NSURL *url1 = [NSURL URLWithString:#"http://imgsrv.995themountain.com/image/kqmt2/UserFiles/Image/SiteGraphics/MTNVideos360x80.jpg"];
UIImage *img1 = [UIImage imageWithData: [NSData dataWithContentsOfURL:url1]];
NSURL *url2 = [NSURL URLWithString:#"http://download.xbox.com/content/images/35f6c527-fb73-40d3-bcb9-bdea2680bc03/1033/banner.png"];
UIImage *img2 = [UIImage imageWithData: [NSData dataWithContentsOfURL:url2]];
NSArray *images = [NSArray arrayWithObjects:img1, img2, nil];
[currentLocationImageView setAnimationImages:images];
[currentLocationImageView setAnimationRepeatCount:0];
[currentLocationImageView setAnimationDuration:5.0];
currentLocationImageView.frame = CGRectMake(0.0, 340.0, 320.0, 30.0);
//self.tableView.tableFooterView = currentLocationImageView;
[currentLocationImageView startAnimating];
[self.view addSubview:currentLocationImageView];
Without seeing more code, it looks as though you may be using a UITableViewController instead of a UIViewController. The difference between the two is important in the case of your last line [self.view addSubview:currentLocationImageView]. What that does in a UIViewController is adds it to the view that would be containing the tableview and the imageview. However, in a UITableViewController the self.view property holds the tableview itself, therefor, it ads your image view as a subview of the tableview, subjecting it to the tableview's scrolling behaviour.
What you can do is change from using a UITableViewController (probably going to be trivial for your application, but may be less than trivial depending on why you opted to use it in the first place); and you'll also need to explicitly create the tableview, and add it to the backing view of the UIViewController subclass you're writing—akin to how you're adding the imageview above.
Hope this helps.