Labels not aligning properly in landscape mode - iphone

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);
}
}

Related

iPhone orientation issue in single class

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];
}

UITableView does not change it size correctly on device orientation change

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

How to set the device orientation in iphone?

I am doing a project which supports both landscape mode and portrait mode,but now i am struck there is no orentation in my project.I set the
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
//return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
return YES;
}
but no use,when i press the command +Right-arrow for rotation to right it rotates to left but view and controllers didnt change to that orentation.Then after some googling i get this code
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
// do stuff
}
But the problem is i didn't know how to write code in //do stuff.Please help me to do this.
thanks in advance.
EDIT:
i put this code ,it is working but whenthe simulator is again turns to portrait it wont be in the default mode.my code is
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight))
{
NSLog(#"Csantos shouldAutorotateToInterfaceOrientation: left or right");
//
table.transform = CGAffineTransformIdentity;
table.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
table.bounds = CGRectMake(0.0, 0.0, 320, 402);//[self.view setFrame:CGRectMake(0.0, 0.0, 768, 90)];
}
else
{
table.transform = CGAffineTransformIdentity;
table.transform = CGAffineTransformMakeRotation(degreesToRadian(360));
table.bounds = CGRectMake(0.0, 51, 320, 402);
}
return YES;
}*/
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
{
//Handle portrait
}
else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
{
//Handle landscape
}
}
This is better code.
You can do like this.
if (UIInterfaceOrientationPortrait == interfaceOrientation || UIInterfaceOrientationLandscapeLeft == interfaceOrientation || UIInterfaceOrientationLandscapeRight == interfaceOrientation ||
UIInterfaceOrientationPortraitUpsideDown == interfaceOrientation) {
return YES;
}

IPhone - Not getting Interface Orientation

I'm trying to set another position for a UIButton when I change the orientation of my IPhone and I had the idea to implement it inside BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation.
Is this the right way to implement or is there any error on my code? It's not printing these logs.
Code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if ((interfaceOrientation == UIDeviceOrientationLandscapeLeft) ||
(interfaceOrientation == UIDeviceOrientationLandscapeRight))
{
NSLog(#"Csantos shouldAutorotateToInterfaceOrientation: left or right");
[adButton setFrame:CGRectMake(100, 700, 768, 90)];
}
if ((interfaceOrientation == UIDeviceOrientationPortrait) ||
(interfaceOrientation == UIDeviceOrientationPortraitUpsideDown))
{
NSLog(#"Csantos shouldAutorotateToInterfaceOrientation: Portrait or UpsideDown portrait");
[adButton setFrame:CGRectMake(0, 0, 768, 90)];
}
return YES;
}
Regards!
Your code too should work but try putting following code. Change is from UIDeviceOrientation to UIInterfaceOrientation
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight))
{
NSLog(#"Csantos shouldAutorotateToInterfaceOrientation: left or right");
[adButton setFrame:CGRectMake(100, 700, 768, 90)];
}
if ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
{
NSLog(#"Csantos shouldAutorotateToInterfaceOrientation: Portrait or UpsideDown portrait");
[adButton setFrame:CGRectMake(0, 0, 768, 90)];
}
return YES;
}
shouldAutorotateToInterfaceOrientation simply specifies what rotations are supported by a UIViewController - nothing more.
You may consider modifying willRotateToInterfaceOrientation which is called just before a rotation is about to begin - to hide background views or other objects, for example.
Then, override didRotateFromInterfaceOrientation which is called after a rotation has just finished - I think this one will be the most important for your setFrame: code.
I don't undestand, the adButton setFrame works correctly or not and this viewcontroller is root or you have another one. Can't it be that root controller shouldAutorotateToInterfaceOrientation return NO for any orientation?

How to set the different image view frame size in Orientations in iPhone?

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;
}