Calling viewWillTransitionToSize: manually - ios

I currently have a UiViewController called Page2
Now if the orientation of the Iphone is changed to landscape the following method is called which is well and good
-(void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
/*The orientation of the phone just changed*/
}
Now My issue is that if the user is already in landscape mode in ViewController page1 and goes to UIViewController Page2 the above method is not called. I know I can check the current orientation of the phone using the following code
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeLeft)
{
//Its in landscape mode
}
However I do not know what parameters to pass for size to the method (that gets called automatically only when the orientation changes) I would like to call this method manually if I detect its in landscape mode. Any suggestions on how to obtain the current size to pass to the method below in which the width would be greater than the height (since its in landscape mode)
-(void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator ;
I tried doing this
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
[self viewWillTransitionToSize:self.view.frame.size withTransitionCoordinator:nil];
}
However the height was greater than the width indicating it was still in portrait mode ... What am i doing wrong ? How can I call this method manually and pass the current size in which width is greater than the height ?
Update:
I would like to give a little more background. I currently have two UIViewControllers PageOne and PageTwo. Now PageTwo has something like this in it for orientation control. The control flow goes from PageOne to PageTwo
//When device rotates
-(void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
DKTuner* sensitivityTuner;
if (size.width > size.height)
{ //Yes this is the landscape mode - Realign some controls to a different position.
.....
}
else
{
// this is portrait mode
.....
}
}
Now the logic of my code in PageTwo assumes that the user entered it in Portrait Mode. However in reality that will not always be true because the user could enter pageTwo while in LandscapeMode. In that case the controls dont readjust to landscape mode as the method of reorientation does not get called in pageTwo. My question is how can I call the reorientation method viewWillTransitionToSize manually and pass a size parameter in which the size width is greater than the height ?

Whatever you're doing in viewWillTransitionToSize:withTransitionCoordinator:, you should probably do in viewWillLayoutSubviews or viewDidLayoutSubviews instead. Those are sent both on a rotation and when the view controller's view first comes onto the screen, and UIKit updates the view's frame before calling viewWillLayoutSubviews.

Related

shouldAutorotate is not locking the screen orientation

I ma trying to lock screen orientation to only landscape orientation when a certain image is still visible, then when the image is hidden, unlock all orientations (targeting iOS 6):
-(BOOL)shouldAutorotate{
if (self.splashImageView.hidden == NO) {
return UIInterfaceOrientationMaskPortrait;//gets called when image is visible
}else{
return UIInterfaceOrientationMaskAll;//gets called when image is hidden
}
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self shouldAutorotate];
}
As you may notice, shouldAutorotate is called properly but the screen is always supporting landscape orientation even when the image is still visible, is there something missing?
P.S: Please note I am trying to get that to work on a tabbar view controller (a UIViewController subclass).
In your appdelegate you have those two methods.but do you have setting like in your project settings -> go to summary tab and see if orientation is set to only landscape or all.Just try to set those.

iOS Auto rotation of view at first

I am working on an ios app that must work in both landscape and portrait, except one view, that should be always in landscape. So, I have:
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) || (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
If I lunch my application in landscape mode, and get to this view and rotate the device (iPad), this method is called and return false, so the interface doesn't rotate, good stuff.
BUT if I am in portrait and get to this view, this method is called and return a false, however the orientation doesn't change. In this context, if I rotate the device to landscape, the method return a true and the interface rotate properly and if I tried to rotate again to portrait, this method return false and the interface remains in landscape.
How can I achieve that the first time I get to this view, if the device is in portrait, the interface change to landscape?
Also, I tried with
- (NSUInteger)supportedInterfaceOrientations
but it never get called.
Thanks in advance!
shouldAutorotateToInterfaceOrientation: is depreciated in iOS 6.0. Override the supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation methods instead.
From the docs on preferredInterfaceOrientationForPresentation
If your view controller implements this method, then when presented,
its view is shown in the preferred orientation (although it can later
be rotated to another supported rotation). If you do not implement
this method, the system presents the view controller using the current
orientation of the status bar.

Cannot resize AdMob's view upon orientation change

What I tried so far is, in viewDidLoad, I called
self.bannerView.autoresizingMask=UIViewAutoresizingFlexibleWidth;
and
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)newInterfaceOrientation duration:(NSTimeInterval)duration {
if (newInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || newInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
self.bannerView.frame=CGRectMake(0.0,
0.0,
480.0,
GAD_SIZE_320x50.height);
}
// Position the UI elements for portrait mode
else {
self.bannerView.frame=CGRectMake(0.0,
0.0,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height);
}
}
Both of these didn't work for me.
Hmm, I don't think that AdMob's creatives can stretch to fit the size of the screen when in landscape. So despite the fact that you're stretching the frame of the view to fit, the ad itself I think will stay the same size.
This means you should still see an ad come in on orientation changes, it will just look like it's the same size (make sure to make another request for an ad in the willAnimateRotationToInterfaceOrientation: method to see this).
You don't need to do any moves, but you must set correct rootViewController for adMovView.
If you use view controller model please add line in each custom view controller
adMobView.rootViewController = viewController;
where viewController - root view controller of your app.
Do not code like this
adMobView.rootViewController = self;
in custom view!

