Question

i am doing my project in iOS6. I have only one UIViewController for now.ON that i have a UIScrollView. I have several controls on it (text fields,buttons,custom view..etc).So i built that project for portrait mode. Now i want to move it to fit both landscape and portrait. For that i have this code

- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskAll);
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    self.scrollView.contentMode = UIViewContentModeTopRight;
self.scrollView.scrollEnabled = YES;
NSLog(@"the end point is %f",self.currentCGRectLocation.origin.y);
CGPoint scrollPoint = CGPointMake(0.0, (self.currentCGRectLocation.origin.y/500)*400);
[self.scrollView setContentOffset:scrollPoint animated:YES];

CGFloat scrollViewHeight = 0.0f;
for (UIView *view in self.scrollView.subviews)
{
    scrollViewHeight += view.frame.size.height;
}

CGSize newSize = CGSizeMake(100,scrollViewHeight);

[self.scrollView setContentSize:newSize];

self.view.userInteractionEnabled =YES;
self.scrollView.userInteractionEnabled = YES;
}

and in my viewDidLoad

  - (void)viewDidLoad
{
[self.scrollView setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];

}

In my AppDelegate.m file i have

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return (UIInterfaceOrientationMaskAll);
}

My problem is i move it from portrait to landscape then all controls align perfectly except the scroll view. The width of simulator is 900 in portrait mode and in landscape mode its 1024.So my scrollview has its scroll at 900 width even in landscape mode. Its like stuck there even though i can move up and down easily. I am attaching a mockup . Scroll view is at the end of portrait(900) and almost 2/3 at landscape(900 again). The mockup is here : http://dl.dropbox.com/u/40597849/mockup3.png. if you need more information please ask.Thanks..

EDIT: Also on MainStoryboard for UIScrollView i have unchecked "Use Autolayout" checkbox. If i check it, i have the scrollview at the end(as i want) when i move from portrait to landscape.. but when i move again from landscape to portrait the screen freezes. I cant move up nor down.

Était-ce utile?

La solution

This two methods are added in ViewController which i want to move from portrait to landscape. I still keep my "Use Autolayout" checkbox UNCHECKED for scroll view.

- (NSUInteger)supportedInterfaceOrientations
{
  return (UIInterfaceOrientationMaskAll);
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft
    ||  toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
    CGRect rect = self.view.frame;
    rect.size.width = self.view.frame.size.width+245;
    rect.size.height = self.view.frame.size.height+245;
    self.scrollView.frame = rect;
}

}

This is added in AppDelegate.m file

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return (UIInterfaceOrientationMaskAll);
}

This did the trick for me. Hope it helps.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top