Modal segue, navigation bar disappears

2020-01-28 03:56发布

I'm using Xcode 4.6.1 to code on Objective-C. I want to know how can I keep the navigation bar shown when I create a modal segue between 2 View controllers, because I'm doing the segue in the storyboard and when I run the application the navigation bar of the second view controller disappears, and I have a done button on that bar but I can't see it.

11条回答
我命由我不由天
2楼-- · 2020-01-28 04:14

Just add another Navigation Controller to your modal view controller. Follow the steps

  1. Select the Modal View Controller
  2. Go to Editor menu
  3. Select Embed In
  4. Select Navigation Controller

Run the application. Now it should work perfectly.

Hope this solves your problem.

查看更多
神经病院院长
3楼-- · 2020-01-28 04:19

Modal segues take over the whole screen, so any navigation bars, tool bars, or tab bars that are in the presenting controller will be covered up. If you want a navigation bar on this modal controller, you'll need to add one specifically to it, and add any buttons you want to that new navigation bar (or tool bar). If you don't want to do this, then don't present it modally, do a push to it.

查看更多
可以哭但决不认输i
4楼-- · 2020-01-28 04:19

You can add a toolbar programatically by doing the following in -(void)viewDidLoad

NSInteger tbHeight = 50;
tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - tbHeight), self.view.frame.size.width, tbHeight)];
tb.translucent = YES;
emailButton = [[UIBarButtonItem alloc] initWithTitle:@"Email Results" style:UIBarButtonItemStyleBordered target:tvController action:@selector(actionSheet:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(doneButtonPressed:)];

NSArray *barButton  =   [[NSArray alloc] initWithObjects:emailButton,flexibleSpace,doneButton,nil];
[tb setItems:barButton];


[self.view addSubview:tb];

barButton = nil;

You will then have to create an IBAction for pressing the done button and it is done just like this:

-(IBAction)doneButtonPressed:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}

That should give you what you want with your modal view controller.

查看更多
孤傲高冷的网名
5楼-- · 2020-01-28 04:23

Here is my SHORTED version of "Volodymyr Nazarkevych" in Swift 4.2

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    switch(segue.identifier ?? "") {

    case "segueIdentifier": // name of your segue
        let navigation: UINavigationController = segue.destination as! UINavigationController

        //here you create new name of your destination ViewController and assign his name to let constant above
        guard let myNewViewController = navigation.viewControllers[0] as? DestinationViewController
       //DestinationViewController is your 2-ViewController where you go to from first one.

       else {
            fatalError("Unexpected destination: \(segue.destination)")
        }
       // TO DO SOMETHING HERE

   default:
        fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))")
    }
}

The code of "Volodymyr Nazarkevych" works perfectly, but only when your segue is from 1-ViewController to NavigationController of 2-ViewController, not directly to 2-ViewController. THANKS A LOT VOVA!.

Also in switch cases after destination block code you can do different stuff, for example to get some information or file from second ViewController.

查看更多
淡お忘
6楼-- · 2020-01-28 04:24

That is probably because you don't have a UINavigationController in your modal. You should use one (or just add a navigation bar to your ViewController in the Storyboard), and present that modally.

查看更多
登录 后发表回答