iOS back button has no padding - ios

When I push a viewcontroller on my navigation stack, the back button appears to be against the left side of the screen. How can I get the regular padding of the back button?
Here is how I present the view controller:
- (void)goToCollection:(UIButton *)btn {
Card *colCard = (Card *)btn.userData;
WViewController *vc = [WViewController new];
NSString *colID = [[colCard.href componentsSeparatedByString:#"/"] lastObject];
vc.collectionID = colID;
[self.navigationController pushViewController:vc animated:YES];
}
Here's my view setup in didFinishLaunchingWithOptions :
//Profile View
ProfileViewController *pv = [ProfileViewController new];
UINavigationController *profNav = [[UINavigationController alloc] initWithRootViewController:pv];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:profNav];
[self.window setTintColor:[UIColor Primary]];
[self.window makeKeyAndVisible];

As per apple documentation, you can not edit or modify back button in any case. Only allowed operation for back button is show and hide .
So if you want something to do with back button, you need to hide it and create a customise left bar button item as back button.
self.navigationItem.hidesBackButton = YES; //hides back button
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; // creates a new button
[myButton setImage:[UIImage backButtonImage]; // sets image for new button
[myButton setContentEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];//content edgeInset to provide required padding
[myButton addTarget:self action:#selector(backToHOmePage) forControlEvents:UIControlEventTouchUpInside];//adding target for new button
UIBarButtonItem *customBackBtn = [[UIBarButtonItem alloc] initWithCustomView:myButton]; //custom bar button item
self.navigationItem.leftBarButtonItem = customBackBtn; //assigning to left bar button item
-(void)backToHOmePage
{
[self.navigationController popViewControllerAnimated:YES];
} // method to be triggered on tapping above button

Put this in your AppDelegate
[[UIBarButtonItem appearance] setBackButtonBackgroundVerticalPositionAdjustment:-3 forBarMetrics:UIBarMetricsDefault];

Related

MVYSideMenu using Navigation Controller in all view controllers iOS

I am using this Cocoa Control Side Menu link.
In this Menu, when you start the app you have a Navigation Bar for the first screen, but when you open the menu and press any element of TableView, the ViewController change without Navigation Bar. I want to have a Navigation Bar for each of the elements of the TableView Menu and a left button on the Navigation bar that could open and close the Menu.
I start the application with this code:
// appDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
YPMenuViewController *menuVC = [[YPMenuViewController alloc] init];
YPProfileViewController *contentVC = [[YPProfileViewController alloc] init];
UINavigationController *contentNavigationController = [[UINavigationController alloc] initWithRootViewController:contentVC];
YPSideMenuOptions *options = [[YPSideMenuOptions alloc] init];
YPSideMenuController *sideMenuController = [[YPSideMenuController alloc] initWithMenuViewController:menuVC
contentViewController:contentNavigationController
options:options];
self.window.rootViewController = sideMenuController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
After this, the app starts with Navigation bar.
But then, in the TableView If Select the view controllers like this, I have the Navigation Bar but without button to open an close
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 0:
{
YPProfileViewController *contentVC = [[YPProfileViewController alloc] init];
UINavigationController *contentNavigationController = [[UINavigationController alloc] initWithRootViewController:contentVC];
UIButton *iwantView = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
[iwantView setBackgroundColor:[UIColor blackColor]];
[iwantView addTarget:self action:#selector(openMenu) forControlEvents:UIControlEventTouchUpInside];
contentNavigationController.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:iwantView];
[[self sideMenuController] changeContentViewController:contentNavigationController closeMenu:YES];
break;
}
case 1:
{
YPProjectListViewController *contentVC = [[YPProjectListViewController alloc] init];
UINavigationController *contentNavigationController = [[UINavigationController alloc] initWithRootViewController:contentVC];
[[self sideMenuController] changeContentViewController:contentNavigationController closeMenu:YES];
break;
}
default:{
YPProfileViewController *contentVC = [[YPProfileViewController alloc] init];
[[self sideMenuController] changeContentViewController:contentVC closeMenu:YES];
break;
}
}
}
You are adding the button before the view controller is loaded, you should add it after loading the view in your content view controller.
#implementation YPProfileViewController
---------------
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIButton *iwantView = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
[iwantView setBackgroundColor:[UIColor blackColor]];
[iwantView addTarget:self action:#selector(openMenu) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:iwantView];
}
Anyway I just added a method to add a menu button on the navigation bar more easily.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self addLeftMenuButtonWithImage:[UIImage imageNamed:#"menu_icon"]];
}

Issue with adding buttons to UINavigationController

So this is how I make the navbar:
- (void)viewDidLoad
{
[super viewDidLoad];
UINavigationController *navBar = [[UINavigationController alloc] init];
[navBar willMoveToParentViewController:self];
navBar.view.frame = CGRectMake(0, 0, 320, 44);
[self.view addSubview:navBar.view];
[self addChildViewController:navBar];
[navBar didMoveToParentViewController:self];
...
And everywhere I have read says that this is how you add buttons:
UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithTitle:#"test" style:UIBarButtonItemStyleBordered target:self action:#selector(print_message:)];
self.navigationItem.rightBarButtonItem = button;
[button release];
But the button does not show on the navbar. What is wrong with this code?
Unless you're building a custom container view controller (which is a relatively rare thing to do), you should not be building a UINavigationController inside your content controller's -viewDidLoad. While it will provide you a navigation bar, your view controller parent-child relationship will be backwards: your content controller will contain the navigation controller, rather than the other way around.
Instead, you need to create the navigation controller earlier in your app's startup process - maybe in your application delegate, or in your main storyboard if you're using one. Make sure that the new navigation controller has your content controller as its root controller (usually by way of -initWithRootViewController:). Then your self.navigationItem configuration will work properly.
You should create your navigationbar probably differently:
In your xxxAppDelegate.m edit this method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//This is the ViewController of the view you want to be the root
xxxViewController *tvc = [[xxxViewController alloc]init];
//Now you have to initialize a UINavigationController and set its RootViewController
UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:tvc];
//Now set the RootViewController to the NavigationViewController
[[self window]setRootViewController:nvc];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
So now you have a proper NavigationController. If you do this in the viewDidLoad method, the NavigationController will be made each time you reload your view.
Now in your xxxViewController.m edit your init method:
- (id)init
{
...
if (self) {
//Create a UINavigationItem
UINavigationItem *n = [self navigationItem];
//Create a new bar button item
UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithTitle:#"test" style:UIBarButtonItemStyleBordered target:self action:#selector(print_message:)];
[[self navigationItem]setRightBarButtonItem:button];
}
return self;
}
This should now display a proper NavigationBar with a UIBarButtonItem.

How do i make the more button go always to more view?

I'm developing an app for ios with a Tab Bar. I have more than 5 buttons on the bar, so on the iphone i have the button more.
Now, suppose i have this buttons: Button1 Button2 Button3 Button4 More (and inside More) Button5 Button6.
If i click More and then Button5 i go in the view relative to Button5. Then i click Button2 (that is not in the More) and i go in the view relative to Button2.
So far so good.
Now if i click More i go not to More Tab but back to the view relative to Button5.
How do i make the more button go always to the more view?
You don't need to add more button. Just set the view controllers to the UITabBarController
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated
and it will automatically create a more button if you have more than 5 view controllers! i.e. the count of NSArray is greater than 5.
Another way you could do is, whenever the user presses more, the first button gets removed and other buttons gets added.
Basically you can create an array and keep all the buttons inside it. And then based on the button pressed you can navigate to that particular view.
For Ex:
Initially you have: Button1 Button2 Button3 Button4 Next
After Clicking Next: Prev Button3 Button4 Button5 Button6
I used this code in my app delegate.m to solve the problem
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
UITabBarController* tabBarController2 = (UITabBarController*)self.window.rootViewController;
if (tabBarController2.selectedIndex < 4) {
[tabBarController2.moreNavigationController popViewControllerAnimated:NO];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[UIViewController alloc] init];
UIViewController *viewController2 = [[UIViewController alloc] init];
UIViewController *viewController3 = [[UIViewController alloc] init];
UIViewController *viewController4 = [[UIViewController alloc] init];
UIViewController *viewController5 = [[UIViewController alloc] init];
UIViewController *viewController6 = [[UIViewController alloc] init];
UIViewController *viewController7 = [[UIViewController alloc] init];
UIViewController *viewController8 = [[UIViewController alloc] init];
UIViewController *viewController9 = [[UIViewController alloc] init];
[viewController1.view setBackgroundColor:[UIColor whiteColor]];
[viewController2.view setBackgroundColor:[UIColor redColor]];
[viewController3.view setBackgroundColor:[UIColor greenColor]];
[viewController4.view setBackgroundColor:[UIColor grayColor]];
[viewController5.view setBackgroundColor:[UIColor blueColor]];
[viewController6.view setBackgroundColor:[UIColor yellowColor]];
[viewController7.view setBackgroundColor:[UIColor brownColor]];
[viewController8.view setBackgroundColor:[UIColor magentaColor]];
[viewController9.view setBackgroundColor:[UIColor purpleColor]];
[viewController1 setTitle:#"one"];
[viewController2 setTitle:#"two"];
[viewController3 setTitle:#"three"];
[viewController4 setTitle:#"four"];
[viewController5 setTitle:#"five"];
[viewController6 setTitle:#"six"];
[viewController7 setTitle:#"seven"];
[viewController8 setTitle:#"eight"];
[viewController9 setTitle:#"nine"];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2, viewController3, viewController4, viewController5, viewController6, viewController7, viewController8, viewController9];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
I have added a sample AppDelegate code which I tried and its working absolutely fine for me. Let me know what problem your having in this.

Switch view to another view on storyboard

I am new in iOS programming.
What I am trying to do is:
I have some views in a storyboard and I'd like to switch between the views programatically. For example, when a button is clicked, I call a method and I want to change views here (I can call the method successfully). The buttons are also created programatically in different positions.
I have searched and I think it happens with NavigationController. I have a navigation controller which I created like so: menu Editor -> Embed In -> NavigationController. How could I do this using this NavigationController?
#Madhu and #Dilip ,I found a solution with xib filed class
icerik *secondViewController = [[icerik alloc] initWithNibName:#"icerik" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
navigationController.topViewController.title = #"SecondViewController";
//[self presentModalViewController:navigationController animated:YES];
if([self respondsToSelector:#selector(presentViewController:animated:completion:)])
[self presentViewController:navigationController animated:YES completion:^{/* done */}];
else if([self respondsToSelector:#selector(presentViewController:animated:)])
[self presentModalViewController:navigationController animated:YES];
I have a class with xib file named icerik, I solved it like this. It is opening but when I want to turn back, What can I do it ?
You can create btn using this code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(aMethod) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
and for going to another vc use this code,if you want navigation bar:
-(void)aMethod
{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:SecondViewController];
navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
navigationController.topViewController.title = #"SecondViewController";
[self presentModalViewController:navigationController animated:YES];
}
Else use this code:
-(void)aMethod
{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self presentModalViewController:secondViewController animated:YES];
}
And for come back to frist vc fromm second vc add this code in second vc.
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(backAction:)];
self.navigationItem.leftBarButtonItem = closeButton;
}
- (void)backAction:(id)sender {
[self dismissModalViewControllerAnimated:NO];
}
If your new to Objective-c first go with Views/ViewControllers. i.e. use addSubView property of UIView
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 250.0, kCCCellHeaderHeight)];
[subView setBackgroundColor:[UIColor redcolor]];
[self.view addSubview:subView];
If your little known of UINavigationCOntroller Use pushViewController
CCFilteredVideosController *filteredController = [[CCFilteredVideosController alloc] initWithNibName:#"CCFilteredVideosController" bundle:nil];
[self.navigationController pushViewController:filteredController animated:YES];
[filteredController release];

UINavigation bar back button not responding

I have a cameraview from which I'm pushing another view of with TableViewController. I can push the second view from my main view by using navbar controller. But the back button from the second view which is table view does not respond. I can't figure out why.
Thanks in advance.
#WrightCS Thank you for your immediate response.
In my app delegate.
ViewController *vc= [[ViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController :navController];
[self.window makeKeyAndVisible];
return YES;
Right now I'm using a temporary button to push the view.
-(IBAction)testButtonPressed {
TableView *tableVC = [[TableView alloc] initWithStyle:UITableViewStylePlain];
[self.navigationController pushViewController:tableVC animated:YES];
}
There are a few reasons your back button may not work:
You are using a custom UIButton and the frame/bounds are not correct you can use [yourButtonName sizeToFit]
to size it properly.
You are adding a button programmatically and not adding a click event:
[yourButtonName addTarget:self action:#selector(yourButtonNameTapped:) forControlEvents: UIControlEventTouchUpInside];
Don't forget your event.
...
-(void)yourButtonNamedTapped:(id)sender
{
...
}
docs: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIControl_Class/Reference/Reference.html
If you are using a UINavigationController, back will automatically show up when you use [self.navigationController pushViewController:viewControllerName];
-(IBAction)testButtonPressed {
TableView *tableVC = [[TableView alloc] initWithStyle:UITableViewStylePlain];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
[self.navigationController pushViewController:tableVC animated:YES];
}
I think it will be helpful to you.
- (void)viewDidLoad
{
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonDidClicked:)];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
}
-(void)backButtonDidClicked :(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
It should work for you!!

Resources