FbGraph API in landscape mode - iphone

I am using FbGraph API for facebook integration.
Everything is working fine, but the problem is that the FbGraph API does not rotate in landscape view.
I also used the shouldAutorotateToInterfaceOrientation to YES, and i tried to put a breakpoint in didRotateFromInterfaceOrientation.
It is never being called.
I am really stuck into this.
pls help

I use below and its work fine,
1) post notification in - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation method of view from which you use FBGraph
2)
- (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation
{
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:#"CHANGE_ORIENTATION" object:orientation]];
}
3) In the FBGraph
-(void)authenticateUserWithCallbackObject:(id)anObject andSelector:(SEL)selector andExtendedPermissions:(NSString *)extended_permissions {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:#selector(shouldAutorotateToInterfaceOrientation:)
name:#"CHANGE_ORIENTATION"
object:nil];
-----------
-------
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(NSNotification *)notofication
{
if([notofication.object isEqualToString:#"LEFT"])
{
CGAffineTransform newTransform;
newTransform = CGAffineTransformMakeRotation(M_PI * 270 / 180.0f);
webView.transform = newTransform;
[webView setFrame:CGRectMake(12, 0, 320, 480)];
NSLog(#"LEFT");
}
else if([notofication.object isEqualToString:#"RIGHT"])
{
CGAffineTransform newTransform;
newTransform = CGAffineTransformMakeRotation(M_PI * 90 / 180.0f);
webView.transform = newTransform;
[webView setFrame:CGRectMake(0, 0, 305, 480)];
NSLog(#"RIGHT");
}
else if([notofication.object isEqualToString:#"PORTRAIT"])
{
CGAffineTransform newTransform;
newTransform = CGAffineTransformMakeRotation(M_PI * 360 / 180.0f);
webView.transform = newTransform;
[webView setFrame:CGRectMake(0, 12, 320, 480)];
NSLog(#"PORTRAIT");
}
else if([notofication.object isEqualToString:#"DOWN"])
{
CGAffineTransform newTransform;
newTransform = CGAffineTransformMakeRotation(M_PI * 180 / 180.0f);
webView.transform = newTransform;
[webView setFrame:CGRectMake(0, 0, 320, 465)];
NSLog(#"DOWN");
}
return YES;
}

Am using the following method, and its working fine.
Use the following code in the view, in which you are going to use fbgraph.
#i-bhavik thanks for the core idea dude.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(#"left");
[self leftOrienation];
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
[self rightOrientation];
NSLog(#"right");
}
else
{
}
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
here i have initialized FbGraph as fbGraph.
-(void)leftOrienation
{
CGAffineTransform newTransform;
newTransform = CGAffineTransformMakeRotation(M_PI * 270 / 180.0f);
fbGraph.webView.transform = newTransform;
[fbGraph.webView setFrame:CGRectMake(0, 0, 768, 1024)];
}
-(void)rightOrientation
{
CGAffineTransform newTransform;
newTransform = CGAffineTransformMakeRotation(M_PI * 90 / 180.0f);
fbGraph.webView.transform = newTransform;
[fbGraph.webView setFrame:CGRectMake(0, 0, 768, 1024)];
}

It would help to go into detail into your application structure, but some steps to help are:
Set all ViewControllers you're using that need to listen to the rotation with the method defined (note this will rotate for ALL orientations, so look at the documentation on how to support Portrait or Landscape):
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
Review other SO issues that mention this (http://stackoverflow.com/questions/2868132/shouldautorotatetointerfaceorientation-doesnt-work)
Review iOS Documentation on this and their trouble-shooting on this issue

I solved this problem myself.
instead using the UIView provided by FBGraph API I just made my own UIViewController and implemented that API in that view controller.
and the next moment the problem was solved...

Related

iOS Hiding tab bar in iOS 6 creates black bar (fix for iOS 6 breaks iOS 7!)

I've got a tabbed application and in one tab there is a UIWebView. When I rotate the device to landscape I've made the UIWebView full screen while hiding the status and tab bar.
I've got it working in iOS 6 - originally when rotating and hiding the tab bar it would leave a black space where the tab bar was, so the fHeight code fixes this. However, on iOS 6 it worked perfectly, but now it actually creates the black bar problem iOS 6 was having!! Any ideas for a workaround to this?
Please see my edit below this
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
{
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[self hideTabBar:self.tabBarController];
[[UIApplication sharedApplication] setStatusBarHidden:TRUE withAnimation:UIStatusBarAnimationSlide];
}
else
{
[self showTabBar:self.tabBarController];
[[UIApplication sharedApplication] setStatusBarHidden:FALSE withAnimation:UIStatusBarAnimationSlide];
}
}
- (void) hideTabBar:(UITabBarController *) tabbarcontroller
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
float fHeight = screenRect.size.height;
if( UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
{
fHeight = screenRect.size.width;
}
for(UIView *view in self.tabBarController.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
view.backgroundColor = [UIColor blackColor];
}
}
[UIView commitAnimations];
}
- (void) showTabBar:(UITabBarController *) tabbarcontroller
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
float fHeight = screenRect.size.height - 49.0;
if( UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
{
fHeight = screenRect.size.width - 49.0;
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
}
}
[UIView commitAnimations];
}
//Edit
I've tried using this but I'm not sure how to pass in the view properly - I've tried self.view and webView and others but I can't get it to work on both iOS 6 and 7! Any kind of idea at all would be really helpful! Let me know if you need more info
- (void)setTabBarHidden:(BOOL)hidden view:(UIView *)view animated:(BOOL)animated
{
if (self.tabBar.hidden == hidden)
return;
CGRect screenRect = [[UIScreen mainScreen] bounds];
float height = 0.0f;
if(UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
{
height = screenRect.size.width;
}
else
{
height = screenRect.size.height;
}
if (!hidden)
{
height -= CGRectGetHeight(self.tabBar.frame);
}
void (^workerBlock)() = ^() {
self.tabBar.frame = CGRectMake(CGRectGetMinX(self.tabBar.frame), height, CGRectGetWidth(self.tabBar.frame), CGRectGetHeight(self.tabBar.frame));
view.frame = CGRectMake(CGRectGetMinX(view.frame), CGRectGetMinY(view.frame), CGRectGetWidth(view.frame), height);
};
void (^completionBlock)(BOOL finished) = ^(BOOL finished) {
self.tabBar.hidden = hidden;
};
if (animated)
{
[UIView animateWithDuration:0.25f animations:workerBlock completion:completionBlock];
}
else
{
workerBlock();
completionBlock(YES);
}
}
I've created a a public Gist on Github for how we're doing this.
This solution has gone through several iterations due to #Chris Byatt and our team trying it out. So, make sure you download the latest revision from there.
The method signature has been simplified to
- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated;
You can call it like this within your UIViewController subclass:
[self.tabBarController setTabBarHidden:YES animated:YES];
OK guys, after struggling with this for about two hours, my colleague taught me the right way to do it. There is actually a very simple fix that I can't find online:
just put :
self.hidesBottomBarWhenPushed = YES;
In the init method of your viewController and comment out the self.tabBar.hidden related code.
I eventually got this working, but it took a while. It stemmed from a mixture of my original code and some of JRG-Developers work.
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
BOOL toLandscape = UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
CGRect screenRect = [[UIScreen mainScreen] bounds];
void (^workerBlock)() = ^() {
[[UIApplication sharedApplication] setStatusBarHidden:toLandscape withAnimation:UIStatusBarAnimationSlide];
float height = toLandscape ? screenRect.size.width : screenRect.size.height - CGRectGetHeight(self.tabBarController.tabBar.frame);
float width = toLandscape ? screenRect.size.height : screenRect.size.width;
webView.frame = CGRectMake(CGRectGetMinX(webView.frame),
CGRectGetMinY(webView.frame),
width,
height);
[self moveTabBarToPosition:height];
};
[UIView animateWithDuration:0.25f animations:workerBlock];
}
//Moving the tab bar and its subviews offscreen so that top is at position y
-(void)moveTabBarToPosition:(int)y {
self.tabBarController.tabBar.frame = CGRectMake(self.tabBarController.tabBar.frame.origin.x, y, self.tabBarController.tabBar.frame.size.width, self.tabBarController.tabBar.frame.size.height);
for(UIView *view in self.tabBarController.view.subviews) {
if ([view isKindOfClass:[UITabBar class]]) {
[view setFrame:CGRectMake(view.frame.origin.x, y, view.frame.size.width, view.frame.size.height)];
} else {
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, y)];
view.backgroundColor = [UIColor blackColor];
}
}
}
In my case this is for my webview but theoretically you can give it any view. Works in iOS 6 and 7
-(void) hideBottomTabs{
// Get the size of the main screen
CGRect fullScreenRect = [[UIScreen mainScreen]bounds];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0){
UITabBar *bar = ((UITabBarController *)self.parentViewController).tabBarController.tabBar;
fullScreenRect.size.height += ViewHeight(bar);
}
// Hide the tab bar
((UITabBarController *)self.parentViewController).tabBarController.tabBar.hidden = YES;
// Resize and fill the screen
[[((UITabBarController *)self.parentViewController).view.subviews objectAtIndex:0] setFrame:fullScreenRect];
}
I have solved my problem in a such way.

iphone's rotated view doesn't take up whole screen

I rotated a view using CGAffineTransformMakeRotation. ( iPhone - allow landscape orientation on just one viewcontroller )
As you can see below, the images have white region in left and right.
I want the image take up the whole space with black background.(at least in one dimension, width or height )
Below is the full code
- (void) viewDidLoad
{
[super viewDidLoad];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.view.backgroundColor = [UIColor blackColor];
self.imageView.backgroundColor = [UIColor blackColor];
self.imageView.opaque = NO;
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[self.imageView setImageWithURL:[NSURL URLWithString:self.jsonAlbumImage.url_image
relativeToURL: [NSURL URLWithString:#URL_BASE]]
placeholderImage: [GlobalHelper placeHolderImage]];
[self.view addSubview: self.imageView];
}
- (void)didRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[notification object] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft) {
[self.view setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)];
} else if (orientation == UIDeviceOrientationLandscapeRight) {
[self.view setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];
} else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
[self.view setTransform:CGAffineTransformMakeRotation(M_PI)];
} else if (orientation == UIDeviceOrientationPortrait) {
[self.view setTransform:CGAffineTransformMakeRotation(0.0)];
}
}
-- EDIT --
What worked for me in the end .. don't know why modification does work.. any explanation would be great!
UIDeviceOrientation orientation = [[notification object] orientation];
CGAffineTransform t;
CGRect rect;
if (orientation == UIDeviceOrientationLandscapeLeft) {
t = CGAffineTransformMakeRotation(M_PI / 2.0);
rect = CGRectMake(0,0,480,320);
} else if (orientation == UIDeviceOrientationLandscapeRight) {
t = CGAffineTransformMakeRotation(M_PI / -2.0);
rect = CGRectMake(0,0,480,320);
} else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
t = CGAffineTransformMakeRotation(M_PI);
rect = CGRectMake(0,0,320,480);
} else if (orientation == UIDeviceOrientationPortrait) {
t = CGAffineTransformMakeRotation(0.0);
rect = CGRectMake(0,0,320,480);
}
else
return; // looks like there are other orientations than the specified 4
[self.view setTransform:t];
self.view.bounds = rect;
In - (void)didRotate:(NSNotification *)notification, you need to relayout your views so that they occupy all the space available. You could do something like this:
- (void)didRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[notification object] orientation];
CGAffineTransform t;
if (orientation == UIDeviceOrientationLandscapeLeft) {
t = CGAffineTransformMakeRotation(M_PI / 2.0);
} else if (orientation == UIDeviceOrientationLandscapeRight) {
t = CGAffineTransformMakeRotation(M_PI / -2.0);
} else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
t = CGAffineTransformMakeRotation(M_PI);
} else if (orientation == UIDeviceOrientationPortrait) {
t = CGAffineTransformMakeRotation(0.0);
}
CGPoint screenCenter = CGPointMake([UIScreen mainScreen].bounds.width/2,[UIScreen mainScreen].bounds.height/2);
self.view.center = CGPointApplyAffineTransform(screenCenter, t);
self.view.bounds = CGRectApplyAffineTransform([UIScreen mainScreen].bounds, t);
[self.view setTransform:t];
}
Where :
CGRectApplyAffineTransform
Applies an affine transform to a rectangle.
CGRect CGRectApplyAffineTransform (
CGRect rect,
CGAffineTransform t
);
Try also removing this line:
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
You don´t need it if you are not going to use autorotation and I fear it might conflict with your own setting the view's frame. This is just an hypothesis to account for the fact that you are not seeing the view´s frame change.
EDIT:
I'd very much like to know what this transform thing does
well, the transform is doing the rotation.
rotating is relative to an anchor point which is used as a pivot; what happens is that if the anchor point is not in the middle of the view being rotated, then the view is also translated (imagine a rotation around a vertex).
So, it is correct to set the bounds to make things even; indeed I was suggesting just that with the lines:
self.view.center = CGPointApplyAffineTransform(screenCenter, t);
self.view.bounds = CGRectApplyAffineTransform([UIScreen mainScreen].bounds, t);
but possibly the idea of applying the same transform to both the center and the bounds was not blessed. (that is also why I asked for some traces, to see what was happening :-)

Orientation is not working in ios6?

I am having problem in Orientation for ios6. same code is working fine for ios5 or 5.1.
I have used - (BOOL) shouldAutorotate and -(NSInteger)supportedInterfaceOrientations as per the ios6 standard. But still "willRotateToInterfaceOrientation" and "didRotateToInterfaceOrientation" is not getting called.
Here is my code:--
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (!UIInterfaceOrientationIsPortrait(lastOrientation) || !UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
if (!UIInterfaceOrientationIsLandscape(lastOrientation) || !UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
{
CGRect frame;
int viewAlpha;
lastOrientation = toInterfaceOrientation;
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
viewAlpha = 1;
[MovieControlContainerLandscape setHidden:YES];
if (isDauntless) {
[self.navigationController setNavigationBarHidden:NO animated:YES];
} else {
[self.navigationController setNavigationBarHidden:NO];
}
frame = iPad ? CGRectMake(0, 88, 768, 432) : CGRectMake(0, 88, 320, 180);
[movieContainer removeGestureRecognizer:toggleMediaControl];
}
else
{
[PromptToBuy dismissWithClickedButtonIndex:0 animated:YES];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
viewAlpha = 0;
[MovieControlContainerLandscape.layer setCornerRadius:22];
[MovieControlContainerLandscape.subviews.lastObject addSubview:[MediaControls use]];
if (isDauntless) {
[self.navigationController setNavigationBarHidden:YES animated:YES];
} else {
[self.navigationController setNavigationBarHidden:YES];
}
frame = iPad ? CGRectMake(0, 0, 1024, 768) : CGRectMake(0, 0, 480, 320);
[movieContainer addGestureRecognizer:toggleMediaControl];
if (isDauntless) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.6];
}
if(TSFullScreen)
{
[movieContainer setAlpha:1];
}
if (isDauntless) {
[UIView commitAnimations];
}
}
[viewContainer setAlpha:viewAlpha];
// Size the overlay view for the current orientation.
[movieContainer setFrame:frame];
}
}
/* Sent to the view controller after the user interface rotates. */
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if (!UIInterfaceOrientationIsPortrait(lastOrientation) || !UIInterfaceOrientationIsPortrait(fromInterfaceOrientation))
if (!UIInterfaceOrientationIsLandscape(lastOrientation) || !UIInterfaceOrientationIsLandscape(fromInterfaceOrientation))
{
float duration = .5;
if (fromInterfaceOrientation == UIInterfaceOrientationPortrait || fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
if (isDauntless) {
[UIView transitionWithView:movieContainer duration:duration options:UIViewAnimationOptionTransitionNone animations:^{
/* Move movie view to parent center. */
[self.moviePlayerController.view setCenter:movieContainer.center];
} completion:^(BOOL finished) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
CGRect frame = movieContainer.frame;
/* Size movie view to fit parent view. */
frame.origin.y = 0;
[self.moviePlayerController.view setFrame:frame];
[UIView commitAnimations];
CGPoint center = CGPointMake(movieContainer.center.x, -50);
[MovieControlContainerLandscape setTag:0];
[MovieControlContainerLandscape setHidden:NO];
[MovieControlContainerLandscape setCenter:center];
[self toggleMovieController];
}];
} else {
CGRect frame = movieContainer.frame;
/* Size movie view to fit parent view. */
frame.origin.y = 0;
[self.moviePlayerController.view setFrame:frame];
CGPoint center = CGPointMake(movieContainer.center.x, -50);
[MovieControlContainerLandscape setTag:0];
[MovieControlContainerLandscape setHidden:NO];
[MovieControlContainerLandscape setCenter:center];
[self toggleMovieController];
}
}
else
{
if (!isDauntless) {
[MovieControlContainer setFrame:CGRectMake(0, 44, 768, 68)];
}
[MovieControlContainer addSubview:[MediaControls use]];
//NSLog(#"MovieControlContainer is %#, MovieControlContainer subviews: %#",MovieControlContainer,[MovieControlContainer subviews]);
if (isDauntless) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
}
if(TSFullScreen){
[movieContainer setAlpha:0];
}
CGRect frame = movieContainer.frame;
/* Size movie view to fit parent view. */
frame.origin.y = 0;
[self.moviePlayerController.view setFrame:frame];
if (isDauntless) {
[UIView commitAnimations];
}
}
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return !mediaTypeIsAudio && isOrientationSupported;
}
//----supported method for ios6--------//
- (BOOL) shouldAutorotate
{
return YES;
}
-(NSInteger)supportedInterfaceOrientations{
NSInteger mask = 0;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight])
mask |= UIInterfaceOrientationMaskLandscapeRight;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeLeft])
mask |= UIInterfaceOrientationMaskLandscapeLeft;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationPortrait])
mask |= UIInterfaceOrientationMaskPortrait;
if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationPortraitUpsideDown])
mask |= UIInterfaceOrientationMaskPortraitUpsideDown;
return mask;
}
//i have set the rootviewcontroller in appdelegate file
self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
self.window.rootViewController = self.navigationController.
Please help me to take out from this problem. I am not able to find the solution for this.Thanks in Advance !!.
In ios6 you have to use this method for vieworientation
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationMaskAll;
}
And check my answer Link

