i have 4 classes (class1, class2, class3 & class4) for class1, class2 & class3 i have all modes support (Landscape & Portrait). But for class 4 i need only Portrait support. Issue here is when i load class4 in Landscape mode its showing view in landscape, when i rotate class4 to Portrait mode once, its not rotating to landscape again. I want class4 to load in portrait only weather i come load class 4 in portrait or landscape mode.
i tried this
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation != UIInterfaceOrientationPortrait)
{
return NO;
}
}
Please let me know, how to proceed
Seems like all you need to do is return a YES for everything but portrait orientation.
How about:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// b.t.w., there is also an upside down portrait constant
// which is UIInterfaceOrientationPortraitUpsideDown
if (interfaceOrientation != UIInterfaceOrientationPortrait)
{
return NO;
}
return YES;
}
B.T.W., according to the public documentation for shouldAutorotateToInterfaceOrientation:, this API is deprecated as of iOS 6.0. If you care about your app's survivability for the future, you may want to consider doing something different.
Try this
-(void)orientationChanged:(NSNotification *)object{
NSLog(#"orientation change");
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if(deviceOrientation == UIInterfaceOrientationLandscapeLeft ||deviceOrientation == UIInterfaceOrientationLandscapeRight)
{
Bg.image=[UIImage imageNamed:#"splashBgL.png"]; ///Lendscape Background
Bg.frame=CGRectMake(0, 0, 480, 320);
self.view.frame=CGRectMake(0, 0, 480, 320);
}
else{
Bg.image=[UIImage imageNamed:#"splashBg.png"];///Protrait Background
Bg.frame=CGRectMake(0, 0, 320, 480);
}
}
-(void)landscapeLeftOrientation{
CGRect contentRect = CGRectMake(0, 0, 480, 320);;
self.view.bounds = contentRect;
}
-(void)landscapeRightOrientation{
CGRect contentRect = CGRectMake(0, 0, 480, 320);
self.view.bounds = contentRect;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:)name:#"UIDeviceOrientationDidChangeNotification" object:nil];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self landscapeLeftOrientation];
}
Related
I am displaying Child view controller on my parent view controller using
childView1 *viewController = [[childView1 alloc]initWithNibName:examTFVC_NIB bundle:nil row:gesture.view.tag productID:test_productID size:testsize Duration:duration];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
self.view.alpha = 0.4f;
viewController.view.backgroundColor = [UIColor clearColor];
viewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:viewController animated:YES completion:NULL];
I am displaying transparent view controller over my parent view controller and its landscape only.
but issue is when i change the device from landscape left to landscape right or right to left than my child view controller orientation changes but parent view controller remain same orientation.
My parent view controller orientation changes when i am not displaying child view controller how can i change parent view controller orientation along with child?
One thing i tried to change orientation by programming passing Notification Center from child to parent to change the orientation.
In child view controller
- (NSUInteger)supportedInterfaceOrientations
{
NSUInteger supportedOrientations = UIInterfaceOrientationMaskLandscape;
[[NSNotificationCenter defaultCenter] postNotificationName:#"RotateParent"object:nil];
return supportedOrientations;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight );
}
- (BOOL)shouldAutorotate
{
return YES;
}
In parent view controller
-(void)rotateParent:(NSNotification *)notification
{
CGAffineTransform transform = CGAffineTransformMakeRotation(-M_PI);
self.view.transform = transform;
NSLog(#"Parent rotate ");
}
but when i click to display my child over parent view than my "rotateParent" method get executed one time by default.
any other solution??
For < iOS 6.0
in childView1
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[[NSNotificationCenter defaultCenter] postNotificationName:#"RotateParent"object:nil];
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||interfaceOrientation == UIInterfaceOrientationLandscapeRight );
}
For > iOS 6.0
- (BOOL)shouldAutorotate
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"RotateParent" object:nil];
return YES;
}
in Parent View
Add observer for notification, and based on current orientation rotate parent view with proper angle.
-(void)rotateParent:(NSNotification *)note{
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
CGFloat rotationAngle = 0;
if (orientation == UIDeviceOrientationPortraitUpsideDown) rotationAngle = M_PI;
else if (orientation == UIDeviceOrientationLandscapeLeft) rotationAngle = M_PI_2;
else if (orientation == UIDeviceOrientationLandscapeRight) rotationAngle = -M_PI_2;
[UIView animateWithDuration:0.5 animations:^{
self.view.transform = CGAffineTransformMakeRotation(rotationAngle);
self.view.transform = CGAffineTransformMakeRotation(rotationAngle);
self.view.transform = CGAffineTransformMakeRotation(rotationAngle);
} completion:nil];
//adjust view frame based on screen size
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}
else
{
self.view.bounds = CGRectMake(0.0, 0.0, 320, 480);
}
}
let me know if you face any issue.
Hey if I have understood your problem correctly, than you may want to look into this superb method from UIView.
- (void)viewWillLayoutSubviews
When a view’s bounds change, the view adjusts the position of its
subviews. Your view controller can override this method to make
changes before the view lays out its subviews. The default
implementation of this method does nothing.
So this method will get called before every subsequent rotation of your Child View Controller and you can post notification to your parent view controller from this method.
This may sound a newbie question, however I'm new to iOS dev,
I've UIWebView and UITableView on my iPad view.
In shouldAutorotateToInterfaceOrientation I resize them for nice look like this.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if( interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
CGRect f = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2);
self.mTextView.frame = f;
f = CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2);
self.mTableView.frame = f;
}
if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
CGRect f = CGRectMake(0, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
self.mTextView.frame = f;
f = CGRectMake(self.view.frame.size.width / 2, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
self.mTableView.frame = f;
}
return YES;
}
Now the question:
For the first load the table view is drawn with correct sizes but when I switch to portrait mode then back to landscape mode the UITableView becomes wider than specified by frame. So why this happens and how to fix this ?
PS. I've tried also to set the frames for all cells in the same method didn't help.
PSS. This happens only on device, on simulator UITableView is displayed correctly
shouldAutorotateToInterfaceOrientation: only returns a boolean value indicating whether the view controller supports the specified orientation, you should not do any UI transformation in this method, instead do everything in willAnimateRotationToInterfaceOrientation:duration:.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
if( interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
CGRect f = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2);
self.mTextView.frame = f;
f = CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2);
self.mTableView.frame = f;
}
if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
CGRect f = CGRectMake(0, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
self.mTextView.frame = f;
f = CGRectMake(self.view.frame.size.width / 2, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
self.mTableView.frame = f;
}
}
Have a look at the documentation for UIViewController, it's well explained.
iOS calls this function just one time! For doing this work nice as you expect, you must add this line into the (void)viewDidLoad function:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
And making (void)orientationChanged:(id)object function:
- (void)orientationChanged:(id)object{
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication] statusBarOrientation;
if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
// some works
}else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
// another works
}
}
I hope it be useful for you!
You can change the size of UITableView in both portrait and landscape modes. UITableView change size automatically in a ratio with respect to previous orientation mode . So you can fix desired size for both modes.
Hope this work for you.
Please check the autoResizing property of the views.
The simplest way is you can make 2 diffrent view for the portrait and landscape mode so whenever mode changes you can easily change the view.
Like this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
self.view = self.portraitView;
else
self.view = self.landscapeView;
[tblScore reloadData];
return YES;
}
you use AutoResizing for this no need to do coding for frame..
http://developer.apple.com/library/ios/#documentation/windowsviews/conceptual/viewpg_iphoneos/CreatingViews/CreatingViews.html
I have an application in which I am changing screen orientation. But when I change orientation for the first screen and go to the next screen, the change does not persist.
Attached are images for clearer understanding.
The main problem is that when device is rotated then first screen rotates but second does not launch in the new orientation.
It will rotate only when we change orientation after the screen is launched.
How do I fix that?
The problem is that when you rotate the device then
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Method gets called but when you go to the next screen then this method does not get called. if you rotate your device again then that method gets called.
So if you want to change next screen orientation as device current orientation, then check the device's current orientation with viewDidLoad method or ViewWillApper() method. Then according to that set your current view.
Use this buddy...
If suppose, you are adding UIImageView to your self.view as
**in .h**,
UIImageView *topView
**in .m**,
topView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#“anyImage.png"]];
topView.userInteractionEnabled = YES;
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
topView.frame = // set your dimensions as per landscape view
}
else if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
topView.frame = // set your dimensions as per portrait view
}
[self.view addSubView : topView];
**Add this lines at end of your viewDidLoad.**
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didRotate:)
name:#"UIDeviceOrientationDidChangeNotification"
object:nil];
**And the method**
- (void)didRotate:(NSNotification *)notification
{
UIDeviceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
CGRect screenBounds = [[UIScreen mainScreen] bounds];
switch (orientation)
{
case UIInterfaceOrientationLandscapeLeft:
{
topView.frame = // set your dimensions as per landscape view
break;
}
case UIInterfaceOrientationLandscapeRight:
{
topView.frame = // set your dimensions as per landscape view
break;
}
case UIInterfaceOrientationPortraitUpsideDown:
{
topView.frame = // set your dimensions as per portrait view
break;
}
case UIInterfaceOrientationPortrait:
{
topView.frame = // set your dimensions as per portrait view
break;
}
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
Happy coding..
call this method.
-(void) viewWillAppear:(BOOL)animated{
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
[self willAnimateRotationToInterfaceOrientation:orientation duration:0];
}
-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown )
{
enter code here
set frame.........
}
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight )
{
set frame.........
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
Is the code below correct? When the user rotates the device, two labels are supposed to go to the coordinates given below. It works when the user starts the app in portrait mode, the labels are placed correctly. However, when the user starts in landscape mode, the labels DO NOT get placed correctly. But if you rotate the view to portrait and then back to landscape, they align properly. I've tried placing the landscape coordinates in viewDidLoad, and it still doesn't work. What should I do? Thanks for your help!
The two labels are recordingTimeLabel and recordingTimeLabelMinutes.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
//is landscape
backGround.frame = CGRectMake(0, 0, 768, 1024);
recordingTimeLabel.center = CGPointMake(967, 22);
recordingTimeLabelMinutes.center = CGPointMake(901, 22);
NSLog(#"is landscape");
// fixedSpace.width = 400;
} else {
//is portrait
backGround.frame = CGRectMake(0, 0, 1024, 768);
recordingTimeLabel.center = CGPointMake(710, 22);
recordingTimeLabelMinutes.center = CGPointMake(661, 22);
NSLog(#"is portrait");
}
}
Additionally, this code doesn't work either:
-(void)viewWillAppear:(BOOL)animated {
if (([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight) || ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft)) {
//is landscape
backGround.frame = CGRectMake(0, 0, 768, 1024);
recordingTimeLabel.center = CGPointMake(967, 22);
recordingTimeLabelMinutes.center = CGPointMake(901, 22);
NSLog(#"is landscape");
} else {
//is portrait
backGround.frame = CGRectMake(0, 0, 1024, 768);
recordingTimeLabel.center = CGPointMake(710, 22);
recordingTimeLabelMinutes.center = CGPointMake(661, 22);
NSLog(#"is portrait");
}
}
willRotateToInterfaceOrientation: may not be called if you start in landscape mode. I suggest setting the coordinates in viewWillAppear:, not viewDidLoad, to figure out the initial orientation (you can use self.interfaceOrientation if you have autorotation enabled).
Have you set following code also for the UIInterfaceOrientation?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
Got it. I used an NSTimer and called a function very often that contained this code:
-(void)updateLabelLocation {
if (([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight) || ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft)) {
recordingTimeLabel.center = CGPointMake(710, 22);
recordingTimeLabelMinutes.center = CGPointMake(661, 22);
}
}
I have created the the image view and set the images in that view and added the image view as sub view of the scroll view(like slide show images). SO i want to set the image view frame size and scroll view frame size as dynamically. I want to set the different image view frame size and the scroll view size in the portrait and landscape mode. So how can i set the different frame size in the landscape and portrait mode.
Here my sample code,
- (void)viewDidLoad {
[super viewDidLoad];
self.scroll = [[UIScrollView alloc] init];
self.scroll.delegate = self;
self.scroll.pagingEnabled = YES;
self.scroll.contentMode = UIViewContentModeScaleAspectFit;
self.scroll.autoresizingMask = ( UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleWidth );
[self.view addSubview:self.scroll];
totalArray = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:#"dashboard_demo_2.png"],
[UIImage imageNamed:#"dashboard_demo_1.png"],
[UIImage imageNamed:#"dashboard_demo_2.png"],
[UIImage imageNamed:#"3.jpg"],
[UIImage imageNamed:#"4.jpg"],
[UIImage imageNamed:#"12.jpg"],
[UIImage imageNamed:#"5.jpg"],nil];
x = 0;
for(int i = 0; i< [totalArray count]; i++)
{
img = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, 320, 200)];
img.image =[totalArray objectAtIndex:i];
x = x+320; // in portait mode if the image sets correctly, but landscape mode it will x = x + 480;
[self.scroll addSubview:img];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait ||
orientation == UIDeviceOrientationPortraitUpsideDown )
{
[self portraitMode];
}
else if (orientation == UIDeviceOrientationLandscapeLeft ||
orientation == UIDeviceOrientationLandscapeRight )
{
[self LandscapeMode];
}
return YES;
}
-(void) portraitMode
{
//How to set the frame sizes.
self.scroll.frame = CGRectMake(0, 0, 320, 200);
img.frame = CGRectMake(x, 0, 320, 200);
scroll.contentSize = CGSizeMake(x, 200); // it doesn't set
}
-(void) LandscapeMode
{
//How to set the frame sizes
self.scroll.frame = CGRectMake(0, 0, 480, 320);
img.frame = CGRectMake(x, 0, 480, 200);
scroll.contentSize = CGSizeMake(x, 320); // it doesn't set
}
PLease help me out.
Thanks.
Hi ,
you can register to notification center for change in screen orientation.
Code to enroll with notification center.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(ScreenRotate:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
Implement the ScreenRotate method.
-(void) screenRotate:(NSNotificationCenter*) notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if( orientation == UIDeviceOrientationLandscapeLeft)
{
}
else if ( orientation == UIDeviceOrientationLandscapeRight )
{
}
else if ( orientation == UIDeviceOrientationPortrait )
{
}
}
Code to unregister with notification center.
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIDeviceOrientationDidChangeNotification
object:nil];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}