UITabBarItem finishedImageSelected placed offsetting the tab bar - objective-c

I'm trying to customize UITabBarItem but I'm having problems with the image's position.
The image size is 81px x 49px, the same height as the UITabBar. This is how I set the image:
// AppDelegate
BlocosController *blocos = [[[BlocosController alloc] initWithManagedObjectContext:moc] autorelease];
UINavigationController *navBlocos = [[[UINavigationController alloc] initWithRootViewController:blocos] autorelease];
tabBarController = [[UITabBarController alloc] initWithManagedObjectContext:moc];
tabBarController.viewControllers = [NSArray arrayWithObjects: navData, navBlocos, navBairro, navAtualizar, nil];
// ...
// BlocosController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tabBarItem = [[[UITabBarItem alloc] initWithTitle:TITLE image:nil tag:10] autorelease];
[[self tabBarItem] setFinishedSelectedImage:[UIImage imageNamed:#"tab_bar_blocos_selected"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab_bar_blocos_unselected"]];
}
return self;
}
I've search on google and found this tutorial, the code uses the same API as I use and works as expected. The code in this article behaves just like mine, but since their background is the same color there's a illusion of it been correctly placed.
Why is the finishedImageSelected and the unselected are placed unaligned with the tab tab? How to fix it?

I've found the correct way to accomplish this and made a post: felipecypriano.com/2012/02/27/….
Basically the problem is that finishedImage is the icon not the icon and the background, it's possible to use with the background by adjusting the imageInset property.

Related

Created a TabBar Controller programmatically - but where is the xib file?

I implemented a TabBar Controller, it is initalised like this
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
firstView = [[FirstView alloc]init];
secondView = [[SecondView alloc]init];
firstItem = [[UITabBarItem alloc]initWithTitle:#"First" image:nil tag:0];
secondItem = [[UITabBarItem alloc]initWithTitle:#"Second" image:nil tag:1];
firstView.tabBarItem = firstItem;
secondView.tabBarItem = secondItem;
NSArray *viewArray = [[NSArray alloc]initWithObjects:firstView, secondView, nil];
[self setViewControllers:viewArray];
}
return self;
}
In the AppDidFinishedLoading I just did this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
mainbarController = [[MainBarController alloc]initWithNibName:#"MainBarController" bundle:nil];
[self.window setRootViewController:mainbarController];
[self.window makeKeyAndVisible];
return YES;
}
My problem is, I want to place the tabBar somewhere else, not at the bottom, as it is when I run the project. But I can´t find a xib File to my code. The mainwindow.xib is just an empty window. I know it´s very basic but I´m confused about this.
thanks anyway

How to set title to tab bar item programatically?