Only having one view autorotate in xcode?

Ok so I currently have 3 views and I need only one of them to autorotate to any orientation while the rest stay in portrait. Right now my set up is a splashviewcontroller fades into view A, and inside view A is a button to switch to view B. All I want is for view B to be able to rotate to any orientation.
When I return YES for shouldautorotatetointerfaceorientation in the splashviewcontroller, every view rotates because this is the parent view. When I return portrait only in the splashview, nothing rotates even if the other views return YES. Is there a way to only have view B rotate? I'm willing to do it manually if you can provide code. Thanks
You can manually mange the rotation of any desired UIView object like so:
EDIT:
In the init or viewDidLoad
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(rotate) name:UIDeviceOrientationDidChangeNotification object:nil];
[super viewDidLoad];
}
#define degreesToRadian(x) (M_PI * (x) / 180.0)
-(void)rotate{
self.view.bounds = CGRectMake(0, 0, 0, 0);
if ([UIDevice currentDevice].orientation == UIInterfaceOrientationPortrait){
CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(0));
landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 320, 480);
[self.view setTransform:landscapeTransform];
} else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationPortraitUpsideDown){
CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(180));
landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 320, 480);
[self.view setTransform:landscapeTransform];
} else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight){
CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 480, 320);
[self.view setTransform:landscapeTransform];
}else if ([UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft){
CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(270));
landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
self.view.bounds = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, 480, 320);
[self.view setTransform:landscapeTransform];
}
}
Inside targets summary section, Supported Interface Orientation all items are selected except for Upside Down, so all you need to do is go to your .m file that handles the view and use this piece of code.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
This did the trick for me.

