i am new on iOS and i wanna add a navigation bar on my view controller with 2 buttons back on left and subscribe on right. ive got no clue how to do it..till now i have just added a nav bar from interface builder, created a (strong)refrnce for it in .h file and did following coding.
navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, 1026, 50)];
[navBar setTintColor:[UIColor clearColor]];
[navBar setBackgroundColor:[UIColor redColor]];
[navBar setDelegate:self];
[self.view addSubview:navBar];
UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:#"subscribe" style:UIBarButtonItemStyleBordered target:self action:#selector(editBotton)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.tintColor =[UIColor colorWithWhite:0.305f alpha:0.0f];
self.navigationItem.rightBarButtonItem = bi1;
but nothings happeing.. please help
You can add in AppDelegate,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone"
bundle: nil];
SampleViewController *mainViewController = (SampleViewController*)[mainStoryboard
instantiateViewControllerWithIdentifier: #"SampleViewController"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:navigationController];
[self.window setBackgroundColor:[UIColor whiteColor]];
[self.window makeKeyAndVisible];
return YES;
}
I think you have to learn the basic concepts about UINavigationController. You can learn from the below tutorials:
http://simplecode.me/2011/09/04/an-introduction-to-uinavigationcontroller/
http://www.ralfebert.de/archive/ios/tutorial_iosdev/navigationcontroller/
http://bharanijayasuri.wordpress.com/2012/12/19/simple-uinavigationcontroller-tutorial-2/
Related
I implemented a UITabBarController with 4 items.
I set the initial ViewController from AppDelegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
HomeTabBarViewController* homeVC = [[HomeTabBarViewController alloc] init];
[self.window setRootViewController:homeVC];
[self.window makeKeyAndVisible];
return YES;
}
Here is HomeTabBarViewController ViewDidLoad Code:
UIViewController *view1 = [[UIViewController alloc] init];
[view1.view setBackgroundColor:[UIColor redColor]];
UIViewController *view2 = [[UIViewController alloc] init];
UINavigationController * centerNav=[[UINavigationController alloc]initWithRootViewController:view1];
HomePagingViewController * view3=(HomePagingViewController*)[storyboard instantiateViewControllerWithIdentifier:#"HomePagingViewController"];
UINavigationController * centerNav3=[[UINavigationController alloc]initWithRootViewController:view3];
UIViewController *view4 = [[UIViewController alloc] init];
UINavigationController * centerNav4=[[UINavigationController alloc]initWithRootViewController:view4];
[view2.view setBackgroundColor:[UIColor greenColor]];
[view3.view setBackgroundColor:[UIColor purpleColor]];
[view4.view setBackgroundColor:[UIColor grayColor]];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
[tabViewControllers addObject:centerNav];
[tabViewControllers addObject:view2];
[tabViewControllers addObject:centerNav3];
[tabViewControllers addObject:centerNav4];
[self setViewControllers:tabViewControllers];
view1.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Home"
image:[UIImage imageNamed:#"home_tab_item.png"] selectedImage:[UIImage imageNamed:#"home_tab_item_active.png"]] ;
view2.tabBarItem =
[[UITabBarItem alloc] initWithTitle:#"Bookmark"
image:[UIImage imageNamed:#"bookmark_tab_item.png"] selectedImage:[UIImage imageNamed:#"bookmark_tab_item_active.png"]];
view3.tabBarItem =
[[UITabBarItem alloc] initWithTitle:#"Paper"
image:[UIImage imageNamed:#"paper_tab_item.png"] selectedImage:[UIImage imageNamed:#"paper_tab_item_active.png"]];
view4.tabBarItem =
[[UITabBarItem alloc] initWithTitle:#"More"
image:[UIImage imageNamed:#"menu_tab_item.png"] selectedImage:[UIImage imageNamed:#"menu_tab_item_active.png"]];
In HomePagingViewController ViewDidLoad I setUp a Navigation as in code
-(void)navigationSetUp{
self.navigationController.navigationBar.translucent=NO;
UIBarButtonItem * settingBtn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"settings.png"] style:UIBarButtonItemStylePlain target:self action:#selector(menuHandler:)];
UIBarButtonItem * searchBtn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"settings.png"] style:UIBarButtonItemStylePlain target:self action:#selector(menuHandler:)];
self.navigationItem.leftBarButtonItem = settingBtn;
self.navigationItem.rightBarButtonItem = searchBtn;
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"ie_New_logo.png"]];
}
After this when I changed the tab for HomePagingViewController the navigation right button not placed correctly there is no margin from right.
Refer Image :
Please Help.
I resolved the issue , there is no issue with the code . Actually for Epaper we using third party SDK So that this problem occur.I Removed SDK & the code is working fine now.
you should think UIBarButtonSystemItemFixedSpace effect.
eg:
UIBarButtonItem * space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
--
Why use a system navigation bar ?Custom navigation bar is nice!like:https://github.com/chenliangloveyou/EasyNavigation
I am having a problem with my tab bar placed above my view
I am using iOS 8 and Autolayout
I have my main view which has a button that opens another view that has the tab bar:
-(IBAction)btnOpen:(id)sender
{
Home* homevc = [[Home alloc] initWithNibName:#"Home_iPhone" bundle:nil];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:homevc];
[nc.tabBarItem setTitle:#"Home"];
[nc.tabBarItem setImage:[UIImage imageNamed:#"home_icon.png"]];
Search *searchvc = [[Search alloc] initWithNibName:#"Search_iPhone" bundle:nil];
[searchvc.tabBarItem setTitle:#"Search"];
[searchvc.tabBarItem setImage:[UIImage imageNamed:#"search_icon.png"]];
UITabBarController *tabController = [[UITabBarController alloc] init];
[tabController setViewControllers:[NSArray arrayWithObjects:nc,searchvc, nil]];
UIColor * color = [UIColor colorWithRed:191/255.0f green:50/255.0f blue:46/255.0f alpha:1.0f];
[[UITabBar appearance] setTintColor:color];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color, NSForegroundColorAttributeName, nil]
forState:UIControlStateSelected];
[self presentViewController:tabController animated:YES completion:nil];
}
How can I place the Home view above the tab bar?
I've been using a standard TabBarController and top NavigationBar within my app. The tabBar loads in the appDelegate, initializes the navigationBar to use an image instead of center title, and this exists for all four tabBar views. Occasionally I'll push a new view onto the view controllers and that works well enough.
What I'd like to do is be able to change the NavigationBar for each of the 4 tabBar view controllers, when they are opened. I'm having trouble doing this. My initial navigationBar has an image, loaded in the appDelegate. My questions from here are:
Should each viewController generally create a new navigationBar from scratch, per its needs, in the respective viewWillAppear method?
What navigationController and navigationBar should be editted-- always the appDelegate's navigationController, the tabBarController.navigationController, or simply self.navigationController in each view? (generally, most editing most of these is not working for me, causes no changes to occur)
If I override the standard title (which is usually the tabBar's current view's title) with the imageView, and want another tabBar view to use the standard title, how should I remove the titleView image, and reset back to text? And viceVersa, dependent on view? This is what I'm really trying to accomplish unsuccessfully.
I guess I'm looking for standard practice in managing the navigationBar per view, and changing it per tabBarItem.
// in appDelegate
- (void)initNav {
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate = self;
self.tabBarController.navigationController.view.backgroundColor = [UIColor whiteColor];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:self.firstViewController,
self.secondViewController,
self.thirdViewController,
self.forthViewController,
nil];
[self.window addSubview:self.tabBarController.view];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.tabBarController];
self.navigationController.navigationBarHidden = NO;
// customize background color of nav bar to "blue"ish color
self.navigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:#"00A3E1"];
self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:#"00A3E1"];
self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:#"00A3E1"];
[self createNavs];
}
// also in appDelegate
- (void)createNavs {
// white text when present
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],
UITextAttributeTextColor,
[UIColor clearColor],
UITextAttributeTextShadowColor, nil];
[[UIBarButtonItem appearance] setTitleTextAttributes: attributes
forState: UIControlStateNormal];
AppDelegate *delegateRef = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[self.navigationController.navigationBar setTranslucent:NO];
// setup left button (currently unused -- no left button)
/*
/*
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"glyphicons_049_star.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(starClick:) forControlEvents:UIControlEventTouchDown];
[button setFrame:CGRectMake(0, 0, 25, 25)];
self.navigationController.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
*/
// setup logo in center
self.tabBarController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"synced_top_logo.png"]];
// setup right button (currently unused -- no right button)
/*
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] init];
rightButton.title = #"edit";
[rightButton setTarget:self ];
[rightButton setAction:#selector(editClick:)];
self.navigationController.navigationBar.topItem.rightBarButtonItem = rightButton;
*/
[[self navigationController] setNavigationBarHidden:NO animated:YES];
[[delegateRef navigationController] setNavigationBarHidden:NO animated:YES];
}
EDIT:
This is a simplified version of the solution posted below that was helpful, and allowed the customization that was needed. Each view controller customizations its navigationbar independently from this. It was probably how it should have been done from the start.
tabBarController.viewControllers = [NSArray arrayWithObjects:[[UINavigationController alloc] initWithRootViewController:self.firstViewController],
[[UINavigationController alloc] initWithRootViewController:self.secondViewController],
[[UINavigationController alloc] initWithRootViewController:self.thirdViewController],
[[UINavigationController alloc] initWithRootViewController:self.forthViewController],
nil];
This maybe what you are needing:
- (void)initTabbar {
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate = self;
/*
You want each of your UIViewControllers to be wrapped in a UINavigationController. Then put each of those UINavigationControllers in a UITabBarController
*/
//You don't need to hang on to this becuase the proceeding UINavigationController will handle it
FirstViewController *firstViewController = [[FirstViewController alloc] ...];
//You'll need to declare this in your header
self.firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
//Second view allocation
SecondViewController *secondViewController = [[SecondViewController alloc] ...];
self.secondNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
//Third view allocation
ThirdViewController *thirdViewController = [[ThirdViewController alloc] ...];
self.thirdNavigationController = [[UINavigationController alloc] initWithRootViewController:thirdViewController];
//Now you add each of the UINavigationControllers (which is a subclass of UIViewController) to the UITabBarController.
self.tabBarController.viewControllers = [NSArray arrayWithObjects:self.firstNavigationController,
self.secondNavigationController,
self.thirdNavigationController,
nil];
[self.window addSubview:self.tabBarController.view];
[self createNavs];
}
//This is more of a 'formatNavs' now
- (void)createNavs {
//Now you can customize each of the UINavigationController's UINavigationBars seperatly
self.firstNavigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:#"00A3E1"];
self.firstNavigationController.navigationBar.tintColor = [UIColor colorWithHexString:#"00A3E1"];
self.firstNavigationController.navigationBar.barTintColor = [UIColor colorWithHexString:#"00A3E1"];
self.secondNavigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:#"...."];
self.secondNavigationController.navigationBar.tintColor = [UIColor colorWithHexString:#"...."];
self.secondNavigationController.navigationBar.barTintColor = [UIColor colorWithHexString:#"...."];
self.thirdNavigationController.navigationBar.backgroundColor = [UIColor colorWithHexString:#"...."];
self.thirdNavigationController.navigationBar.tintColor = [UIColor colorWithHexString:#"...."];
self.thirdNavigationController.navigationBar.barTintColor = [UIColor colorWithHexString:#"...."];
}
I am trying to add UIBarButtonItem to navigationItem but it does not gets displayed
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
lbl =[[UILabel alloc] initWithFrame:CGRectMake(20, 100, 200, 40)];
lbl.text=#"Hello,World";
lbl.textAlignment= NSTextAlignmentCenter;
[self.view addSubview:lbl];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"house.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(goHome:)];
self.navigationItem.rightBarButtonItem = rightButton;
}
app delegate code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ViewController *controller = [[ViewController alloc] init];
[controller.navigationItem setTitle:#"Main View"];
UINavigationController *navcontroller =[[UINavigationController alloc] initWithRootViewController:controller];
self.window.rootViewController=navcontroller;
self.window.backgroundColor = [UIColor whiteColor];
// Override point for customization after application launch.
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:self.viewController];
// CHANGE COLOR TO BLACK
nav.navigationBar.tintColor = [UIColor blackColor];
// ADDING IMAGE TO NAVIGATION BAR
UIImage *image = [UIImage imageNamed:#"logo_36.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[nav.navigationBar.topItem setTitleView:imageView];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
I have tried this code in appdelegate class because i want it throughout the whole project, it works for displaying image, but it shows in centre also i need text which i couldn't display.
Please could someone suggest how can i display both image and text on left side of navigation bar in iOS
Why you are setting imageview to navigation topItem titleview
Replace
[nav.navigationBar.topItem setTitleView:imageView];
with nav.navigationbar.topItem.leftBarButtonItem:imageview
and set `[nav.navigationBar.topItem setTitle:#"text you want"];
UIImage* image3 = [UIImage imageNamed:#"mail-48_24.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(sendmail)
forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.leftBarButtonItem=mailbutton;
[someButton release];