I try this but it not works I can not see tab bar item..How can I solve this problem? Thanks for your help.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
myTabBarController = [[UITabBarController alloc] init];
tab1 = [[ZiyaretFormTab1 alloc] initWithNibName:#"ZiyaretFormTab1" bundle:nil];
tab2 = [[ZiyaretFormTab2 alloc] initWithNibName:#"ZiyaretFormTab2" bundle:nil];
tab3 = [[ZiyaretFormTab3 alloc] initWithNibName:#"ZiyaretFormTab3" bundle:nil];
tab4 = [[ZiyaretFormTab4 alloc] initWithNibName:#"ZiyaretFormTab4" bundle:nil];
tab5 = [[ZiyaretFormTab5 alloc] initWithNibName:#"ZiyaretFormTab5" bundle:nil];
myTabBarController.viewControllers = [NSArray arrayWithObjects: tab1, tab2,tab3,tab4,tab5,nil];
UITabBarItem *tabItem = [[[myTabBarController tabBar] items] objectAtIndex:1];
[tabItem setTitle:#"theTitle"];
[self.view addSubview:myTabBarController.view];
myTabBarController.selectedIndex=0;
}
In each of your view controllers that go into the tabs (ZiyaretFormTab1 to ZiyaretFormTab5), insert this code into initWithNib or viewDidLoad functions.
UITabBarItem * tabtitle = [[UITabBarItem alloc] initWithTitle: #"title"
image: nil //or your icon
tag: 0];
[self setTabBarItem: tabtitle];
Tab bar titles are controlled by their respective view controllers. The easiest way to set the title is from within the view controller for that tab, instead of trying to set it in the tab directly.
Each view controller has a title property which is used to set the title in tab bars and navigation bars. It also has a tabBarItem property which has it's own title property that you can set if you want to just set the tab and not affect navigation bars.
So in the viewDidLoad of your viewController, you could write:
self.tabBarItem.title = #"theTitle";
Put the tab settings to AppDelegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Put your name and icon information uniforms visit here:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Kulağını tersten tutmana gerek yok";
self.tabBarItem.image = [UIImage imageNamed:#"ringtab.png"];
}
return self;
}
Have you tried to set
[tab1 setTitle:#"Your Title"]
after allocating and initialising it.
UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex:0];
[item setTitle:#"fgfdgd"];

tab bar item title programatically not showing in objective c

I try create tab bar controller programatically, it works but I can not set title to ta bar items. I can not see title when I running my application. My code is here. Please help me, what is the problem?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
myTabBarController = [[UITabBarController alloc] init];
view2Controller = [[testView alloc] init];
view3Controller = [[testView2 alloc] init];
view4Controller = [[testView3 alloc] init];
view5Controller = [[testView4 alloc] init];
view6Controller = [[testView5 alloc] init];
myTabBarController.viewControllers = [NSArray arrayWithObjects: view2Controller, view3Controller,view4Controller,view5Controller,view6Controller,nil];
UITabBarItem *tabItem = [[[myTabBarController tabBar] items] objectAtIndex:1];
[tabItem setTitle:#"theTitle"];
[self.view addSubview:myTabBarController.view];
myTabBarController.selectedIndex=0;
}
Set the title of ur viewcontroller in side viewcontroller's viewwillappear/disappear and it will be displayed in ur tabbaritem.
You can set the title of UIViewController, which reflects when it is pushed in UINavigationController or a UITabBarController. But you should set the title before putting it inside any of them.
init is generally a good place to set the title.
- (id)init {
// ... other code including check for self
self.title = #"My Title";
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// self.title = #"My Title";
}
self.title = #"My Title";
return self;
}
Use above code in every subviews of tabbar controllers.
This may be help.
use this code
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title=#"FREE BOOKS";
self.tabBarItem.image = [UIImage imageNamed:#"first"];
}
return self;
}
Here is the tutorial from the scratch which should help you to achieve the exact functionality you want to achieve. Download the source code and run it directly and if title are correct the you can follow the tutorial to get the proper understanding of the flow of UITabBarController.
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
self.tabBar.items?[0].title = "Title"
}
}
OR
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBar.items?[0].title = "Title"
}

Display title for view in back button but not in navigation bar

I would like to set the title of my view to "Main Menu", however, I do not want the title to appear in the navigation toolbar. The reason I want to set the title is so the back button has the title "Main Menu". Is there a way to do this? Here is the relevant code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Main Menu";
// Custom initialization
}
return self;
}
To set the title of back button item, you should write in viewDidLoad method as follows:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Main Menu"
style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButtonItem;
[backButtonItem release];
}
I would set the back button for this one. You need to set the value of the back button in the Main Menu controller before it pushes the next UIViewController.
For an explanation of why check out my previous answer and the docs for View controller programming guide
Set the title right before you push the next view controller:
[[self navigationItem] setTitle:#"Main Menu"];
[navigationController pushViewController:nextVC animated:YES];
and set it to a blank string (or nil) in viewWillAppear:
[[self navigationItem] setTitle:#""];

UINavigationController with TTNavigator

I am using a UINavigationController in the appdelegate as a basis for all views and I am using the TTNavgitor for antother flow starting with a TTTableViewController.
When I use the TTNavigator for some reason I have 2 problems:
After I choose one of the rows there is no back button in the viewcontroller loaded
The transition is not animated when selecting a row in the table
I am using the basis of the Three20 examples:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.title = #"Settings";
self.navigationItem.backBarButtonItem =
[[[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered
target:nil action:nil] autorelease];
self.tableViewStyle = UITableViewStyleGrouped;
TTNavigator* navigator = [TTNavigator navigator];
navigator.supportsShakeToReload = YES;
navigator.persistenceMode = TTNavigatorPersistenceModeNone;
navigator.window = self.view.window;
TTURLMap* map = navigator.URLMap;
[map from:#"*" toViewController:[TTWebController class]];
[map from:#"tt://login" toViewController:[LoginController class]];
}
return self;
}
Does some has an idea why do I have these problems? and how can I solve this fast without rewriting all of the code?