1

I haven't embedd navigation controller with my view controller. Now i'm using a code to go back to home screen to with the use of back button but there nothing appears, i don't know why. I don't want to use navigation controller in my vc. This is the code,

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"car_marker.png"]
                                                               style:UIBarButtonItemStylePlain
                                                              target:self.navigationController
                                                              action:@selector(popViewControllerAnimated:)];
self.navigationItem.leftBarButtonItem = backButton;

It should look like this only, enter image description here

Hamza Imran
  • 189
  • 1
  • 18

4 Answers4

1

You will need to change your approach because you cannot pop without a navigation controller.

You should embed your viewcontroller into navigation controller and then you can hide with [[self navigationController] setNavigationBarHidden:YES animated:YES]; and then you can add any kind of custom button to perform pop action.

For adding a custom back button to your view

- (void)addCustomBackButtonToView
{
    UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(20, 20, 44.0f, 30.0f)];
    [backButton setImage:[UIImage imageNamed:@"back.png"]  forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(popVC) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
}

- (void) popVC{
  [self.navigationController popViewControllerAnimated:YES];
}
Paras Gorasiya
  • 1,295
  • 2
  • 13
  • 33
  • How can i change the nav bar color to transparent so that my backgroung image looks like the screenshot i had added above? @Paras Gorasiya – Hamza Imran Aug 07 '17 at 09:32
  • You just need to hide navigation bar instead of making it transparent. Please follow my pasted code and you will get what you want but in order to have perform pop action you will need to add a custom back button at top left position. – Paras Gorasiya Aug 07 '17 at 09:34
  • how can we use custom popup back button according to ur code? @Paras Gorasiya – Hamza Imran Aug 07 '17 at 09:43
  • Its not working, its just disappearing the nav bar but not showing an back button. @Paras Gorasiya – Hamza Imran Aug 07 '17 at 11:14
  • Please show us how are you adding the custom back button in your code. – Paras Gorasiya Aug 07 '17 at 13:58
  • So you will also have to add an image of the back button in your bundle otherwise you will see nothing. – Paras Gorasiya Aug 08 '17 at 02:25
  • Its not working again, i have added all ur code in one vc but not working and i have image of back button also. @Paras Gorasiya – Hamza Imran Aug 08 '17 at 05:33
  • - (void)viewDidLoad { [super viewDidLoad]; [[self navigationController] setNavigationBarHidden:YES animated:YES]; UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(20, 20, 44.0f, 30.0f)]; [backButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal]; [backButton addTarget:self action:@selector(popVC) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:backButton]; } – Hamza Imran Aug 08 '17 at 09:30
  • How are you adding all the other objects into the same view? I guess you are adding those after adding back button. So your back button is hidden. You should add [self.view bringSubviewToFront:backButton] after adding all the other objects. – Paras Gorasiya Aug 08 '17 at 09:41
  • @HamzaImran No problem, it would be great if you accept the answer and upvote. Thanks – Paras Gorasiya Aug 08 '17 at 09:45
0

You can hide your navigation controller by below code self.navigationController.navigationBar.isHidden = true

and use swipegesture option to move back, even though you have to use navigation controller,@iPeter has a valid point.

0

1) Add two ViewControllers make first as initial controller(entry point).

2) Add view on both ViewController with size you wish to have.

3) Add buttons on both views as subviews.

4) create segue from firstButton ->(use show) -> second controller.

5) From second ->(use show) -> first controller.

see the image below-:

enter image description here

This is not preferred way of doing pop/push. You must use Navigation Controller(that works like a stack for controllers you push) that will provide you default Back button or you can create custom .

enter image description here

Tushar Sharma
  • 2,839
  • 1
  • 16
  • 38
0

You can not push or pop view controllers as you are not using the Navigation controller. Try using Present & dismiss controller which can called from any controller Instace.

Step 1 : Present to " Controller which is shown with no navigation bar but back button"

[self presentViewController:@"<ViewControllerToBePresented>" animated:NO completion:nil];

Step 2: add backBatton in ViewDidLoad

-(void)addBack{

UIButton *backBtn = [[UIButton alloc] initWithFrame: CGRectMake(20.0f, 20.0f, 50.0f, 30.0f)];
[backBtn setTitle:@"back" forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBtn];

}

Step 3: Dismisscontroller to go back to previous scene

-(void) dismissViewController{

[self dismissViewControllerAnimated:NO completion:nil];

}

Chaitra
  • 16
  • 4