Control rotation - iphone

I have an app like pulse app, an horizaontal tableview. I want to show 5 rows in a section but i want to control the width of the row when it is in landscape or portrait, so it adjust to the screen. how can i do this is there a method to do that ?

Resize your UITableView when the device is rotated. Something like this:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration{
//Resize your UITableView, i.e.:
orientation = self.interfaceOrientation;
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
myTable.frame = CGRectMake(x, y, width, height);
}
else
{
myTable.frame = CGRectMake(x, y, width, height);
}
[[self view] setNeedsDisplay];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

Related

Device rotaion issue

I detect screen change orientation with :
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(detectOrientation) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
and when it call i call this method:
if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
[md changeToBigNowPlaying:YES withLayer:avPlayerLayer];
}else{
[md changeToBigNowPlaying:NO withLayer:avPlayerLayer];
}
and in this function i do this stuff:
-(void)changeToBigNowPlaying:(BOOL)flag withLayer:(AVPlayerLayer*)layer{
if (flag) {
//To portrait
[landVC.view removeFromSuperview];
[layer removeFromSuperlayer];
[self.window addSubview:tab.view];
}else {
//To landscape
[tab.view removeFromSuperview];
[landVC setUpNewLayer:layer];
[self.window addSubview:landVC.view];
}
}
and this is the explanation :
I have tabbar with viewcontroller,and when i detect that the user rotate the screen to landscape i remove the tabbar and add a UIViewController(landVC) and when i rotate it to portrait again i remove the landvc and add again the tabbar.
my problem is when i rotate first to UIInterfaceOrientationLandscapeLeft so it show me the screen in UIInterfaceOrientationLandscapeRight. but when i rotate to UIInterfaceOrientationLandscapeRight first i see the screen like it should and when i rotate from this situation to UIInterfaceOrientationLandscapeLeft i see the screen good too.
any idea why i have a problem when i rotate to UIInterfaceOrientationLandscapeLeft and i see the landVc in UIInterfaceOrientationLandscapeRight
In landVC UIViewController i implement:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight){
return YES;
}else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
return YES;
}else {
return NO;
}
}
Edit
-(void)detectOrientation{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
[md changeToBigNowPlaying:YES withLayer:avPlayerLayer];
}
if(orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft) {
[md changeToBigNowPlaying:NO withLayer:avPlayerLayer];
}
}

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 manage application when orientation changes?

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

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?