iOS - presentViewController like the image (Open iMessages app store in Messages) - ios

I want present a UINavigationController like the image above, I use
nvc.modalPresentationStyle = UIModalPresentationFormSheet;
But I can't change the controller's size.
This is how I did:
UIViewController *vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor grayColor];
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
nvc.modalPresentationStyle = UIModalPresentationFormSheet;
nvc.view.frame = CGRectInset(self.view.bounds, 20, 20);
[self presentViewController:nvc animated:YES completion:nil];
But the present viewController is still full screen.

May be you can use preferredContentSize as follows:
nvc.modalPresentationStyle = UIModalPresentationFormSheet;
nvc.preferredContentSize = CGSizeMake(width, height);
Like,
nvc.preferredContentSize = CGSizeMake(300, 600);
OR
In Presented viewController's viewDidLoad method write below code:
self.preferredContentSize = CGSizeMake(width, height);
Edit:
Select your viewController or xib and set size property to Freeform.
Enable user preferred explicit size in Content size in attributes inspector.
For more detail refer answers here:

Related

Adding a persistent footer underneath a UITabBarController

I've inherited a project for which I want to add a persistent footer area beneath the entire app. The app is using a UITabBarController that is always shown other than for a login screen. The tabBarController is created as follows:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
UIStoryboard *sb1 = [UIStoryboard storyboardWithName:#"SB1" bundle:nil];
// ... same for sb2 and sb3
[tabBarController setViewControllers:#[sb1, sb2, sb3]];
[tabBarController setSelectedIndex:0];
I've tried manually setting tabBarController.size.height, but it doesn't seem to have any effect. I've never used storyboards before, is there some way to do initWithFrame when using them? Or am I approaching this completely the wrong way?
Thanks werediver, you got me on the right track. I got it working by adding the UITabBarController as a child view controller and then manually adding its view as well.
const CGFloat FOOTER_HEIGHT = 20;
// Set up tab bar area
CGRect contentFrame = [UIApplication sharedApplication].delegate.window.frame;
contentFrame.size.height -= FOOTER_HEIGHT;
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.view.frame = contentFrame;
// Set up footer
CGRect footerFrame = contentFrame;
footerFrame.origin.y = contentFrame.size.height;
footerFrame.size.height = FOOTER_HEIGHT;
UIView *footer = [[UIView alloc] initWithFrame:footerFrame];
// Create outer view controller
UIViewController *outer = [[UIViewController alloc] init];
[outer addChildViewController:tabBarController];
[outer.view addSubview:tabBarController.view];
[outer.view footer];
[[UIApplication sharedApplication] keyWindow].rootViewController = outer;

UIPopoverPresentationController can't setup arrow color

I am trying to present popover with UIPopoverPresentationController class on the iPhone.
I need to present navigation controller inside the popover. So I do the following:
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
viewController.preferredContentSize = size;
navigationController.modalPresentationStyle = UIModalPresentationPopover;
self.currentPopoverController = navigationController.popoverPresentationController;
self.currentPopoverController.backgroundColor = [UIColor commonApplicationBgColor];//My common app blue color
self.view.alpha = 0.7;
[self presentViewController:navigationController animated:YES completion:nil];
It is working, but arrow colour is differ from the popover navigation bar background colour. I checked they use the same [UIColor commonApplicationBgColor], but it looks like arrow colour is darker. Tried setup alpha for self.currentPopoverController.backgroundColor, but it is still has wrong color.
Please check image:
if you don't want arrow then you can pass 0 to permittedarrowdirection like,
[popoverController presentPopoverFromRect:YOUR_RECT
inView:self.view
permittedArrowDirections:0
animated:YES];
Update :
you can set like,
popover = [[UIPopoverController alloc] initWithContentViewController:contentViewController];
popover.backgroundColor = contentViewController.view.backgroundColor;
set bacground color to popovercontroller.
Update 2 :
myPopoverViewController.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:myPopoverViewController animated: YES completion: nil];
// Get the popover presentation controller and configure it.
UIPopoverPresentationController *presentationController =
[myPopoverViewController popoverPresentationController];
presentationController.permittedArrowDirections =
UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionRight;
presentationController.sourceView = myView;
presentationController.sourceRect = sourceRect;
presentationController.backgroundColor = [UIColor grayColor]; //set your expacted color here.
Hope this will help

Can't change size for UIPopover?

I'm writing an app that needs to spawn a popover in order to add a new note. To that end, I have something that does kind of the trick, however, I can't seem to adjust the size of the popover. This is how I'm spawning it:
UIButton* btn =sender;
UIViewController* fooTroller = [[UIViewController alloc] init];
CGRect rectFoo = CGRectMake(0, 0, 100, 100);
UIView* fooView = [[UIView alloc] initWithFrame:rectFoo];
[fooView setBackgroundColor:[UIColor redColor]];
[fooTroller setView:fooView];
popOver =[[UIPopoverController alloc] initWithContentViewController:fooTroller];
[popOver presentPopoverFromRect:btn.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionLeft
animated:YES];
Any thoughts? It's not respecting the view size.
You should not be calling setView: on the view controller. Let the view controller setup its own view.
The proper way to size a popover is to either override the contentSizeForViewInPopover method of your view controller to return the size or to set the popoverContentSize property on the popover.
UIButton* btn =sender;
UIViewController* fooTroller = [[UIViewController alloc] init];
popOver = [[UIPopoverController alloc] initWithContentViewController:fooTroller];
popOver.popoverContentSize = CGSizeMake(100, 100);
[popOver presentPopoverFromRect:btn.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionLeft
animated:YES];

How do I make a popover appear over a button inside a collection view protype cell

I created a UpcomingCell.h and .m for the cell. In the button's tap method i tried this
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
// cameraSystemMenu = [mainStoryboard instantiateViewControllerWithIdentifier:#"CameraSystemMenuViewController"];
UIView *anchor = sender;
UIViewController *viewControllerForPopover =
[storyboard instantiateViewControllerWithIdentifier:#"yourIdentifier"];
self.popover = [[UIPopoverController alloc]
initWithContentViewController:viewControllerForPopover];
[self.popover presentPopoverFromRect:anchor.frame
inView:anchor.superview
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
and I also tried this
self.popoverContent = [[PopOverViewController alloc]init];
UIView *popoverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 300)];
[popoverView setBackgroundColor:[UIColor purpleColor]];
self.popoverContent.view = popoverView;
self.popoverContent.contentSizeForViewInPopover = CGSizeMake(200, 300);
UIPopoverController *contactPopover =[[UIPopoverController alloc] initWithContentViewController:self.popoverContent];
[contactPopover presentPopoverFromRect:self.remindButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES ];
[contactPopover setDelegate:self];
and countless other popover examples on stackoverflow and I cant get it to work.
Both examples crashes. The first one crashes on
the rect passed in to this method must have non-zero width and height. This will be an exception in a future release
The second one crashes because an UIPopoverController needs to be a property or an instance variable, otherwise it will be deallocated on the end of the method, because the View that's showing it will not retain it

How can I show a UIDatePicker inside a Popover on iPad using StoryBoard?

I have achieved to show the datepicker inside the popover, doing it programmatically as it is shown in UIDatePicker in UIPopover.
But I have been trying to do it in interface Builder, I already made a View Controller named DatePickerViewController.m with a DatePicker in it and its corresponding IBoulet
#import <UIKit/UIKit.h>
#interface DatePickerViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIDatePicker *birthdayDatePicker;
#end
Then I need this to be shown in a popover when the Birthday text field is being edited. so I use the following code
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
//Assign DatePicker to Birthday TextField
//build our custom popover view
DatePickerViewController* popoverContent = [[DatePickerViewController alloc] init];
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 216);
// dismiss existing popover
if (self.popoverControllerBirthday)
{
[self.popoverControllerBirthday dismissPopoverAnimated:NO];
self.popoverControllerBirthday = nil;
}
//create a popover controller with my DatePickerViewController in it
UIPopoverController *popoverControllerForDate = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
//Set the delegate to self to receive the data of the Datepicker in the popover
popoverControllerForDate.delegate = self;
//Adjust the popover Frame to appear where I want
CGRect myFrame =textField.frame;
myFrame.origin.x = 260;
myFrame.origin.y = 320;
//Present the popover
[popoverControllerForDate presentPopoverFromRect:myFrame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
self.popoverControllerBirthday = popoverControllerForDate;
return NO; // tells the textfield not to start its own editing process (ie show the keyboard)
}
And I also import the custom DatePickerViewController in my current ViewController
#import "DatePickerViewController.h"
But it is not showing the datepicker inside the popover.
Does anybody know what could be happening?
You might want to avoid all the work in the storyboard by creating the ViewController and the DatePicker programmatically:
//build our custom popover view
UIView *v = [[UIView alloc] init];
CGRect pickerFrame = CGRectMake(0, 0, 320, 216);
UIDatePicker *pView=[[UIDatePicker alloc] initWithFrame:pickerFrame];
pView.datePickerMode = UIDatePickerModeDate;
self.datePicker = pView;
[v addSubview:self.datePicker];
UIViewController *popoverContent = [[UIViewController alloc]init];
popoverContent.view = v;
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 216);
// dismiss existing popover
if (self.pcDatePicker)
{
[self.pcDatePicker dismissPopoverAnimated:NO];
self.pcDatePicker = nil;
}
//create a popover controller with my DatePickerViewController in it
UIPopoverController *popoverControllerForDate = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
//Set the delegate to self to receive the data of the Datepicker in the popover
popoverControllerForDate.delegate = self;
//Adjust the popover Frame to appear where I want
CGRect myFrame = sender.frame;//CGRectMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 320, 200);
myFrame.origin.x = 260;
myFrame.origin.y = 320;
//Present the popover
[popoverControllerForDate presentPopoverFromRect:myFrame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
self.pcDatePicker = popoverControllerForDate;
And then use the delegate Method to get the date:
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
self.startDate = self.datePicker.date;
}
Solved
All I have to do is instantiate the StoryBoard Date Picker View Controller like :
...
DatePickerViewController* popoverContent = [[DatePickerViewController alloc] init];
popoverContent =[[UIStoryboard storyboardWithName:#"MainStoryboard"
bundle:nil]
instantiateViewControllerWithIdentifier:#"DatePickerVC"];
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(320, 216);
...
where the identifier is set in StoryBoard Attributes Inspector
and now it is shown

Resources