UIModalPresentationFormSheet resizing view - ipad

I am interested to know on how I can resize the view when using UIModalPresentationFormSheet of modalPresentationStyle, it looks like it has a fixed size so I was wondering if anyone out there did managed to manipulate the popup view from the sizing perspective.
So there is either UIModalPresentationFormSheet with a fixed view size or full views and I am after something in between.

MyModalViewController *targetController = [[[MyModalViewController alloc] init] autorelease];
targetController.modalPresentationStyle = UIModalPresentationFormSheet;
targetController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:targetController animated:YES];
// it is important to do this after presentModalViewController:animated:
targetController.view.superview.bounds = CGRectMake(0, 0, 200, 200);

You are able to adjust the frame of a modal view after presenting it:
Tested in iOS 5.1 - 6.1, using XCode 4.62
MyModalViewController *targetController = [[[MyModalViewController alloc] init] autorelease];
targetController.modalPresentationStyle = UIModalPresentationFormSheet;
targetController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; //transition shouldn't matter
[self presentModalViewController:targetController animated:YES];
targetController.view.superview.frame = CGRectMake(0, 0, 200, 200);//it's important to do this after presentModalViewController
targetController.view.superview.center = GPointMake(roundf(self.view.center.x), roundf(self.view.center.y));//self.view assumes the base view is doing the launching, if not you might need self.view.superview.center etc.
Update The preferred iOS 6.0 view controller presentation method also works correctly:
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion

In ios8 and earlier works:
AboutViewController * _aboutViewController = [[AboutViewController alloc] init];
_aboutViewController.modalPresentationStyle = UIModalPresentationFormSheet;
if(IS_IOS8)
{
_aboutViewController.preferredContentSize = CGSizeMake(300, 300);
}
[self presentViewController:_aboutViewController animated:YES completion:nil];
In AboutViewController.m
- (void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
if(!IS_IOS8)
{
self.view.superview.bounds = CGRectMake(0, 0, 300, 300);
}
}
IS_IOS8
#define IS_IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)
In iOS 8 you can also use UIPresentationController which gives you more customization options.

For iOS 8, simply implement the delegate method (CGSize)preferredContentSize on each view controller. It should resolve all the size issue.

Just to extent Fatos solution (great one)
If your are creating the view controller using a .xib file after the alloc initWithNibName you could store the view frame:
CGRect myFrame = targetController.view.frame;
...
targetController.view.superview.bounds = myFrame;
And then use it for superview.bounds, so the view's size in the .xib will be used and you could change the size more visually.

I put this code in the form sheet view controller:
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.view.superview.bounds = CGRectMake(0, 0, 540, 500); // your size here
}
Note that the resizing occurs early enough that the the presentation animation looks correct.

Related

Editing bounds of UIView when presenting hides keyboard in iOS 8

