Device Orientation in iphone sdk - iphone

i have a requirnmant for ipad to show contents both in landscape and portrait..so i have done the code and done the postions of controlles in each orientations.it works fine..my code is
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight ||interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) {
_viewContainer.frame = CGRectMake(342, 1, 681, 707);
_viewtopic.frame = CGRectMake(1, 62, 343, 56);
_viewpublicspeekingheaderleft.frame = CGRectMake(0, 6, 341, 58);
_viewTableview.frame = CGRectMake(1, 118, 341, 590);
_viewFooter.frame = CGRectMake(1, 716, 1024, 48);
if (interfaceOrientation == UIInterfaceOrientationPortrait ||interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ) {
_viewContainer.frame = CGRectMake(276, 1, 503, 946);
_viewtopic.frame = CGRectMake(0, 58, 275, 50);
_viewpublicspeekingheaderleft.frame = CGRectMake(0, 1, 275, 58);
_viewTableview.frame = CGRectMake(1, 109, 274, 837);
_viewFooter.frame = CGRectMake(1, 954, 767, 48);
}
return YES;
}
the above code works fine for me,i got alla the controllers in corect postions..here is my probm,,i have to set a timer to show full screen and i need to hide some controlers in the viewcontroller,,i had done the timer set to 3 second and after 3 second it hides some controlles and arrange the othe controlers in the view controller to show the ful sceen.it also works fine for me.my code fo this is
- (void)viewDidLoad
{
[super viewDidLoad];
// Define the timer object
NSTimer *timer;
// Create the timer object
timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self
selector:#selector(updateorientation:) userInfo:nil repeats:NO];
}
- (void) updateorientation:(NSTimer *)incomingTimer
{
_tableTopic.hidden =YES;
_viewtopic.hidden =YES;
_viewpublicspeekingheaderleft.hidden =YES;
_viewTableview.hidden =YES;
_imgpulbicspeakingabovethetableview.hidden =YES;
_topiclabel.hidden =YES;
_lblpublicspeekingleft.hidden =YES;
UIInterfaceOrientation orientation =[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown ) {
NSLog(#"portrait");// only works after a rotation, not on loading app
_imgMicimage.frame = CGRectMake(69, 130, 635, 216);
_txtContentofTopic.frame = CGRectMake(69, 354, 635, 203);
_viewContainer.frame = CGRectMake(0, 0, 768, 1024);
}
UIInterfaceOrientation orientationLandscape =[[UIApplication sharedApplication] statusBarOrientation];
if (orientationLandscape == UIDeviceOrientationLandscapeLeft || orientationLandscape == UIDeviceOrientationLandscapeRight ) {
NSLog(#"landscape");
_imgMicimage.frame = CGRectMake(93, 113, 836, 239);
_txtContentofTopic.frame = CGRectMake(93, 360, 836, 203);
_viewContainer.frame = CGRectMake(0, 0, 1024, 768);
}
}
it works fine,but after the timer fires ,then i chnage the orientation of the device ,i only get the arrangmnts in the - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { function.i need the arrangmnts in the time function.how to solve this.
thanks in advance.
}

You should not be doing this work in -shouldAutorotateToInterfaceOrientation:. That method is simply to determine whether your UI should rotate (and will soon be superceded by a new system). You should use these methods to lay out your UI in reaction to an actual rotation:
-willRotateToInterfaceOrientation:duration:
-willAnimateRotationToInterfaceOrientation:duration:
-didRotateFromInterfaceOrientation:
Please see the UIViewController docs for more on those.
On iOS 5, -viewWillLayoutSubviews might be another good choice. (It will be called even if your controller's view isn't currently visible.)

Related

iPhone Orientation change frame of view

I want to set position of UILable programatically depend on orientation. But When I get back to Portrait, it doesn't work.
Here is my code :
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown)
{
lblUserName.frame = CGRectMake(20, 200, 280, 44);
lblPassword.frame = CGRectMake(20, 246, 280, 44);
}
else
{
lblUserName.frame = CGRectMake(20, 220, 280, 44);
lblPassword.frame = CGRectMake(20, 266, 280, 44);
}
}
There are 2 possibilities.Either you can set the label programatically.And check the condition for orientation in the - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{}
method.Otherwise you can use auto resizing mask.
try with this IF condition:
if ( UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
{
}
else {}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//write code here
return YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
//write code here
}
Use this two methods for set your label when rotate device.

How remove the gap from screen?