Rotating the screen from Portrait to Landscape

I use following code I found in the web to rotate the screen to landscape mode. I don’t understand what they suppose to do. Specially the bounds it is setting. Can someone give some explanation what it is doing?
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait)
{
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
UIScreen *screen = [UIScreen mainScreen];
CGRect newBounds = CGRectMake(0, 0, screen.bounds.size.height, screen.bounds.size.width - statusBarFrame.size.height);
self.navigationController.view.bounds = newBounds;
self.navigationController.view.center = CGPointMake(newBounds.size.height / 2.0, newBounds.size.width / 2.0);
self.navigationController.view.transform = CGAffineTransformConcat(self.navigationController.view.transform, CGAffineTransformMakeRotation(degreesToRadian(90)));
self.navigationController.view.center = window.center;
}
Your rootView's size used to be (320, 480) for example, after rotating, you should set it to (480, 320) in order to fit the screen in landscape mode, that's why you need to change the bounds of your view.
Set the transform is making the view actually rotate 90 degrees.
UIKit is doing the similar things when automatically rotate for you.
Hello Janaka,
You will try this code.
Take a look at the function `shouldAutorotateToInterfaceOrientation`: in the UIViewController class. This function returns YES if the orientations is supported by your UIView. If you return YES only to the landscape orientation, then the iPhone will automatically be put in that orientation.
The following code should do it. Put it in the UIViewController that controls the view that you want to put in landscape mode.
Use this Method.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscape);
}
Try this link its definitely help you
this
#define degreesToRadians(x) (M_PI * x / 180.0)
- (void)viewWillAppear:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
CGRect newBounds = CGRectMake(0, 0, 480, 320);
self.navigationController.view.bounds = newBounds;
self.navigationController.view.center = CGPointMake(newBounds.size.height / 2.0, newBounds.size.width / 2.0);
self.navigationController.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
self.navigationController.view.transform = CGAffineTransformIdentity;
self.navigationController.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0));
self.navigationController.view.bounds = CGRectMake(0.0, 0.0, 320.0, 480.0);
[super viewWillDisappear:animated];
}