I present a small "login" UIViewController as a UIModalPresentationFormSheet with custom bounds. In the viewWillLayoutSubviews method, I change the size of the view to (300,250). This has worked in iOS 5/6/7 but no longer works in 8.
When the view is presented and a UITextField is tapped, the app becomes unresponsive (not frozen, just not responding to touches). Almost as if the keyboard is presented but not appearing. Delegate methods ARE called correctly. If I remove the self.view.superview.bounds = CGRectMake(0, 0, 300, 250); from the viewWillLayoutSubviews method the keyboard works, but the view is now a full sized UIModalPresentationFormSheet style.
This only happens in iOS 8, so I can only assume its an issue with the way the keyboard is presented and the way I am masking/resizing the view, but I'm at a loss as to the solution.
Presenting ViewController -
UserLoginViewController *loginVC = [[UserLoginViewController alloc] initWithNibName:#"UserLoginViewController" bundle:nil];
loginVC.modalPresentationStyle = UIModalPresentationFormSheet;
loginVC.delegate = self;
[self presentViewController:loginVC animated:YES completion:nil];
Editing the view bounds -
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
self.view.superview.layer.cornerRadius = 10.0;
self.view.superview.layer.masksToBounds = YES;
self.view.superview.bounds = CGRectMake(0, 0, 300, 250);
}
In iOS8 You shouldn't change superview bounds in viewWillLayoutSubviews because it causes infinite loop.
EDIT:
in iOS8 property preferredContentSize works well.
You should change your code in that way:
UserLoginViewController *loginVC = [[UserLoginViewController alloc] initWithNibName:#"UserLoginViewController" bundle:nil];
loginVC.modalPresentationStyle = UIModalPresentationFormSheet;
loginVC.delegate = self;
if(IS_IOS8)
{
loginVC.preferredContentSize = CGSizeMake(300, 250);
}
[self presentViewController:loginVC animated:YES completion:nil];
Editing the view bounds -
- (void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
self.view.superview.layer.cornerRadius = 10.0;
self.view.superview.layer.masksToBounds = YES;
if(!IS_IOS8)
{
self.view.superview.bounds = CGRectMake(0, 0, 300, 250);
}
}
Another way, which gives you more customization options is to use UIPresentationController and UIViewControllerTransitioningDelegate. Take a look at my code below.
Parent view controller:
_aboutViewController = [[AboutViewController alloc] init];
_aboutViewController.modalPresentationStyle = UIModalPresentationFormSheet;
if(IS_IOS8)
{
if(aboutTransitioningDelegate == nil)
{
aboutTransitioningDelegate = [[AboutTransitioningDelegate alloc] init];
}
_aboutViewController.transitioningDelegate = aboutTransitioningDelegate;
_aboutViewController.modalPresentationStyle = UIModalPresentationCustom;
}
[self presentViewController:_aboutViewController animated:YES completion:nil];
AboutViewController.m
#import "AboutViewController.h"
#interface AboutViewController ()
#end
#implementation AboutViewController
- (void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
if(IS_IOS8)
{
return;
}
CGSize displaySize = CGSizeMake(320, 462);
self.view.superview.bounds = CGRectMake(0, 0, displaySize.width, displaySize.height);
}
#end
AboutTransitioningDelegate.h:
#interface AboutTransitioningDelegate : NSObject <UIViewControllerTransitioningDelegate>
#end
AboutTransitioningDelegate.m:
#import "AboutTransitioningDelegate.h"
#import "AboutPresentationController.h"
#implementation AboutTransitioningDelegate
-(UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
{
return [[AboutPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
}
#end
AboutPresentationController.h
#import <UIKit/UIKit.h>
#interface AboutPresentationController : UIPresentationController
#end
AboutPresentationController.m
#import "AboutPresentationController.h"
#implementation AboutPresentationController
-(CGRect)frameOfPresentedViewInContainerView
{
CGSize displaySize = CGSizeMake(320, 462);
if([[Config sharedInstance] latestVersionFromAppstoreInstalled])
{
displaySize = CGSizeMake(320, 416);
}
CGRect r = CGRectZero;
r.size = displaySize;
r.origin.y = self.containerView.bounds.size.height/2 - displaySize.height/2;
r.origin.x = self.containerView.bounds.size.width/2 - displaySize.width/2;
return r;
}
-(void)containerViewWillLayoutSubviews
{
[super containerViewWillLayoutSubviews];
self.presentedView.frame = [self frameOfPresentedViewInContainerView];
}
#end
ProjectName-Prefix.pch
#define IS_IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8)
The simplest way to call containerViewWillLayoutSubviews() is to call:
containerView?.setNeedsLayout()

Best Practices, addChildViewController, addSubview

I am creating an app using iOS 5, and I want to work as follows:
MainViewController w/ NavigationBar, and i set on viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
NavViewController *nav = [[NavViewController alloc] init];
[self addChildViewController:nav];
[self.view addSubview:nav.view];
}
Like a "partial view" with some nav controls (my own/custom toolbar), and at NavViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect viewRect = CGRectMake(0, 0, 320, 200);
self.view.frame = viewRect;
self.view.backgroundColor = [UIColor redColor];
}
At this point I have MainViewController with a navigation subview and still have space (in MainViewController view) that another subview can be added using the actions NavViewController.
Is there a serious flaw in this logic? or I can keep developing ?
Thnks a lot.

Can't make modal view appear right in the center of the screen on iPad iOS6

I have a problem with modalviewcontrollers that I want to represent on iPad screen. If I leave the size as it is, then it's all centred fine. But I have very few info on these views, so I need to resize them.
So, when I resize them, I can't force them to appear in the middle of the screen. Even if I manage to center one of them in one orientation, it's messed up in other one.
Here is my current code:
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:acvc];
[nav setModalPresentationStyle:UIModalPresentationFormSheet];
[nav setModalTransitionStyle: UIModalTransitionStyleCoverVertical];
[[acvc view] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"main_bg.jpg"]]];
[self presentViewController:nav animated:YES completion:nil];
nav.view.superview.frame = CGRectMake(self.view.bounds.size.width/2 + 175, self.view.bounds.size.height/2 - 125, 350, 250);
// nav.view.superview.center = self.view.window.center;
Would appreciate any help, thank you.
Here's a method that works on iOS7 as well as iOS6 and iOS5 and still allows you to use UIModalTransitionStyleCoverVertical:
-(void)presentController:(UIViewController*)controller fromRootController:(UIViewController*)rootController withSize:(CGSize)size
{
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:controller];
nav.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
nav.modalPresentationStyle = UIModalPresentationFormSheet;
[rootController presentModalViewController:nav animated:YES];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
nav.view.superview.backgroundColor = [UIColor clearColor];
nav.view.bounds = CGRectMake(0, 0, size.width, size.height);
}
else
{
nav.view.superview.bounds = CGRectMake(0, 0, size.width, size.height);
}
}
Change your last line to the following:
nav.view.superview.bounds = CGRectMake(0, 0, 350, 250);
Stopped working in iOS7, unfortunately.
Only changing ModalTransitionStyle to Dissolve solves the problem. Maybe it's a bug in iOS 7...
in ios 7 you can use :
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
self.view.superview.bounds = CGRectMake(0, 0, width, height);
}
it works.