I have an application in which I have apply facility to change orientation of view but when I change the orientation then go to next screen then there show a gap on right side if I rotate device in right side as show in image:
I use this code in viewcontroller.m:
-(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 )
{
lgo_imag.frame=CGRectMake(0, 0, 320, 44);
btn_settings.frame=CGRectMake(238, 3, 72, 37);
btn_alarm_activity.frame=CGRectMake(125, 10, 70, 85);
btn_alarm_screen.frame=CGRectMake(48, 210, 225, 55);
btn_routine_screen.frame=CGRectMake(48, 275, 225, 55);
btn_view_info.frame=CGRectMake(48, 340, 225, 55);
lable_alarm_value.frame=CGRectMake(25, 100, 270, 81);
scroll_view.frame=CGRectMake(0, 44, 320, 416);
scroll_view.contentSize=CGSizeMake(320 ,416);
}
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight )
{
lgo_imag.frame=CGRectMake(0, 0, 480, 44);
btn_settings.frame=CGRectMake(398, 3, 72, 37);
btn_alarm_activity.frame=CGRectMake(205, 10, 70, 85);
btn_alarm_screen.frame=CGRectMake(128, 210, 225, 55);
btn_routine_screen.frame=CGRectMake(128, 275, 225, 55);
btn_view_info.frame=CGRectMake(128, 340, 225, 55);
lable_alarm_value.frame=CGRectMake(125, 100, 270, 81);
scroll_view.frame=CGRectMake(0, 44, 480, 320);
scroll_view.contentSize=CGSizeMake(480 ,470);
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
How fix that problem?
Add +20 to the view frame. This happens because its not including status bar space.
You can use the below property for AutoSizing the controls based on orientation. But be careful in using it, only change those controls property which you want to autosize.
EDIT
Please go through this link iPhone SDK: Orientation (Landscape and Portrait views) I think you are not putting your code in orientation changing method. This link will definitely guide you.
Use this method for orientation change detection..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscape);
}

What is difference between self.view.frame and self.superview.frame and how to use them?

I would like to know that what is difference between these both lines of code?
self.view.frame = CGRectMake(0, 0, 320, 480);
self.view.superview.frame = CGRectMake(0, 0, 800, 900);
and I want to change the view frame when my orientation will change, because it will change the position of labels, and I want them in middle of screen, can anyone guide me ?
I am using following delegate method, for orientation, but it is not working with
self.view.frame
but it is working ok with following line
self.view.superview.frame
See the following code
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(#"LEFT");
self.view.frame = CGRectMake(100, 0, 480, 320);
NSLog(#"Show self.view.frame: %#", NSStringFromCGRect(self.view.frame));
// self.view.superview.frame = CGRectMake(-50, -70, 800, 900);
[button setFrame:CGRectMake(340, 320, 100, 30)];
}
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
NSLog(#"RIGHT");
self.view.frame = CGRectMake(0, 0, 320, 480);
NSLog(#"Show self.view.frame: %#", NSStringFromCGRect(self.view.frame));
//self.view.superview.frame = CGRectMake(10, 90, 800, 900); //It is working if I will uncomment it
[button setFrame:CGRectMake(250, 300, 100, 30)];
}
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
self.view.frame = CGRectMake(0, 0, 320, 480);
//self.view.superview.frame = CGRectMake(0, 0, 800, 900);//It is working if I will uncomment it
[button setFrame:CGRectMake(250, 400, 100, 30)];
}
return YES;
}
self.view is the view of self (if we are talking about viewControllers).
self.view.superview is the view that is holding self.view.
So, in short, if you add a view to the window the superview of that view will be the window.
Setting the frame will fail if the autoresize mask isn't set correctly.
As a generic statement, the first line is trying set the frame of the current viewController's view that this code is written in.
The second line is trying to set the frame of the parent view of the view of the current viewController's view.
What this exactly means, and which one you should be using I'm afraid depends on the view hierarchy you have set up in your application.

Labels not aligning properly in landscape mode

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

Autoresize UIScrollView

I made a UIViewController, which programatically generates a UIScrollView. Everything's fine, but when I rotate the Device, the UIScollView should resize so it takes the complete width of my View.
Is there a way to do that without rebuilding the complete UIScrollView ?
Thx a lot !
Sebastian
This is called in my viewDidLoad:
-(void)buildmyScroller {
myScroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 800, 768, 100)];
//...adding some subviews to myScroller
thumbScroller.contentSize = CGSizeMake(3000, 100);
[[self view] addSubview:myScroller];
}
Then I tried to resize myScroller with this, when I used setFrame, I said myScroller would not respond to it... :
-(void)changemyScroller {
UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
[thumbScroller setFrame:CGRectMake(0, 805, 768, 150)];
}
else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
thumbScroller.frame = CGRectMake(0, 805, 768, 150);
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
thumbScroller.frame = CGRectMake(0, 549, 1024, 150);
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight){
thumbScroller.frame = CGRectMake(0, 549, 1024, 150);
}
}
And called the method in didAnimateFirstHalf... cause I'm not shure where else to call it.
Thx a lot again !!
[scrollView setFrame:CGRectmake(x, y, width, height)];
//Maybe you need to do the same for the content of the scrollView to make it fit your layout
should do it. You can wrap that in an UIAnimation block if it need to be a transition.
Try this:
if(self.rowNumber == 0){
/******************* Scroller Setup *****************/
// how many pages
int pageCount = 5;
//set up the scrollView
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 960)];
// support for Landscape Orienation
if(UIInterfaceOrientationLandscapeLeft){
[scroller setFrame:CGRectMake(0,0,1024, 704)];
}
if(UIInterfaceOrientationLandscapeRight){
[scroller setFrame:CGRectMake(0,0,1024, 704)];
}