Ensure a view remains full screen upon rotate - iphone

I have a simple UIWebView that I've added to my UIViewController in the viewDidLoad method:
CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
self.webView = [[UIWebView alloc] initWithFrame:rect];
self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.webView];
And it looks great, but when I rotate the phone, the width and height remain the same so now it's too wide for the update view frame. I also tried using self.view.bounds, but it didn't make any difference.
So how do you ensure that a view that is fullscreen on load, stays the same size when rotated? (without using IB)

What you are doing is correct & should work in most scenarios. But since I have no idea about your View Stack. I will suggest a sure shot way -
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
CGRect rect;
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft||toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
else
{
//some other dimensions.
}
self.webView = [[UIWebView alloc] initWithFrame:rect];
}

Because web view not called only once it need to be called again
to set the new frame
self.webView = [[UIWebView alloc] initWithFrame:rect];
so you have to register notification in viwewillappear or viewdidload
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(viewBecamePortrait:) name:#"orientationIsPortrait" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(viewBecameLandscape:) name:#"orientationIsLandscape" object:nil];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
NSNotification* notification = [NSNotification notificationWithName:#"orientationIsPortrait" object:self];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}else {
NSNotification* notification = [NSNotification notificationWithName:#"orientationIsLandscape" object:self];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
}
Then implement
-(void)viewBecameLandscape:(id)sender{
if(webview){
[webview.setframe(cgrectmake(x,y,width,height))];
}
}
-(void)viewBecamePortrait:(id)sender{
}

Related

How to change orientation of toolbar which is adding to window

I am developing one app which is based on tabbarcontroller.In one view Controller i want to hidden the tabbarController instead of that i want to show toolbar for that purpose i wrote the following code
//tabBAr hidden
[self.tabBarController.tabBar setHidden:YES];
//creation of tool bar
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
tb = [[UIToolbar alloc] init];
//[tb setBackgroundImage:[UIImage imageNamed:#"tabbar.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
tb.tintColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
if ([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone)
{
tb.frame = CGRectMake(0, delegate.window.frame.size.height-50, 320, 44);
}
else
{
tb.frame = CGRectMake(0, delegate.window.frame.size.height-70, 768, 44);
}
[delegate.window addSubview:tb];
But problem is in iPad i want to change orientation of toolbar but it does not change it always takes portrait width and height.
Use notification to detect the device orientation like the following.
// Start Orientation //
- (void)orientationChanged:(NSNotification *)notification{
[self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
}
- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//NSLog(#"Vertical");
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//NSLog(#"Horizontal");
}
}
// End Orientation //
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
Somebody has kindly written this notification code somewhere. I didn't write it.
use viewWillLayoutSubViews method and add code to setFrame of toolbar in that method
ex:
- (void) viewWillLayoutSubviews
{
[toolBar setFrmae:CGRectMake(self.view.bounds.origin.x, self.view.bounds.size.height - 44, self.view.bounds.size.width, 44)];
}

how to provide only one view with landscape for MPMoviePlayerController in IOS6

i had spend 2 hours finding the correct code to fix my orientation problem
this are my movieplayer code. i need this particular view to be shown in landscape.
in appdelegate i set all orientation and in my rootviewcontroller i set is as portrait only and in my movieplayer view as landscape but not luck. can anybody give me some comment on how to fix the issues please ?
- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
return UIInterfaceOrientationMaskAll;
}
my rootviewcontroller
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
-(void)prepareIntroVideo
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"designthinking_pt1" ofType:#"mp4"]];
self.playercontroller = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.playercontroller.view setFrame:CGRectMake(0, -20, 320, 480)];
self.playercontroller.movieSourceType = MPMovieSourceTypeFile;
self.playercontroller.scalingMode = MPMovieScalingModeAspectFill;
self.playercontroller.fullscreen = NO;
self.playercontroller.controlStyle = MPMovieControlStyleFullscreen;
//playercontroller.controlStyle = MPMovieControlStyleFullscreen;
self.playercontroller.view.userInteractionEnabled =YES;
self.playercontroller.view.backgroundColor = [UIColor blackColor];
self.playercontroller.shouldAutoplay = NO;
//playercontroller.repeatMode = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.playercontroller];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidEnterBackground)
name: UIApplicationDidEnterBackgroundNotification
object:[UIApplication sharedApplication]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillEnterForeground)
name:UIApplicationWillEnterForegroundNotification
object:[UIApplication sharedApplication]];
[self.playercontroller prepareToPlay];
[self.view addSubview:self.playercontroller.view];
[self.playercontroller setFullscreen:YES animated:YES];
//[self.playercontroller stop];
[self.view sendSubviewToBack:self.playercontroller.view];
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationLandscapeLeft;
}
You can check my example... https://dl.dropboxusercontent.com/u/19438780/testRotate.zip
I have 2 controllers with navigation controller - portrait; for this I "subclassed" nav controller (here is the "rotation" code for portrait)
The 3th controller is landscape - must be presented modal if you want to rotate it - put the rotation code accordingly .
I would look into preferredInterfaceOrientationForPresentation, I spent about a week on this problem a few months ago. I don't remember exactly how i solved it but I do know it had something to do with implementing that method