FreeForm ViewController - iOS

I have added another viewcontroller (freeform viewController) on the top of mainviewcontroller, but somehow, it is not showing full size. Please look at my code which is below and screenshots.
- (void)viewDidLoad
{
[super viewDidLoad];
firstSurveyViewController = [[FirstSurveyViewController alloc] initWithNibName:#"FirstSurveyViewController" bundle:[NSBundle mainBundle]];
CGRect rect = [firstSurveyViewController.view frame];
rect.origin.x = 10;
rect.origin.y = 20;
[firstSurveyViewController.view setFrame:rect];
[self.view addSubview:firstSurveyViewController.view];
}
Try setting with width and height in addition to the origin, manually. What is the initial value of rect after CGRect rect = [firstSurveyViewController.view frame];?
Also, in general, you don't really want to add viewControllers to other viewControllers unless you're using a UINavigationController or UIViewController Containment to manage them. Seems like in this case firstSurveyViewController should just be firstSurveyView, a subclass of UIView.

UIWebView does not cover entire view in landscape mode

I am developing an iPad App that will load a webpage in a UIWebView modally when the user is authenticated through a login form. This works great, but when I rotate the device into landscape mode, the webview covers only 75% of the screen:
Here is the code from my login view controller:
// Load storyboard for easy instatiation of the login view controller
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPad" bundle:nil];
WebViewController *webController =
(WebViewController *)[storyboard instantiateViewControllerWithIdentifier:#"WebView"];
// Present the embedded browser in fullscreen.
[webController setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:webController animated:YES completion: nil];
Are you using constraints or auto-resizing masks? If not, you might want to use them and make sure the web view covers the entire view's frame.
All you gotta do is define the frame of UIWebView for landscape orientation in -shouldAutorotateToInterfaceOrientation method of your WebViewController.
It goes something like this,
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if(toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
webView.frame = CGRectMake (0, 0 768, 1024);
}
else
{
webView.frame = CGRectMake (0, 0 1024, 768);
}
return YES;
}
Use webView delegates
2.Include this method
(void)webViewDidFinishLoad:(UIWebView *)webView {
[self.webView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}
}
include this one also
(void)willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self.webView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}

Resources