当从纵向更改为横向时,滚动视图位置在相同位置,反之亦然

时间:2013-01-07 23:51:24

标签: ios uiscrollview landscape fixed portrait

我在iOS6上做我的项目。我现在只有一个UIViewController,我有一个UIScrollView。我有几个控件(文本字段,按钮,自定义视图..等)。所以我为肖像模式构建了该项目。现在我想移动它以适应风景和肖像。为此,我有这个代码

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

和我的viewDidLoad

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

}

在我的AppDelegate.m文件中,我有

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

我的问题是我将它从纵向移动到横向,然后除滚动视图外所有控件都完美对齐。模拟器的宽度在纵向模式下为900,在横向模式下为1024.因此,即使在横向模式下,我的滚动视图也具有900宽度的滚动。它就像卡在那里,即使我可以轻松上下移动。 我附上了一个样机。滚动视图位于肖像(900)的末尾,并且几乎是景观的2/3(再次是900)。模型在这里:http://dl.dropbox.com/u/40597849/mockup3.png。如果您需要更多信息,请询问。谢谢..

编辑:同样在UIScrollView的MainStoryboard上,我取消选中“使用Autolayout”复选框。如果我检查它,当我从纵向移动到横向时,我在最后(我想要)滚动视图...但是当我再次从横向移动到纵向时,屏幕冻结。我不能向上或向下移动。

1 个答案:

答案 0 :(得分:0)

在ViewController中添加了这两种方法,我希望从纵向移动到横向。我仍然保持我的“使用Autolayout”复选框UNCHECKED滚动视图。

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

}

这是在AppDelegate.m文件中添加的

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

这对我有用。希望它有所帮助。

相关问题