keyboardWillShow not respond

here is problem, when i debug my program, i found keyboardWillShow function not responds every time. just first time, it will be called by program. here is my code, i dont know whats wrong in my code, but, when the keyboard first appeared, the function run well.
- (void)keyboardWillShow:(NSNotification *)notification {
/*
Reduce the size of the text view so that it's not obscured by the keyboard.
Animate the resize so that it's in sync with the appearance of the keyboard.
*/
NSDictionary *userInfo = [notification userInfo];
// Get the origin of the keyboard when it's displayed.
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
// Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
CGRect keyboardRect = [aValue CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
CGFloat keyboardTop = keyboardRect.origin.y;
CGRect newTextViewFrame = self.textview.frame;
newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
// Get the duration of the animation.
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
// Animate the resize of the text view's frame in sync with the keyboard's appearance.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
textview.frame = newTextViewFrame;
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary* userInfo = [notification userInfo];
/*
Restore the size of the text view (fill self's view).
Animate the resize so that it's in sync with the disappearance of the keyboard.
*/
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
// textview.frame = self.view.bounds;
[self save];
[UIView commitAnimations];
}
and i regist notification
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
and remove it in here
- (void)viewDidUnload
{
[super viewDidUnload];
[self save];
self.textview = nil;
self.title = nil;
self.tags = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
i resign firstresponder, here is my code
- (IBAction)save:(id)sender {
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addNote)] autorelease];
[textview resignFirstResponder];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(save:)] autorelease];
[self setUpUndoManager];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
[self save];
return YES;
}
Make sure that you are not writing any code to remove observer.....
Please provide keyboardWillHide method also....

How to build a bar on the keyboard