Monotouch - View created in landscape orientation is displayed in portrait orientation

I've created a view controller and set the orientation of the view to Landscape in XCode 4.2 (Interface Builder). However, when I add the view, the view is displayed in Portrait orientation. I've overriden ShouldAutorotateToInterfaceOrientation in all view controllers to return true, and I've attempted to rotate the view manually using the following code:
this.View.Transform.Rotate (3.14f * .5f);
I have also tried to set the frame to a landscape frame (i.e. 480 X 320), though the frame of the view is already set correctly.
Just to clarify, the vast majority of my views are in Portrait. I would like to load the landscape view in a landscape orientation, irrespective of what orientation the device is actually in. Is this possible? If so, what am I missing?
Thanks in advance for any assistance on this matter!
UPDATE: I'm noticing that ShouldAutorotateToInterfaceOrientation is only called once when the view is loaded. It is not called when the device is rotated. Is this normal behavior?
ShouldAutorotateToInterfaceOrientation is called every time the device orientation changes.
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
Console.Out.WriteLine(toInterfaceOrientation.ToString());
// Return true for supported orientations
// This particular screen is landscape only!
return (toInterfaceOrientation == UIInterfaceOrientation.LandscapeLeft || toInterfaceOrientation == UIInterfaceOrientation.LandscapeRight);
}
this code will only allow the view to orient itself in either landscapeleft or landscaperight mode, and writes the new device orientation to the console every time.
when you load the view, and push it onto (for instance) a UINavigationController however, it's still in portrait mode (and ShouldAutorotateToInterfaceOrientation is not called.)
From Objective-C, it's fairly easy to force a device orientation, but in MonoTouch, there appears to be no direct mapping.
the solution ;
Send a message to the objective-c runtime, specifying which orientation we want.
you can easily do this by adding the following to a view controller:
Selector orientationSetter;
private void SetOrientation (UIInterfaceOrientation toOrientation)
{
if (orientationSetter == null)
{
orientationSetter = new Selector ("setOrientation:");
}
Messaging.void_objc_msgSend_int (UIDevice.CurrentDevice.Handle,
orientationSetter.Handle, (int)toOrientation);
}
you can now manually change the orientation of the entire device.
What's more, is that if you use a UINavigationController, the orientation will return back to normal once this view is popped off the stack.
I think that for the Landscape view that you always want to display in Landscape Orientation, you need to return False within ShouldAutorotateToInterfaceOrientation or remove the override altogether. This will prevent the view from auto-rotating when the device is in portrait mode.

UIView rotation text manipulation

I have a view that consists of a bunch of UILabels. As of right now the view is only configured to work when the device's orientation is portrait, however I would like to make it so that when the device is rotated one specific UILabel's location is changed and text size is magnified. When the device's orientation is reverted to portrait, I'd like for it to return to its original state.
How should I go about doing this?
Implement this method in your ViewController and do the changes accordingly.
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
// do something in landscape orientation
} else {
// do something in portrait orientation
}
}

Resources