The following code should popover a view and the view size should be customized as I customized it by code, but when I run it, I see it's not customized.
Can anyone help?
- (IBAction)barButtonLeft:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
_myViewController = [storyboard instantiateViewControllerWithIdentifier:#"Sort"];
_myViewController.modalPresentationStyle = UIModalPresentationPopover;
self.myViewController.modalPresentationStyle = UIModalPresentationFormSheet;
self.myViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
self.myViewController.preferredContentSize = CGSizeMake(139, 70);
[self presentViewController:_myViewController animated:YES completion:nil];
UIPopoverPresentationController *popController = [_myViewController popoverPresentationController];
}
You're using _myViewController and self.myViewController together here, which is a little confusing but I assume you have a property, myViewController, backed by an instance variable, _myViewController? I'm therefore assuming both of these things are actually pointing to the same instance.
For clarity I'd recommend sticking with one or the other, probably property access.
If they are both pointing to the same instance, then what the code above is doing is setting a popover presentation style, then setting a form sheet presentation style. The last one you set will "win", so the view controller will be presented as a form sheet (on iPads, this will be a box in the middle of the screen, on iPhones, it will be full screen).
If you are running this code on an iPhone, then popovers don't work by default anyway - they will be presented full screen. To prevent that, you need to set a delegate to the popover presentation controller, implement - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection;, and return UIModalPresentationNone.
You must also set a source view or bar button item on the popover presentation controller before attempting the presentation, or your app will crash.
Related
I'm building my app using storyboard.so I can open another view controller when button preesed,by dragging.and then I can select presentation= over current context for the segue in storyboard.But what I want is to do this programmatically.I found an answer,but it says it will work for only ipads.I'm building an universal app, so I want to work it for all devices.
Is this possible.
And how can I do that.
in my first view controller
UIStoryboard *story = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
UIViewController *middleViewController = [story instantiateViewControllerWithIdentifier:#"FlightMiddleViewController"];
and in my second view controller, viewDidLoad method I put
self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
it works for a while.that means it transparent for while and then screen black.I don't know why is that.
it is working for iphone also with ios 9 >=
this is what you want to do.
in your first view controller, before you set up which view should present,
- (IBAction)searchNowAction:(id)sender {
UIStoryboard *story = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
UIViewController *secondViewController = [story instantiateViewControllerWithIdentifier:#"secondviewControllerSBname"];
secondViewController.modalPresentationStyle = UIModalPresentationOverFullScreen;
secondViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:secondViewController animated:YES completion:nil];
}
this works fine for iphones also.
You need to set following property before presenting.
self.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
Also set definesPresentationContext property of parent controller to true
Yes it will work only for iPad, as modal presentations and popover controllers are only supported in iPad.
Actually, it's not that hard to do OverCurrentContext presentation style on iPhone. Have a look at UIViewController custom transition, which was introduced in iOS 7. You will find out how to do it.
For anyone that was doning performSegue(.. programmatically
In my case I was having a modal that present a button. taping the button should push a new viewController, but it get push in fullScreen even if Im inside a modal
So after little search I found out that I was setting in storyBoard in xcode:
Presentation => fullScreen. So then I had to change is to Current Context
I added a Navigation Controller to my storyboard and it appears like so:
Now in the table view controller, I gave the TableViewController a storyboard id and class to a TableViewController Controller
When I run my app, I don't see the Navigation Bar at the top. This has been extremely frustrating and can't find a solution anywhere. PLEASE HELP
To get to the scene, someone clicks a button and this code runs and it goes to my Table View Controller:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
LHFileBrowser *LHFileBrowser = [storyBoard instantiateViewControllerWithIdentifier:#"FileBrowser"];
[self.navigationController pushViewController:LHFileBrowser animated:YES];
[self presentViewController:LHFileBrowser animated:YES completion:nil];
The error is in your code.
If you want to (modally) present a view controller when the user presses a button, you need to present the navigation controller (which will contain the table view controller), not the table view controller itself.
Right now, you're presenting the view controller, which won't show it being embedded in a navigation controller.
Also, you're mixing up two different approaches, by trying to push a view controller onto a navigation controller stack, and also presenting the view controller.
Code Sample:
Here's what you apparently mean to do:
UIStoryboard *storyboard = self.storyboard;
UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:#"MyNavigationControllerID"];
LHFileBrowser *rootViewController = [navigationController topViewController];
// Configure your LHFileBrowser view controller here.
rootViewController.someProperty = ...;
// Modally present the embedded view controller
[self presentViewController:navigationController animated:YES completion:nil];
If you want to change the presentation or transition style, you can set those details in your storyboard.
You didn't explain why you had to programmatically add buttons, but Storyboard segues would have instantiated and presented an embedded view controller for you, without you having to have done it in code.
The more you can do in Storyboard, the less code you have to maintain, support, and update, and the more likely your app will still work properly when a new SDK is released.
Update:
The better way to do this is to let Storyboard do it for you, by adding a segue from the button to the navigation controller that you want to present.
In my app, I dynamically load a set of images, and when a user taps on an image, it opens up a new ViewController (MediaPreview) that opens up a large preview of the image.
I create the MediaPreview controller as follows:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
mediaPreviewVC = (MediaPreviewViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MediaPreviewViewController"];
mediaPreviewVC.selectedImageURL = [NSString stringWithFormat:#"%#",gestureRecognizer.view.tag];
navigationController = [[UINavigationController alloc] initWithRootViewController:mediaPreviewVC];
[self presentViewController:navigationController animated:YES completion:nil];
This creates a ViewController that looks like this:
However, the bottom navigation bar appears to be missing, which is outlined in my storyboard:
How can I ensure that the bottom navigation bar buttons appear in my view?
You are creating the navigation controller out of whole cloth:
navigationController = [[UINavigationController alloc] initWithRootViewController:mediaPreviewVC];
alloc-init means "make me a completely fresh, separate, generic one". Thus, navigationController is not the navigation controller in your storyboard. It's a completely new and different navigation controller. Thus, what's in your storyboard is irrelevant.
If you wanted the navigation controller from your storyboard, you needed to instantiate the navigation controller from the storyboard. Or, if you're doing this intentionally, then the bottom bar won't be visible unless you explicitly make it visible, because by default it isn't (it's obvious how to make it visible explicitly).
But then in that case, if you meant to pull it out of the storyboard, then your whole code is probably wrong, because the one in the storyboard probably already has the correct root view controller as well.
So in my universal app I have a section where a person can look at an existing list of notes from our system (retrieved through a simple web service) and then also create a new note if they want. So for the iphone it's pretty simple layout, a TableViewController for displaying the list with a "Add" button on the NavigationBar that presents the modalview for adding the new item. On the iPad though, the same layout has a lot of wasted space so I opted to go with the popOver method to show the list in a popOver and then let them add from there. My problem is that when the user clicks on the Add button within the PopOver view, the modal view comes up full screen instead of just coming up within the popover view. Here's the code I have so far:
-(void) AddButtonPressed:(id)sender {
NewNoteVC *newNote = [[[NewNoteVC alloc] initWithNibName:#"NewNoteVC" bundle:nil] autorelease];
newNote.defaultClientID = defaultClientID;
UINavigationController *navCon = [[[UINavigationController alloc] initWithRootViewController:newNote] autorelease];
if ([isPopOver isEqualToString:#"YES"]) {
[navCon setModalInPopover:YES];
[self.navigationController setModalInPopover:YES];
[self.navigationController presentModalViewController:navCon animated:YES];
}
else {
[self.navigationController presentModalViewController:navCon animated:YES];
}
}
The "isPopOver" string is just a placeholder sent from the previous screen that called this TableView (I know I can switch this to a boolean for better performance I just put this together real quick to try it out). I know I messed up somewhere, I just don't know what setting I need to get this working correctly.
You need to define the view controller's modalPresentationStyle to be "current context".
navCon.modalPresentationStyle = UIModalPresentationCurrentContext;
This will result in modal view controller filling the popover like the popover's root controller.
Try using the presentViewController:animated:completion: instead of presentModalViewController:animated: and set self.navigationController.definesPresentationContext = YES
I need to present a viewController as a formSheet when the user taps a 'settings' button in the navigation bar in the iPad side of my universal app. The view is called "SettingsViewController," and it's a View Controller in my iPad Storyboard with an ID of "settings".
I'm not using segues because in my iPad version of the app, I have more than one button in the navigation bar on the left side, so I have to add the buttons progammatically. (can't connect a segue to a nonexistent UIBarButtonItem in the storyboard.)
The functionality I want is there, the presentation is not.
When I present my SettingsViewController, on the iPad, it's presenting as a full screen view.
I need it to be a form sheet.
I'm wrapping it in a NavigationController, which I need and works fine, just need it to present itself in the correct manner.
Here's the code that presents the view:
-(void)showSettings:(id)sender {
SettingsViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:#"settings"];
svc.delegate = self;
svc.modalPresentationStyle = UIModalPresentationFormSheet;
UINavigationController *navcont = [[UINavigationController alloc] initWithRootViewController:svc];
[self presentViewController:navcont animated:YES completion:NULL];
}
I've tried everything I can think of. Any help is deeply appreciated.
Looks like you set the modal presentation style on SettingsViewController but you should have set it on your UINavigationController.
navcont.modalPresentationStyle = UIModalPresentationFormSheet;