I want to dismiss keyboard by click dismiss button ,How to build a bar on the keyboard? like this:
(source: alexcurylo.com)
It was answered here. Basically, you send a resignFirstResponder message to the UITextView. Of course, you will put that code on your button's delegate.
Try to put following code. It might work for you.
Put following variables in your viewController.h file
UIToolbar *keyboardToolbar;
id notisender;
BOOL isAlreadyResigned;
float kx,ky,kh,kw;
Now put following code in you viewController.m file.
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated{
isAlreadyResigned=YES;
[self keyboardToolbarShouldShow];
[self keyboardToolbarShouldShow];
[super viewWillAppear:animated];
}
-(void)viewWillDisappear:(BOOL)animated{
[self keyboardToolbarShouldNotShow];
[super viewWillDisappear:animated];
}
#pragma mark keyboardWillShow methods
-(void)keyboardToolbarShouldShow {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardhide:)
name:UIKeyboardWillHideNotification
object:nil];
}
-(void)keyboardToolbarShouldNotShow {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
if(!isAlreadyResigned){
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillNotShow:)
name:UIKeyboardWillShowNotification
object:nil];
[GEEtxtMsg becomeFirstResponder];
}
}
-(void)keyboardWillShow : (NSNotification *)sender {
#try {
// NSLog(#"notification in first view");
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
for (UIView *keyboard in [keyboardWindow subviews]) {
NSLog(#"keyboard description - %#",[keyboard description]);
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES) {
NSValue *v = [[sender userInfo] valueForKey:UIKeyboardBoundsUserInfoKey];
CGRect kbBounds = [v CGRectValue];
if(keyboardToolbar == nil) {
keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectZero];
keyboardToolbar.barStyle=UIBarStyleBlackOpaque;
keyboardToolbar.tintColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(dismissKeyboard)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *items = [[NSArray alloc] initWithObjects:flex, barButtonItem, nil];
[keyboardToolbar setItems:items];
[items release];
}
[keyboardToolbar removeFromSuperview];
keyboardToolbar.frame = CGRectMake(0, 0, kbBounds.size.width, 45);
[keyboard addSubview:keyboardToolbar];
NSLog(#"x=%f y=%f width=%f height=%f",kbBounds.origin.x, kbBounds.origin.y, kbBounds.size.width, kbBounds.size.height);
kx=kbBounds.origin.x; ky=kbBounds.origin.y;
kh=kbBounds.size.height; kw=kbBounds.size.width;
keyboard.bounds = CGRectMake(kbBounds.origin.x, kbBounds.origin.y, kbBounds.size.width, kbBounds.size.height + 87);
isAlreadyResigned=NO;
for(UIView* subKeyboard in [keyboard subviews]) {
if([[subKeyboard description] hasPrefix:#"<UIKeyboardImpl"] == YES) {
subKeyboard.bounds = CGRectMake(kbBounds.origin.x, kbBounds.origin.y - 45, kbBounds.size.width, kbBounds.size.height);
}
}
}
}
}
} #catch (NSException * e) {
NSLog(#"Problem in keyboardWillShow:%#",e);
}
}
-(void)keyboardWillNotShow : (NSNotification *)sender {
#try {
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
for (UIView *keyboard in [keyboardWindow subviews]) {
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES) {
// NSValue *v = [[sender userInfo] valueForKey:UIKeyboardBoundsUserInfoKey];
// CGRect kbBounds = [v CGRectValue];
if([[keyboard subviews] containsObject:keyboardToolbar]){
[keyboardToolbar removeFromSuperview];
}
if(!isAlreadyResigned){
isAlreadyResigned=YES;
keyboard.bounds = CGRectMake(kx,ky,kw,kh);
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
for(UIView* subKeyboard in [keyboard subviews]) {
if([[subKeyboard description] hasPrefix:#"<UIKeyboardImpl"] == YES) {
subKeyboard.bounds = CGRectMake(0, 0,0, 0);
}
}
}
}
}
}
} #catch (NSException * e) {
NSLog(#"Problem in keyboardWillShow:%#",e);
}
}
-(void)dismissKeyboard {
[self resignTextFields];
}
-(void)keyboardhide:(NSNotification *)noti {
notisender = noti;
}
Here you can build a bar on a keyboard
Take static toolbar on xib and take one button and put it on toolbar, and make toolbar hidden ,now
write code in your method when you create a toolbar ,like if you want to create toolbar on textview begin editing method then,
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.26];
[tlBar setFrame:CGRectMake(0, 220, 320, 44)];
tlBar.hidden=NO;
[UIView commitAnimations];
}
And when you dismiss keyboard write this method and call this method when toolbar button pressed
-(IBAction)kbAway:(id)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
[tlBar setFrame:CGRectMake(0, 480, 320, 44)];
[UIView commitAnimations];
[txtviewmessage resignFirstResponder];
[txtsubject resignFirstResponder];
[txttemplatename resignFirstResponder];
}
Hope this will help you..

View controller not getting -shouldAutorotateToInterfaceOrientation: messages on second load?

I have a UIViewController that I'm using to control a "pop-up" view for viewing images throughout my application. It supports autorotation, as it automatically sizes the image to fit properly regardless of orientation. This works perfectly, but only the first time I initialize and display the view controller. When it closes, I am removing the UIView from my view hierarchy and releasing the view controller - but the next time I instantiate and add it to my view hierarchy, it stops receiving the -shouldAutorotateToInterfaceOrientation messages when the phone is rotated.
This is how I instantiate and display it:
popupVC = [[PopupVC alloc] init];
[popupVC viewWillAppear:NO];
[[[UIApplication sharedApplication] keyWindow] addSubview:popupVC.view];
[popupVC viewDidAppear:NO];
this is how I remove/release it when it's finished:
[popupVC viewWillDisappear:NO];
[popupVC.view removeFromSuperview];
[popupVC viewDidDisappear:NO];
[popupVC release];
popupVC = nil;
I've tried looping through [[UIApplication sharedApplication] keyWindow] subviews to see if somehow my popup view isn't on top, but it always is. And it has a different address each time so I do know that it's a different instance of the view controller class.
As requested, here is the complete loadView method from PopupVC:
- (void)loadView {
UIView *myView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
myView.backgroundColor = self.overlayColor;
myView.autoresizesSubviews = NO;
myView.hidden = YES;
myView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.view = myView;
[myView release];
_isVisible = NO;
UIView *myMaskView = [[UIView alloc] initWithFrame:self.view.bounds];
myMaskView.backgroundColor = [UIColor clearColor];
myMaskView.clipsToBounds = YES;
myMaskView.hidden = YES;
myMaskView.autoresizesSubviews = NO;
myMaskView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:myMaskView];
self.imageMaskView = myMaskView;
[myMaskView release];
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
myImageView.center = self.view.center;
myImageView.hidden = NO;
myImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleHeight;
[self.imageMaskView addSubview:myImageView];
self.imageView = myImageView;
[myImageView release];
UIButton *myImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
myImageButton.frame = self.view.frame;
myImageButton.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleHeight;
[myImageButton addTarget:self action:#selector(clickImage:) forControlEvents:UIControlEventTouchUpInside];
[self.imageMaskView addSubview:myImageButton];
self.imageButton = myImageButton;
UIActivityIndicatorView *myActivityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
myActivityView.hidden = YES;
[self.view addSubview:myActivityView];
myActivityView.center = self.view.center;
self.activityView = myActivityView;
[myActivityView release];
}
The shouldAutorotateToInterfaceOrientation handler isn't the place for custom code to respond to rotation events. It's purpose is just to tell the OS that your view controller can rotate. If you want to have custom code to handle the rotation events you should overide - didRotateFromInterfaceOrientation or one of the other similar callback methods depending on your needs:
willRotateToInterfaceOrientation:duration:
willAnimateRotationToInterfaceOrientation:duration:
didRotateFromInterfaceOrientation:
willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:
didAnimateFirstHalfOfRotationToInterfaceOrientation:
willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:
See Autorotating Views in the developer docs.
I think that might be a bug in the new OS 3.0. A workaround to this would be to use NSNotificationCenter after turning on beginGeneratingDeviceOrientationNotifications.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receivedRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void) receivedRotate: (NSNotification *) notification {
DebugLog(#"ORIENTATION CHANGE");
UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
if(interfaceOrientation == UIDeviceOrientationPortrait) {
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
self.view.bounds = CGRectMake(0, 0, 320, 480);
}
else if(interfaceOrientation == UIDeviceOrientationLandscapeLeft) {
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
self.view.bounds = CGRectMake(0, 0, 480, 320);
}
else if(interfaceOrientation == UIDeviceOrientationLandscapeRight) {
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90));
self.view.bounds = CGRectMake(0, 0, 480, 320);
}
}
My problem is that I get the rotation event for my rootViewController, but as soon as I launch my second viewController, it won't get any signals except motion (shaking) and touch. Is that the same issue as the original post -- it is certainly similiar??
I followed-up with above recommendation and I don't have the selector coded correctly so it throws an exception as soon as I try to rotate. Meanwhile, I found this other discussion on the web which confirms that the problem was introduced in 3.0.
link text